| 1 |
<?php |
| 2 |
|
| 3 |
namespace Pixelavo; |
| 4 |
|
| 5 |
/** |
| 6 |
* Exit if accessed directly |
| 7 |
* */ |
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Base |
| 14 |
*/ |
| 15 |
final class Base { |
| 16 |
|
| 17 |
private static $_instance = null; |
| 18 |
|
| 19 |
/** |
| 20 |
* Instance |
| 21 |
* |
| 22 |
* Initializes a singleton instance |
| 23 |
* |
| 24 |
* @return self class |
| 25 |
*/ |
| 26 |
static function instance() { |
| 27 |
if (is_null( self::$_instance )) { |
| 28 |
self::$_instance = new self(); |
| 29 |
} |
| 30 |
return self::$_instance; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Base class constructor |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
private function __construct() { |
| 38 |
if (!function_exists('is_plugin_active')) { |
| 39 |
include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 40 |
} |
| 41 |
add_action('init', [$this, 'i18n']); |
| 42 |
add_action('plugins_loaded', [$this, 'init']); |
| 43 |
add_action('wp_enqueue_scripts', [$this, 'scripts']); |
| 44 |
/** |
| 45 |
* Register Plugin Active Hook |
| 46 |
*/ |
| 47 |
register_activation_hook( PIXELAVO_PL_ROOT, [ $this, 'plugin_activate_hook' ] ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* i18n |
| 52 |
* |
| 53 |
* Load text domain |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
function i18n() { |
| 57 |
load_plugin_textdomain('pixelavo', false, dirname(plugin_basename(PIXELAVO_PL_ROOT)) . '/languages'); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* init |
| 62 |
* |
| 63 |
* Plugins loaded init hook |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
function init() { |
| 67 |
|
| 68 |
/** |
| 69 |
* Include required files. |
| 70 |
*/ |
| 71 |
$this->include_files(); |
| 72 |
|
| 73 |
/** |
| 74 |
* Redirect to option page after plugin activate |
| 75 |
*/ |
| 76 |
$this->plugin_redirect_option_page(); |
| 77 |
|
| 78 |
/** |
| 79 |
* Add plugin setting link to the plugin. |
| 80 |
*/ |
| 81 |
add_filter('plugin_action_links_' . PIXELAVO_PLUGIN_BASE, [$this, 'plugins_setting_links']); |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Include Require Files |
| 87 |
*/ |
| 88 |
function include_files() { |
| 89 |
require_once (PIXELAVO_PL_PATH . 'includes/helper-functions.php'); |
| 90 |
require_once (PIXELAVO_PL_PATH . 'admin/admin-setting.php'); |
| 91 |
require_once (PIXELAVO_PL_PATH . 'includes/add-pixel.php'); |
| 92 |
require_once (PIXELAVO_PL_PATH . 'includes/pixel-events-data.php'); |
| 93 |
if(pixelavo_get_option('product_feed', 'pixelavo_settings') == 'on') { |
| 94 |
require_once (PIXELAVO_PL_PATH . 'includes/pixel-feed.php'); |
| 95 |
} |
| 96 |
|
| 97 |
if( is_admin() ){ |
| 98 |
require_once (PIXELAVO_PL_PATH . 'admin/class-diagnostic-data.php'); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Scripts |
| 104 |
* |
| 105 |
* Enqueue style and scripts for frontend part |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
function scripts() { |
| 109 |
if(pixelavo_pixel_status()) { |
| 110 |
wp_enqueue_script('pixelavo', PIXELAVO_DIR_URL . 'assets/public/js/main.js', ['jquery'], PIXELAVO_VERSION, true); |
| 111 |
wp_localize_script('pixelavo', 'pixelavo', [ |
| 112 |
'ajax_url' => admin_url('admin-ajax.php'), |
| 113 |
'nonce' => wp_create_nonce('pixelavo_ajax_event_nonce'), |
| 114 |
'custom_events' => json_encode(pixelavo_get_custom_events()), |
| 115 |
'additional_user_info' => apply_filters('pixelavo_additional_user_info', [], [] ) |
| 116 |
]); |
| 117 |
wp_localize_script('pixelavo', 'pixelavo_event', []); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Plugins setting links |
| 123 |
|
| 124 |
* @param array plugin default action links |
| 125 |
* @return array plugin action link |
| 126 |
*/ |
| 127 |
function plugins_setting_links($links) { |
| 128 |
$link = sprintf( |
| 129 |
'<a href="%1$s">%2$s</a>', |
| 130 |
admin_url('admin.php?page=pixelavo#/pixels'), |
| 131 |
__( 'Settings', 'pixelavo' ) |
| 132 |
); |
| 133 |
array_unshift($links, $link); |
| 134 |
return $links; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Plugin Active Hook |
| 139 |
* Run when plugin is activated |
| 140 |
*/ |
| 141 |
public function plugin_activate_hook() { |
| 142 |
$events = get_option('pixelavo_events', [ |
| 143 |
'search' => 'on', |
| 144 |
'view_content' => 'on', |
| 145 |
'view_category' => 'on', |
| 146 |
]); |
| 147 |
update_option('pixelavo_events', $events); |
| 148 |
add_option('pixelavo_do_activation_redirect', true); |
| 149 |
flush_rewrite_rules(); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* After Active the plugin then redirect to option page |
| 154 |
* @return void |
| 155 |
*/ |
| 156 |
public function plugin_redirect_option_page() { |
| 157 |
if ( get_option( 'pixelavo_do_activation_redirect', false ) ) { |
| 158 |
delete_option('pixelavo_do_activation_redirect'); |
| 159 |
if( !isset( $_GET['activate-multi'] ) ){ |
| 160 |
wp_redirect( admin_url("admin.php?page=pixelavo#/pixels") ); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Initialize Base Class |
| 169 |
*/ |
| 170 |
Base::instance(); |
| 171 |
|