/** * * @return Advanced_Ads_Plugin */ public static function get_instance() { // If the single instance hasn't been set, set it now. if (null === self::$instance) { self::$instance = new self(); } return self::$instance; }
public function __construct() { $this->plugin = Advanced_Ads_Plugin::get_instance(); // load notices $this->load_notices(); // display notices $this->display_notices(); add_action('advanced-ads-ad-params-before', array($this, 'adsense_tutorial'), 10, 2); }
public function init() { if (!current_user_can(Advanced_Ads_Plugin::user_cap('advanced_ads_place_ads'))) { return; } if ('true' == get_user_option('rich_editing')) { add_filter('mce_external_plugins', array($this, 'add_plugin')); add_filter('mce_buttons', array($this, 'register_buttons')); add_filter('mce_external_languages', array($this, 'add_l10n')); add_action('wp_ajax_advads_content_for_shortcode_creator', array($this, 'get_content_for_shortcode_creator')); } }
/** * manipulate output of ad widget * * @since 1.6.8.2 * @param arr $params widget and sidebar params */ public function manipulate_widget_output($params = array()) { if ($params[0]['widget_name'] === 'Advanced Ads') { $options = $this->plugin->options(); // hide id by default (when options are empty) or when option is enabled if ($options === array() || isset($options['remove-widget-id']) && $options['remove-widget-id']) { $pattern = '#\\s(id)=("|\')[^"^\']+("|\')#'; $params[0]['before_widget'] = preg_replace($pattern, '', $params[0]['before_widget']); } } return $params; }
function __construct() { $options = Advanced_Ads_Plugin::get_instance()->options(); $prefix = Advanced_Ads_Plugin::get_instance()->get_frontend_prefix(); $classname = $prefix . 'widget'; $widget_ops = array('classname' => $classname, 'description' => __('Display Ads and Ad Groups.', 'advanced-ads')); $control_ops = array(); // deprecated to keep previously changed prefixed working $prefix2 = isset($options['id-prefix']) && $options['id-prefix'] !== '' ? $options['id-prefix'] : 'advads_ad_'; $base_id = $prefix2 . 'widget'; parent::__construct($base_id, 'Advanced Ads', $widget_ops, $control_ops); }
/** * load interface for single visitor condition * * @since 1.5.4 */ public function load_visitor_condition() { if (!current_user_can(Advanced_Ads_Plugin::user_cap('advanced_ads_edit_ads'))) { return; } // get visitor condition types $visitor_conditions = Advanced_Ads_Visitor_Conditions::get_instance()->conditions; $condition = array(); $condition['type'] = isset($_POST['type']) ? $_POST['type'] : ''; $condition['connector'] = isset($_POST['connector']) ? $_POST['connector'] : ''; $index = isset($_POST['index']) ? $_POST['index'] : 0; if (isset($visitor_conditions[$condition['type']])) { $metabox = $visitor_conditions[$condition['type']]['metabox']; } else { die; } if (method_exists($metabox[0], $metabox[1])) { call_user_func(array($metabox[0], $metabox[1]), $condition, $index); } die; }
/** * create a random wrapper id * * @since 1.1.4 * @return string $id random id string */ private function create_wrapper_id() { if (isset($this->output['wrapper-id'])) { $id = sanitize_key($this->output['wrapper-id']); if ('' !== $id) { return $id; } } $prefix = Advanced_Ads_Plugin::get_instance()->get_frontend_prefix(); return $prefix . mt_rand(); }
/** * inject ads directly into the content * * @since 1.2.1 * @param string $placement_id id of the placement * @param arr $options placement options * @param string $content * @return type * @link inspired by http://www.wpbeginner.com/wp-tutorials/how-to-insert-ads-within-your-post-content-in-wordpress/ */ public static function &inject_in_content($placement_id, $options, &$content) { // test ad is emtpy $whitespaces = json_decode('"\\t\\n\\r \\u00A0"'); $adContent = Advanced_Ads_Select::get_instance()->get_ad_by_method($placement_id, 'placement', $options); if (trim($adContent, $whitespaces) === '') { return $content; } // parse document as DOM (fragment - having only a part of an actual post given) // -TODO may want to verify the wpcharset is supported by server (mb_list_encodings) // prevent messages from dom parser $wpCharset = get_bloginfo('charset'); // check if mbstring exists if (!function_exists('mb_convert_encoding')) { if ($wpCharset === "UTF-8") { $content = htmlspecialchars_decode(htmlentities($content, ENT_COMPAT, $wpCharset, false)); } else { return $content; } } else { $content = mb_convert_encoding($content, 'HTML-ENTITIES', $wpCharset); } // check which priority the wpautop filter has; might have been disabled on purpose $wpautop_priority = has_filter('the_content', 'wpautop'); if ($wpautop_priority && Advanced_Ads_Plugin::get_instance()->get_content_injection_priority() < $wpautop_priority) { $content = wpautop($content); } $dom = new DOMDocument('1.0', $wpCharset); // may loose some fragments or add autop-like code libxml_use_internal_errors(true); // avoid notices and warnings - html is most likely malformed $success = $dom->loadHtml('<!DOCTYPE html><html><meta http-equiv="Content-Type" content="text/html; charset=' . $wpCharset . '" /><body>' . $content); libxml_use_internal_errors(false); if ($success !== true) { // -TODO handle cases were dom-parsing failed (at least inform user) return $content; } // parse arguments $tag = isset($options['tag']) ? $options['tag'] : 'p'; $tag = preg_replace('/[^a-z0-9]/i', '', $tag); // simplify tag // only has before and after $before = isset($options['position']) && $options['position'] === 'before'; $paragraph_id = isset($options['index']) ? $options['index'] : 1; $paragraph_id = max(1, (int) $paragraph_id); $paragraph_select_from_bottom = isset($options['start_from_bottom']) && $options['start_from_bottom']; // select positions $xpath = new DOMXPath($dom); $items = $xpath->query('/html/body/' . $tag); $offset = null; $options = array('allowEmpty' => false); // if there are too few items at this level test nesting $options['itemLimit'] = $tag === 'p' ? 2 : 1; // allow hooks to change some options $options = apply_filters('advanced-ads-placement-content-injection-options', $options, $tag); if ($items->length < $options['itemLimit']) { $items = $xpath->query('/html/body/*/' . $tag); } // try third level as last resort if ($items->length < $options['itemLimit']) { $items = $xpath->query('/html/body/*/*/' . $tag); } // allow to select other elements $items = apply_filters('advanced-ads-placement-content-injection-items', $items, $xpath, $tag); // filter empty tags from items $paragraphs = array(); foreach ($items as $item) { if ($options['allowEmpty'] || isset($item->textContent) && trim($item->textContent, $whitespaces) !== '') { $paragraphs[] = $item; } } $paragraph_count = count($paragraphs); if ($paragraph_count >= $paragraph_id) { $offset = $paragraph_select_from_bottom ? $paragraph_count - $paragraph_id : $paragraph_id - 1; // convert HTML to XML! $adDom = new DOMDocument('1.0', $wpCharset); libxml_use_internal_errors(true); $adDom->loadHtml('<!DOCTYPE html><html><meta http-equiv="Content-Type" content="text/html; charset=' . $wpCharset . '" /><body>' . $adContent); $adNode = $adDom->lastChild->lastChild; // >html>body libxml_use_internal_errors(false); $adContent = $adDom->saveXML($adNode); $adContent = substr($adContent, 6, -7); $adNode = $dom->createDocumentFragment(); $adNode->appendXML($adContent); // inject $node = apply_filters('advanced-ads-placement-content-injection-node', $paragraphs[$offset], $tag, $before); if ($before) { $refNode = $node; $items = $xpath->query('/html/body/' . $tag); $refNode->parentNode->insertBefore($adNode, $refNode); } else { // append before next node or as last child to body $refNode = $node->nextSibling; if (isset($refNode)) { $refNode->parentNode->insertBefore($adNode, $refNode); } else { // append to body; -TODO using here that we only select direct children of the body tag $paragraphs[$offset]->parentNode->appendChild($adNode); } } } // convert to text-representation $content = $dom->saveHTML(); // remove head and tail (required for dom parser but unwanted for content) $content = substr($content, stripos($content, '<body>') + 6); $content = str_replace(array('</body>', '</html>'), '', $content); // no fall-back desired: if there are too few paragraphs do nothing // fix shortcode quotes (malformed by backend editor) $matches = array(); if (0 < preg_match_all('/\\[[^]]+\\]/Siu', $content, $matches, PREG_OFFSET_CAPTURE) && isset($matches[0])) { foreach ($matches[0] as $match) { $offset = $match[1]; $content = substr($content, 0, $offset) . str_replace(array('“', '″', '“', '"e;', '″'), '"', $match[0]) . substr($content, $offset + strlen($match[0])); } } return $content; }
/** * register license key notices */ public function register_license_notices() { if (Advanced_Ads_Admin::screen_belongs_to_advanced_ads()) { $options = $this->options(); $queue = isset($options['queue']) ? $options['queue'] : array(); // check license keys if (Advanced_Ads_Plugin::check_licenses_invalid() && !in_array('license_invalid', $queue)) { $this->notices[] = 'license_invalid'; } else { $this->remove_from_queue('license_invalid'); } // check expiring licenses if (Advanced_Ads_Plugin::check_licenses_expire() && !in_array('license_expires', $queue)) { $this->notices[] = 'license_expires'; } else { $this->remove_from_queue('license_expires'); } // check expired licenses if (Advanced_Ads_Plugin::check_licenses_expired() && !in_array('license_expired', $queue)) { $this->notices[] = 'license_expired'; } else { $this->remove_from_queue('license_expired'); } } }
/** * create a random wrapper id * * @since 1.1.4 * @return string $id random id string */ private function create_wrapper_id() { $prefix = Advanced_Ads_Plugin::get_instance()->get_frontend_prefix(); return $prefix . mt_rand(); }
/** * injected ad into content (before and after) * displays ALL ads * * @since 1.1.0 * @param str $content post content */ public function inject_content($content = '') { // run only within the loop on single pages of public post types $public_post_types = get_post_types(array('public' => true, 'publicly_queryable' => true), 'names', 'or'); // check if admin allows injection in all places $options = $this->plugin->options(); if (!isset($options['content-injection-everywhere'])) { // check if this is a singular page within the loop if (!is_singular($public_post_types) || !in_the_loop()) { return $content; } } $placements = get_option('advads-ads-placements', array()); foreach ($placements as $_placement_id => $_placement) { if (empty($_placement['item']) || !isset($_placement['type'])) { continue; } $_options = isset($_placement['options']) ? $_placement['options'] : array(); switch ($_placement['type']) { case 'post_top': // TODO broken: does not serve placement but serves ad directly $content = Advanced_Ads_Select::get_instance()->get_ad_by_method($_placement_id, Advanced_Ads_Select::PLACEMENT, $_options) . $content; break; case 'post_bottom': $content .= Advanced_Ads_Select::get_instance()->get_ad_by_method($_placement_id, Advanced_Ads_Select::PLACEMENT, $_options); break; case 'post_content': $content = Advanced_Ads_Placements::inject_in_content($_placement_id, $_options, $content); break; } } return $content; }
/** * bulk update groups * */ public function update_groups() { // check nonce if (!isset($_POST['advads-group-update-nonce']) || !wp_verify_nonce($_POST['advads-group-update-nonce'], 'update-advads-groups')) { return new WP_Error('invalid_ad_group', __('Invalid Ad Group', 'advanced-ads')); } // check user rights if (!current_user_can(Advanced_Ads_Plugin::user_cap('advanced_ads_edit_ads'))) { return new WP_Error('invalid_ad_group_rights', __('You don’t have permission to change the ad groups', 'advanced-ads')); } // iterate through groups if (isset($_POST['advads-groups']) && count($_POST['advads-groups'])) { // empty group settings update_option('advads-ad-groups', array()); foreach ($_POST['advads-groups'] as $_group_id => $_group) { // save basic wp term wp_update_term($_group_id, Advanced_Ads::AD_GROUP_TAXONOMY, $_group); // save ad weights $group = new Advanced_Ads_Group($_group['id']); if (isset($_group['ads'])) { $group->save_ad_weights($_group['ads']); } // save other attributes $type = isset($_group['type']) ? $_group['type'] : 'default'; $ad_count = isset($_group['ad_count']) ? $_group['ad_count'] : 1; $options = isset($_group['options']) ? $_group['options'] : array(); // allow other add-ons to save their own group attributes $atts = apply_filters('advanced-ads-group-save-atts', array('type' => $type, 'ad_count' => $ad_count, 'options' => $options), $_group); $group->save($atts); } } // reload groups $this->load_groups(); return true; }
/** * return slider options * * @param obj $group Advanced_Ads_Group * @return array that contains slider options */ public static function get_slider_options(Advanced_Ads_Group $group) { $settings = array(); if (isset($group->options['slider']['delay'])) { $settings['delay'] = absint($group->options['slider']['delay']); $settings['autoplay'] = 'true'; $settings['nav'] = 'false'; $settings['arrows'] = 'false'; } $settings = apply_filters('advanced-ads-slider-settings', $settings); // merge option keys and values in preparation for the option string $setting_attributes = array_map(array('Advanced_Ads_Slider', 'map_settings'), array_values($settings), array_keys($settings)); $settings = implode(', ', $setting_attributes); $prefix = Advanced_Ads_Plugin::get_instance()->get_frontend_prefix(); $slider_id = $prefix . 'slider-' . $group->id; $slider_init_class = $prefix . 'slider-' . mt_rand(); return array('prefix' => $prefix, 'slider_id' => $slider_id, 'init_class' => $slider_init_class, 'settings' => $settings); }
/** * initiate the admin notices class * * @since 1.5.3 */ public function admin_notices() { // display ad block warning to everyone who can edit ads if (current_user_can(Advanced_Ads_Plugin::user_cap('advanced_ads_edit_ads'))) { if ($this->screen_belongs_to_advanced_ads()) { include ADVADS_BASE_PATH . 'admin/views/notices/adblock.php'; } } if (current_user_can(Advanced_Ads_Plugin::user_cap('advanced_ads_edit_ads'))) { $this->notices = Advanced_Ads_Admin_Notices::get_instance(); } }
/** * return content of a placement * * @since 1.1.0 * @param string $id slug of the display * @param array $args optional arguments (passed to child) */ public static function output($id = '', $args = array()) { // get placement data for the slug if ($id == '') { return; } $placements = Advanced_Ads::get_ad_placements_array(); if (isset($placements[$id]['item']) && $placements[$id]['item'] !== '') { $_item = explode('_', $placements[$id]['item']); if (!isset($_item[1]) || empty($_item[1])) { return; } // inject options if (isset($placements[$id]['options']) && is_array($placements[$id]['options'])) { foreach ($placements[$id]['options'] as $_k => $_v) { if (!isset($args[$_k])) { $args[$_k] = $_v; } } } // options $prefix = Advanced_Ads_Plugin::get_instance()->get_frontend_prefix(); // return either ad or group content switch ($_item[0]) { case 'ad': case Advanced_Ads_Select::AD: // create class from placement id (not if header injection) if (!isset($placements[$id]['type']) || $placements[$id]['type'] !== 'header') { if (!isset($args['output'])) { $args['output'] = array(); } if (!isset($args['output']['class'])) { $args['output']['class'] = array(); } $class = $prefix . $id; if (!in_array($class, $args['output']['class'])) { $args['output']['class'][] = $class; } } // fix method id $_item[0] = Advanced_Ads_Select::AD; break; // avoid loops (programmatical error) // avoid loops (programmatical error) case Advanced_Ads_Select::PLACEMENT: return; case Advanced_Ads_Select::GROUP: default: } // add the placement to the global output array $advads = Advanced_Ads::get_instance(); $advads->current_ads[] = array('type' => 'placement', 'id' => $id, 'title' => $placements[$id]['name']); return Advanced_Ads_Select::get_instance()->get_ad_by_method((int) $_item[1], $_item[0], $args); } }