get_option('active')) { $this->check_account_active(3600); // while not active, check every hour } add_action('admin_menu', array($this, 'admin_menu')); if ($this->get_page_type() == 'admin' && false !== stristr($_GET['page'], self::$PLUGIN_SLUG)) { add_action('admin_enqueue_scripts', array($this, 'admin_assets')); } else { add_action('admin_notices', array($this, 'admin_notices')); } if ($this->get_option('active')) { add_action('admin_post_'.self::$ACTION_PREFIX.'save', array($this, 'admin_post_save')); } else { add_action('admin_post_'.self::$ACTION_PREFIX.'check', array($this, 'admin_post_check')); } // TODO: load_plugin_textdomain($PLUGIN_SLUG, false, self::$PLUGIN_DIR.'languages' ); } else { if ($this->get_option('active')) { add_action('wp_head', array($this, 'front_write_script')); add_action('admin_bar_menu', array($this, 'admin_bar_menu'), 1000); } } // make sure we have the cron if (is_admin() && !wp_next_scheduled(self::$ACTION_PREFIX.'cron_check_account')) { wp_schedule_event(time(), 'daily', self::$ACTION_PREFIX.'cron_check_account'); } add_action(self::$ACTION_PREFIX.'cron_check_account', array($this, 'cron_check_account')); register_activation_hook(self::$PLUGIN_DIR.basename(__FILE__), array($this, 'activate_plugin')); register_deactivation_hook(self::$PLUGIN_DIR.basename(__FILE__), array($this, 'deactivate_plugin')); } public static function init() { if (!self::$instance) { self::$PLUGIN_SLUG = basename(__FILE__, '.php'); self::$PLUGIN_DIR = self::$PLUGIN_SLUG.DIRECTORY_SEPARATOR; self::$ACTION_PREFIX = preg_replace('/[^a-z]+/', '_', strtolower(self::$PLUGIN_SLUG)).'_'; self::$instance = new self(); } } public function activate_plugin() { if (!$this->check_account_active()) { if ($check_err = $this->get_option('active_last_check_err')) { die('Error while activating the plugin: please contact support (error: '.$check_err.')'); } } } public function deactivate_plugin() { wp_clear_scheduled_hook(self::$ACTION_PREFIX.'cron_check_account'); } public function cron_check_account() { $this->check_account_active(); } /** * Adding the settings menu */ public function admin_menu() { add_menu_page('heatmap', 'heatmap', 'manage_options', self::$PLUGIN_SLUG, array($this, 'admin_options_page'), $this->get_asset('icon-16.png')); } public function admin_notices() { $this->show_notice( $this->get_page_type() == 'plugins' && !$this->get_option('active'), 'error', 'heatmap', __('You are almost done!', self::$PLUGIN_SLUG).' '. sprintf('%s', admin_url('admin.php?'.http_build_query(array('page' => self::$PLUGIN_SLUG))), __('Click here to setup the plugin', self::$PLUGIN_SLUG)) ); } /** */ public function admin_assets() { wp_enqueue_style(self::$PLUGIN_SLUG.'-admin-style', $this->get_asset('admin.css')); wp_enqueue_style(self::$PLUGIN_SLUG.'-admin-codemirror-style', $this->get_asset('codemirror.css')); wp_enqueue_script('jquery'); wp_enqueue_script(self::$PLUGIN_SLUG.'-admin-codemirror-script', $this->get_asset('codemirror-compressed.js')); } public function admin_options_page() { $this->show_options_page(); } public function admin_bar_menu() { if (!is_admin_bar_showing()) return; global $wp_admin_bar; $wp_admin_bar->add_menu( array( 'id' => self::$PLUGIN_SLUG.'-bar', 'meta' => array('onclick' => self::$JS_TRIGGER.';return false;'), 'title' => $this->get_admin_bar_toggle_button(), 'href' => '#', ) ); } public function admin_post_check() { check_admin_referer(self::$ACTION_PREFIX.'check'); $this->check_account_active(); wp_safe_redirect(wp_get_referer()); } public function admin_post_save() { check_admin_referer(self::$ACTION_PREFIX.'save'); $new_values = array( 'ext_use' => true && $this->array_get($_POST, 'ext_use'), 'ext_code' => $this->array_get($_POST, 'ext_code'), ); $this->set_options($new_values, true); wp_safe_redirect(add_query_arg('saved', '1', wp_get_referer())); } /** * Writing the script on the front-end pages */ public function front_write_script() { ?> get_option('active_last_check', 0) < time() - $frequency) { $params = array( 'u' => get_bloginfo('url'), 'callback' => 'wp' ); if (defined('WP_HEATMAP_AFFILIATEID')) { $params['aff'] = WP_HEATMAP_AFFILIATEID; } $check_url = '//heatmap.it/api/check/account?'.http_build_query($params); $check_response = wp_remote_get('https:'.$check_url, array('timeout' => 10, 'sslverify' => false)); $check_result = false; $check_err = ''; if (is_wp_error($check_response)) { // fallback to http $check_response = wp_remote_get('http:'.$check_url, array('timeout' => 10)); } if (is_wp_error($check_response)) { $check_err = $check_response->get_error_message(); } else { $json_result = trim(preg_replace(array('/[\n\r]/', '/^wp/'), array('', ''), $check_response['body']), '();'); $check_result = json_decode($json_result, true); if (!is_array($check_result) || !array_key_exists('active', $check_result)) { $check_err = 'cannot parse jsonp response '.print_r($json_result,true); } } $options_new_values = array( 'active_last_check' => time(), 'active_last_check_err' => $check_err, ); if ('' === $check_err) { $options_new_values['active'] = $this->array_get($check_result, 'active', false); } elseif ($this->get_option('active_last_check_err')) { // on second successive error, we disable the plugin $options_new_values['active'] = false; } $this->set_options($options_new_values); } return $this->get_option('active'); } private function get_asset($filename) { return plugins_url(self::$PLUGIN_DIR.'assets'.DIRECTORY_SEPARATOR.$filename); } private function get_page_type() { global $pagenow; return str_replace('.php', '', $pagenow); } private function load_options($force_reload = false) { if (!$force_reload && count($this->options) > 0) return; $default_options = array( 'active' => false, 'active_last_check' => 0, 'active_last_check_err' => '', 'ext_use' => false, 'ext_code' => <<< EXT_DEFAULT var heatmap_ext = { cleanupURL: function(url) { return url; }, getCurrentURL: function() { return heatmap_ext.cleanupURL(document.location.href); } }; EXT_DEFAULT ); $option_db_value = get_option(self::$OPTION_NAME); if (empty($option_db_value)) { $this->set_options($default_options); // make sure to save the options in DB so WP won't try to load them separately each time } else { $this->set_options(wp_parse_args($option_db_value, $default_options), false); } } private function get_option($option_key, $default = false) { $this->load_options(); return $this->array_get($this->options, $option_key, $default); } private function set_options($new_values, $save_in_db = true) { $this->options = array_merge($this->options, $new_values); if ($save_in_db) { update_option(self::$OPTION_NAME, $this->options); } } private function show_notice($condition, $type, $title, $message) { if (!$condition) return; echo '
', '', $title, ': ', $message, '
', '