← Back
Editing: convert-a-link.php
<?php if (!defined('ABSPATH')) exit; /* Plugin Name: Awin Publisher MasterTag Description: The Awin Publisher MasterTag allows you to access and enable technology from Awin and our partners, and reports AI bot visits to Awin for AI visibility insights. Version: 2.0.0 Requires at least: 5.3 Requires PHP: 7.0 Author: awinglobal Author URI: https://profiles.wordpress.org/awinglobal/ Plugin URI: https://wordpress.org/plugins/convert-a-link License: GPLv2 */ define('PUBLISHER_MASTERTAG_ADMIN_SLUG', 'publisher-mastertag'); /** This plugin's version, not the Publisher MasterTag script's. */ define('PUBLISHER_MASTERTAG_PLUGIN_VERSION', '2.0.0'); define('PUBLISHER_MASTERTAG_DIR', plugin_dir_path(__FILE__)); define('CAL_AIV_OPT_STATUS', 'cal_aiv_status'); define('CAL_AIV_OPT_ENABLED', 'cal_aiv_enabled'); define('CAL_AIV_OPT_SETUP_VERSION', 'cal_aiv_setup_version'); define('CAL_AIV_OPT_BOT_LIST', 'cal_aiv_bot_list'); define('CAL_AIV_TRANSIENT_RETRY_COUNT', 'cal_aiv_retry_count'); require_once PUBLISHER_MASTERTAG_DIR . 'includes/helpers.php'; require_once PUBLISHER_MASTERTAG_DIR . 'includes/class-cal-aiv-bot-list.php'; require_once PUBLISHER_MASTERTAG_DIR . 'includes/class-cal-aiv-detector.php'; require_once PUBLISHER_MASTERTAG_DIR . 'includes/class-cal-aiv-dispatcher.php'; require_once PUBLISHER_MASTERTAG_DIR . 'includes/class-cal-aiv-cdn-checker.php'; ############################################### # MENU PAGES # ############################################### add_action('admin_menu', "convert_a_link_settings"); function convert_a_link_settings() { add_menu_page( 'Awin Publisher MasterTag', 'Awin Publisher MasterTag', 'manage_options', PUBLISHER_MASTERTAG_ADMIN_SLUG, 'publisher_mastertag_render_settings_page', plugins_url('icon.png', __FILE__) ); } function publisher_mastertag_render_settings_page() { require_once dirname(__FILE__) . '/convert-a-link-admin.php'; } add_action('admin_enqueue_scripts', 'convert_a_link_enqueue_admin_assets'); function convert_a_link_enqueue_admin_assets($hook) { if ($hook !== 'toplevel_page_' . PUBLISHER_MASTERTAG_ADMIN_SLUG) { return; } wp_enqueue_style('convert-a-link-admin', plugins_url('assets/admin.css', __FILE__), array(), PUBLISHER_MASTERTAG_PLUGIN_VERSION); } add_action('wp_enqueue_scripts', 'convert_a_link_enqueue_script'); function convert_a_link_enqueue_script() { $publisherId = get_option('cal_publisherId'); $script_url = 'https://www.dwin2.com/pub.' . $publisherId . '.min.js?' . 'plugin_ver=' . PUBLISHER_MASTERTAG_PLUGIN_VERSION . '&wp_ver=' . get_bloginfo('version'); wp_enqueue_script('convert-a-link', $script_url, array(), null, true); } ########################################################## # ADMIN NOTIFICATION # ########################################################## function convert_a_link_admin_notice() { $publisherId = get_option('cal_publisherId'); if (empty($publisherId)) { ?> <div class="notice notice-error"> <p><?php _e('<a href="' . admin_url('admin.php?page=' . PUBLISHER_MASTERTAG_ADMIN_SLUG) . '"> Enter your Publisher ID to configure the MasterTag!</a>', 'my-text-domain'); ?> </p> </div> <?php } } add_action('admin_notices', 'convert_a_link_admin_notice'); ########################################################## # AI VISIBILITY # ########################################################## add_filter('cron_schedules', 'cal_aiv_cron_schedules'); add_action('cal_aiv_setup', array('CAL_AIV_Bot_List', 'fetch_and_cache')); add_action('cal_aiv_bot_refresh', array('CAL_AIV_Bot_List', 'fetch_and_cache')); add_action('cal_aiv_retry_fetch', array('CAL_AIV_Bot_List', 'fetch_and_cache')); add_action('admin_post_cal_aiv_refresh', array('CAL_AIV_Bot_List', 'manual_refresh')); add_action('admin_post_cal_aiv_toggle', 'cal_aiv_handle_toggle'); add_action('cal_aiv_flush', array('CAL_AIV_Dispatcher', 'flush')); // Activation hooks don't run on plugin updates, so existing installs set themselves up here. add_action('plugins_loaded', 'cal_aiv_maybe_setup'); add_action('plugins_loaded', 'cal_aiv_register_runtime_hooks'); function cal_aiv_register_runtime_hooks() { if (cal_aiv_is_active()) { add_action('init', array('CAL_AIV_Detector', 'run'), 1); add_action('shutdown', array('CAL_AIV_Dispatcher', 'run'), 999); } } function cal_aiv_cron_schedules($schedules) { if (!isset($schedules['cal_aiv_6hours'])) { $schedules['cal_aiv_6hours'] = array( 'interval' => 6 * HOUR_IN_SECONDS, 'display' => 'Every 6 hours', ); } return $schedules; } function cal_aiv_maybe_setup() { if (get_option(CAL_AIV_OPT_SETUP_VERSION) === PUBLISHER_MASTERTAG_PLUGIN_VERSION) { // Puts the refresh back if a cron-manager plugin or a lost cron write removed it; reads the autoloaded cron option. if (!wp_next_scheduled('cal_aiv_bot_refresh')) { cal_aiv_schedule_events(); } return; } // Saved first so concurrent requests during an update don't all run setup. update_option(CAL_AIV_OPT_SETUP_VERSION, PUBLISHER_MASTERTAG_PLUGIN_VERSION); // Stored so it autoloads; a missing option costs a query on every page view. add_option keeps an existing '0'. add_option(CAL_AIV_OPT_ENABLED, '1'); cal_aiv_schedule_events(); } // No HTTP call here: the first fetch is deferred to cron so a visitor's page never waits on it. function cal_aiv_schedule_events() { if (!wp_next_scheduled('cal_aiv_bot_refresh')) { // Random start spreads bot list requests after a release. wp_schedule_event(time() + wp_rand(0, 6 * HOUR_IN_SECONDS), 'cal_aiv_6hours', 'cal_aiv_bot_refresh'); } if (!wp_next_scheduled('cal_aiv_setup')) { // Random delay so sites updating at the same time don't all fetch the list at once. wp_schedule_single_event(time() + wp_rand(5, HOUR_IN_SECONDS), 'cal_aiv_setup'); } } // Missing option means on: installs updating from 1.x have never seen the checkbox. function cal_aiv_is_enabled() { return get_option(CAL_AIV_OPT_ENABLED, '1') === '1'; } function cal_aiv_is_active() { return cal_aiv_is_enabled() && cal_aiv_publisher_id() > 0 && !empty(CAL_AIV_Bot_List::get_list()); } function cal_aiv_handle_toggle() { check_admin_referer('cal_aiv_toggle_nonce'); if (!current_user_can('manage_options')) { wp_die(esc_html('Unauthorized'), '', array('response' => 403)); } $enabled = !empty($_POST['cal_aiv_enabled']); update_option(CAL_AIV_OPT_ENABLED, $enabled ? '1' : '0'); if ($enabled) { CAL_AIV_Bot_List::fetch_and_cache(); } else { // Emptied, not deleted: a missing option would cost a query on every page view. update_option(CAL_AIV_OPT_BOT_LIST, array()); delete_option(CAL_AIV_OPT_STATUS); delete_transient(CAL_AIV_TRANSIENT_RETRY_COUNT); wp_clear_scheduled_hook('cal_aiv_retry_fetch'); CAL_AIV_Dispatcher::clear_queue(); } wp_safe_redirect(add_query_arg(array( 'page' => PUBLISHER_MASTERTAG_ADMIN_SLUG, 'aiv' => $enabled ? 'on' : 'off', ), admin_url('admin.php'))); exit; } ########################################################## # ACTIVATION / DEACTIVATION # ########################################################## register_activation_hook(__FILE__, 'convert_a_link_activate'); function convert_a_link_activate() { // Same setup as an update, so a fresh install also stores cal_aiv_enabled. cal_aiv_maybe_setup(); } register_deactivation_hook(__FILE__, 'convert_a_link_deactivate'); function convert_a_link_deactivate() { delete_option('cal_publisherId'); // Cleared so reactivating runs setup again. delete_option(CAL_AIV_OPT_SETUP_VERSION); delete_option(CAL_AIV_OPT_BOT_LIST); delete_option(CAL_AIV_OPT_STATUS); delete_transient(CAL_AIV_TRANSIENT_RETRY_COUNT); wp_clear_scheduled_hook('cal_aiv_setup'); wp_clear_scheduled_hook('cal_aiv_bot_refresh'); wp_clear_scheduled_hook('cal_aiv_retry_fetch'); CAL_AIV_Dispatcher::clear_queue(); }
Save File
Cancel