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); }
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); }
/** * 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(); }
private function __construct() { $this->plugin = Advanced_Ads_Plugin::get_instance(); $this->plugin->set_model($this->get_model()); $this->ad_selector = Advanced_Ads_Select::get_instance(); // initialize plugin specific functions add_action('init', array($this, 'wp_init')); // only when not doing ajax if (defined('DOING_AJAX') && DOING_AJAX) { Advanced_Ads_Ajax::get_instance(); } add_action('plugins_loaded', array($this, 'wp_plugins_loaded')); }
/** * 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; }
/** * 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(); }
/** * 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); }
/** * 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); } }