true, 'strategy' => 'defer') ); wp_enqueue_style( 'add-smart-wayback-machine-links-to-urls-css', WAYBACK_FILTER_PLUGIN_URL . 'css/add-smart-wayback-machine-links-to-urls.css', array(), WAYBACK_FILTER_VERSION ); } } /** * Enqueue admin scripts */ public function admin_enqueue_scripts($hook) { if ($hook === 'settings_page_add-smart-wayback-machine-links-to-urls') { wp_enqueue_script( 'add-smart-wayback-machine-links-to-urls-admin-js', WAYBACK_FILTER_PLUGIN_URL . 'js/add-smart-wayback-machine-links-to-urls-admin.js', array('jquery'), WAYBACK_FILTER_VERSION, array('in_footer' => true) ); } } /** * Add admin menu */ public function add_admin_menu() { add_options_page( __('Add Smart Wayback Machine links to URLs Settings', 'add-smart-wayback-machine-links-to-urls'), __('Add Smart Wayback Machine links to URLs', 'add-smart-wayback-machine-links-to-urls'), 'manage_options', 'add-smart-wayback-machine-links-to-urls', array($this, 'admin_page') ); } /** * Register plugin settings */ public function register_settings() { register_setting('wayback_filter_settings', 'wayback_filter_options', array( 'type' => 'array', 'sanitize_callback' => array($this, 'validate_options'), 'default' => array() )); add_settings_section( 'wayback_filter_main', __('Add Smart Wayback Machine links to URLs Settings', 'add-smart-wayback-machine-links-to-urls'), array($this, 'settings_section_callback'), 'add-smart-wayback-machine-links-to-urls' ); add_settings_field( 'waybacklink_icon', __('Icon or text for the Wayback link', 'add-smart-wayback-machine-links-to-urls'), array($this, 'icon_field_callback'), 'add-smart-wayback-machine-links-to-urls', 'wayback_filter_main' ); add_settings_field( 'waybacklink_title', __('Link title for the Wayback link', 'add-smart-wayback-machine-links-to-urls'), array($this, 'title_field_callback'), 'add-smart-wayback-machine-links-to-urls', 'wayback_filter_main' ); add_settings_field( 'waybacklink_start', __('Place Wayback links on posts older than...', 'add-smart-wayback-machine-links-to-urls'), array($this, 'start_field_callback'), 'add-smart-wayback-machine-links-to-urls', 'wayback_filter_main' ); add_settings_field( 'field_types', __('Content types to apply filter to', 'add-smart-wayback-machine-links-to-urls'), array($this, 'field_types_callback'), 'add-smart-wayback-machine-links-to-urls', 'wayback_filter_main' ); } /** * Settings section callback */ public function settings_section_callback() { echo '
' . esc_html__('Configure how Wayback Machine links are added to your older posts.', 'add-smart-wayback-machine-links-to-urls') . '
'; } /** * Renders the input field for the wayback link icon. * * This function is a callback for the WordPress Settings API (add_settings_field). * It displays a text input where users can set the icon/emoji and provides * helpful recommendations and links. * * @since 1.0.0 * * @param array $args Arguments passed from add_settings_field. Not used in this function. */ public function icon_field_callback( $args = array() ) { // 1. Define constants/variables for option names and defaults. $option_group = 'wayback_filter_options'; $option_name = 'waybacklink_icon'; $default_icon = '🏛️'; // 2. Get the saved option value, providing a default. $options = get_option( $option_group, [] ); $value = isset( $options[ $option_name ] ) && ! empty( $options[ $option_name ] ) ? $options[ $option_name ] : $default_icon; // 3. Prepare the description text for translation. $description_text = sprintf( /* translators: 1: A link to the 'building' emoji. 2: A link to the 'floppy disk' emoji. 3: A link to the 'circle arrow' unicode character. 4: The text for an example link. 5: A link to the Emojipedia website. */ esc_html__( 'Enter an emoji, a unicode character or some text. %1$s We recommend %2$s (classical building), %3$s (floppy disk), %4$s (circle arrow) or simply the text %5$s. %1$s You can find more emojis at %6$s.', 'add-smart-wayback-machine-links-to-urls' ), ''; echo '
' . esc_html__('This is what desktop users get to read when their cursor hovers over the Wayback link.', 'add-smart-wayback-machine-links-to-urls') . '
'; } /** * Start field callback */ public function start_field_callback() { $options = get_option('wayback_filter_options', array()); $value = isset($options['waybacklink_start']) ? $options['waybacklink_start'] : 3; echo ' ' . esc_html__('years', 'add-smart-wayback-machine-links-to-urls'); echo '' . esc_html__('Posts older than this many years will get trailing Wayback links.', 'add-smart-wayback-machine-links-to-urls') . '
'; } /** * Field types callback */ public function field_types_callback() { $options = get_option('wayback_filter_options', array()); $post_types = get_post_types(array('public' => true), 'objects'); $selected_types = isset($options['field_types']) ? $options['field_types'] : array('post'); foreach ($post_types as $post_type) { $checked = in_array($post_type->name, $selected_types, true) ? 'checked="checked"' : ''; echo '' . esc_html__('Select which post types should have Wayback links added to their content.', 'add-smart-wayback-machine-links-to-urls') . '
'; } /** * Validate options */ public function validate_options($input) { $validated = array(); if (isset($input['waybacklink_icon'])) { $validated['waybacklink_icon'] = sanitize_text_field($input['waybacklink_icon']); } if (isset($input['waybacklink_title'])) { $validated['waybacklink_title'] = sanitize_text_field($input['waybacklink_title']); } if (isset($input['waybacklink_start'])) { $validated['waybacklink_start'] = absint($input['waybacklink_start']); } if (isset($input['field_types']) && is_array($input['field_types'])) { $validated['field_types'] = array_map('sanitize_key', $input['field_types']); } return $validated; } /** * Admin page */ public function admin_page() { if (!current_user_can('manage_options')) { wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'add-smart-wayback-machine-links-to-urls')); } ?> post_type, $post->post_date)) { return $content; } $options = get_option('wayback_filter_options', array()); $selected_types = isset($options['field_types']) ? $options['field_types'] : array('post'); // Check if current post type should be processed if (!in_array($post->post_type, $selected_types, true)) { return $content; } // Check post age $waybacklink_start = isset($options['waybacklink_start']) ? $options['waybacklink_start'] : 3; $waybacklink_start_date = strtotime('-' . $waybacklink_start . ' years'); if ($waybacklink_start === 0) { $waybacklink_start_date = 0; // Always show for 0 years } $post_date = strtotime($post->post_date); if ($post_date > $waybacklink_start_date) { return $content; // Post is too new } // Process links in content $waybacklink_icon = isset($options['waybacklink_icon']) ? $options['waybacklink_icon'] : '🏛️'; $waybacklink_title = isset($options['waybacklink_title']) ? $options['waybacklink_title'] : __('Click to see an archived version of the destination page.', 'add-smart-wayback-machine-links-to-urls'); $time = gmdate('YmdHis', $post_date); // Regular expression to find links $regexp = '/]*href=(["\'`])([^"\' >]*?)\1[^>]*>(.*?)<\/a>/si'; preg_match_all($regexp, $content, $matches, PREG_SET_ORDER); $processed_urls = array(); foreach ($matches as $match) { $full_link = $match[0]; $url = $match[2]; // Skip if already a Wayback link if (strpos($url, 'web.archive.org') !== false) { continue; } // Skip if not http/https if (!preg_match('/^https?:\/\//', $url)) { continue; } // Skip if already processed if (isset($processed_urls[$url])) { continue; } $processed_urls[$url] = true; // Create Wayback link $wayback_url = 'https://web.archive.org/web/' . $time . '/' . $url; $wayback_link = ' ' . esc_html($waybacklink_icon) . ''; // Replace the original link with link + wayback link $content = str_replace($full_link, $full_link . $wayback_link, $content); } return $content; } /** * Plugin activation */ public function activate() { // Set default options $default_options = array( 'waybacklink_icon' => '🏛️', 'waybacklink_title' => __('Click to see an archived version of the destination page.', 'add-smart-wayback-machine-links-to-urls'), 'waybacklink_start' => 3, 'field_types' => array('post') ); add_option('wayback_filter_options', $default_options); } /** * Plugin deactivation */ public function deactivate() { // Clean up if needed } } // Initialize the plugin new WaybackFilter();