| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
$tabpx_options = get_option('taboola_pixel_settings'); |
| 9 |
$tabpx_account_id = isset($tabpx_options['account_id']) ? trim(esc_js($tabpx_options['account_id'])) : ''; |
| 10 |
|
| 11 |
// Hook to add the admin menu |
| 12 |
add_action('admin_menu', 'tabpx_add_admin_menu'); |
| 13 |
|
| 14 |
// Hook to enqueue the admin styles |
| 15 |
add_action('admin_enqueue_scripts', 'tabpx_enqueue_admin_styles'); |
| 16 |
|
| 17 |
add_action('admin_init', 'tabpx_settings_init'); |
| 18 |
|
| 19 |
function tabpx_settings_init() |
| 20 |
{ |
| 21 |
// Register the setting |
| 22 |
register_setting('taboola_pixel_settings_group', 'taboola_pixel_settings', 'tabpx_sanitize_settings'); |
| 23 |
|
| 24 |
// Add a settings field |
| 25 |
add_settings_field( |
| 26 |
'taboola_pixel_syndicator_id', |
| 27 |
'Syndicator ID', |
| 28 |
'taboola_pixel_syndicator_id_callback', |
| 29 |
'taboola-pixel-plugin', |
| 30 |
'taboola_pixel_main_section' |
| 31 |
); |
| 32 |
} |
| 33 |
|
| 34 |
function tabpx_sanitize_settings($input) |
| 35 |
{ |
| 36 |
$sanitized = array(); |
| 37 |
|
| 38 |
if (isset($input['account_id'])) { |
| 39 |
$sanitized['account_id'] = sanitize_text_field($input['account_id']); |
| 40 |
} |
| 41 |
|
| 42 |
return $sanitized; |
| 43 |
} |
| 44 |
|
| 45 |
function tabpx_add_admin_menu() |
| 46 |
{ |
| 47 |
add_menu_page( |
| 48 |
'Taboola Pixel', // Page title |
| 49 |
'Taboola Pixel', // Menu title |
| 50 |
'manage_options', // Capability |
| 51 |
'taboola-pixel-admin', // Menu slug |
| 52 |
'tabpx_admin_page', // Callback function |
| 53 |
plugin_dir_url(__DIR__) . 'assets/icons/menu_icon.png', // Icon URL |
| 54 |
100 // Position |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
function tabpx_admin_page() |
| 59 |
{ |
| 60 |
$tabpx_options = get_option('taboola_pixel_settings'); |
| 61 |
$tabpx_account_id = isset($tabpx_options['account_id']) ? trim(esc_js($tabpx_options['account_id'])) : ''; |
| 62 |
$button_text = $tabpx_account_id ? 'Uninstall' : 'Install Pixel'; |
| 63 |
$is_pixel_installed = $tabpx_account_id ? true : false; |
| 64 |
|
| 65 |
include plugin_dir_path(__FILE__) . '../templates/introduction.php'; |
| 66 |
} |
| 67 |
|
| 68 |
function tabpx_enqueue_admin_styles() |
| 69 |
{ |
| 70 |
$version = tabpx_get_plugin_version(); |
| 71 |
wp_enqueue_style('taboola-pixel-admin-style', plugin_dir_url(__DIR__) . 'assets/css/style.css', array(), $version); |
| 72 |
} |
| 73 |
|
| 74 |
function tabpx_enqueue_admin_scripts() |
| 75 |
{ |
| 76 |
$version = tabpx_get_plugin_version(); |
| 77 |
wp_enqueue_script('taboola-pixel-admin-script', plugin_dir_url(__DIR__) . 'assets/js/script.js', array('jquery'), $version, true); |
| 78 |
|
| 79 |
// Determine platform based on WooCommerce presence |
| 80 |
$platform = tabpx_is_woocommerce_active() ? 'wc' : 'wp'; |
| 81 |
|
| 82 |
wp_localize_script('taboola-pixel-admin-script', 'tabpx_ajax', array( |
| 83 |
'ajax_url' => admin_url('admin-ajax.php'), |
| 84 |
'nonce' => wp_create_nonce('taboola_pixel_nonce'), |
| 85 |
'third_party_connect_host' => TABPX_THIRD_PARTY_CONNECT_HOST, |
| 86 |
'api_host' => TABPX_API_HOST, |
| 87 |
'platform' => $platform, |
| 88 |
'plugin_url' => plugin_dir_url(__DIR__), |
| 89 |
'loading_url' => plugin_dir_url(__DIR__) . 'assets/loading.html' |
| 90 |
)); |
| 91 |
} |
| 92 |
|
| 93 |
add_action('admin_enqueue_scripts', 'tabpx_enqueue_admin_scripts'); |
| 94 |
|
| 95 |
function tabpx_install_account_id() |
| 96 |
{ |
| 97 |
if (!current_user_can('manage_options')) { |
| 98 |
wp_send_json_error('Unauthorized', 403); |
| 99 |
} |
| 100 |
|
| 101 |
check_ajax_referer('taboola_pixel_nonce', 'nonce'); |
| 102 |
|
| 103 |
if (isset($_POST['account_id'])) { |
| 104 |
$account_id = sanitize_text_field(wp_unslash($_POST['account_id'])); |
| 105 |
$tabpx_options = get_option('taboola_pixel_settings', array()); |
| 106 |
$tabpx_options['account_id'] = $account_id; |
| 107 |
update_option('taboola_pixel_settings', $tabpx_options); |
| 108 |
wp_send_json_success(); |
| 109 |
} else { |
| 110 |
wp_send_json_error(); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
function tabpx_uninstall_account_id() |
| 115 |
{ |
| 116 |
if (!current_user_can('manage_options')) { |
| 117 |
wp_send_json_error('Unauthorized', 403); |
| 118 |
} |
| 119 |
|
| 120 |
check_ajax_referer('taboola_pixel_nonce', 'nonce'); |
| 121 |
$tabpx_options = get_option('taboola_pixel_settings', array()); |
| 122 |
unset($tabpx_options['account_id']); |
| 123 |
update_option('taboola_pixel_settings', $tabpx_options); |
| 124 |
wp_send_json_success(); |
| 125 |
} |
| 126 |
|
| 127 |
add_action('wp_ajax_tabpx_install_account_id', 'tabpx_install_account_id'); |
| 128 |
add_action('wp_ajax_tabpx_uninstall_account_id', 'tabpx_uninstall_account_id'); |
| 129 |
|
| 130 |
function tabpx_inject_topbar() |
| 131 |
{ |
| 132 |
$screen = get_current_screen(); |
| 133 |
if ($screen && $screen->id === 'toplevel_page_taboola-pixel-admin') { |
| 134 |
?> |
| 135 |
<div class="taboola-pixel-plugin"> |
| 136 |
<div class="topbar"> |
| 137 |
<span> |
| 138 |
<img class="icon" src="<?php echo esc_url(plugin_dir_url(__DIR__) . 'assets/icons/logo.png'); ?>" width="20" height="20" alt="Taboola Logo"> |
| 139 |
</span> |
| 140 |
<span>Taboola Pixel</span> |
| 141 |
</div> |
| 142 |
</div> |
| 143 |
<?php |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
add_action('in_admin_header', 'tabpx_inject_topbar'); |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|