/** * Setup the singleton. */ public function setup() { /** * Filter the capability required to manage the layer priority settings. * * @param string $capability. Defaults to `manage_options`. */ $this->layer_priority_capability = apply_filters('ad_layers_layer_priority_capability', 'manage_options'); /** * Filter the capability required to manage the custom variables settings. * * @param string $capability. Defaults to `manage_options`. */ $this->custom_variables_capability = apply_filters('ad_layers_custom_variables_capability', 'manage_options'); // Register the settings pages if (function_exists('fm_register_submenu_page')) { if (current_user_can($this->layer_priority_capability)) { fm_register_submenu_page('ad_layers', Ad_Layers::instance()->get_edit_link(), __('Layer Priority', 'ad-layers'), null, $this->layer_priority_capability); } if (current_user_can($this->custom_variables_capability)) { fm_register_submenu_page('ad_layers_custom_variables', Ad_Layers::instance()->get_edit_link(), __('Custom Variables', 'ad-layers'), null, $this->custom_variables_capability); } } // Hook the ad layer settings pages onto Fieldmanager's actions add_action('fm_submenu_ad_layers', array($this, 'add_layer_priority_settings')); add_action('fm_submenu_ad_layers_custom_variables', array($this, 'add_custom_variables_settings')); // Load admin-only JS and CSS add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts')); }
/** * Add the ad layer priority management page. * * @access public */ public function add_settings_pages() { if (!class_exists('Fieldmanager_Field')) { return; } $fm_priority = new Fieldmanager_Group(array('name' => 'ad_layers', 'sortable' => true, 'collapsible' => true, 'collapsed' => true, 'limit' => 0, 'extra_elements' => 0, 'label' => __('Ad Layer', 'ad-layers'), 'label_macro' => array(__('%s', 'ad-layers'), 'title'), 'children' => array('post_id' => new Fieldmanager_Hidden(), 'title' => new Fieldmanager_Textfield(array('label' => __('Title', 'ad-layers'), 'attributes' => array('readonly' => 'readonly')))))); $fm_priority->add_submenu_page(Ad_Layers::instance()->get_edit_link(), __('Layer Priority', 'ad-layers')); $fm_custom = new Fieldmanager_Textfield(array('name' => 'ad_layers_custom_variables', 'limit' => 0, 'extra_elements' => 0, 'one_label_per_item' => false, 'label' => __('Add one or more custom variables for targeting.', 'ad-layers'), 'add_more_label' => __('Add a custom variable', 'ad-layers'))); $fm_custom->add_submenu_page(Ad_Layers::instance()->get_edit_link(), __('Custom Variables', 'ad-layers')); }
public function setUp() { parent::setUp(); $this->ad_server_parent = Ad_Layers_Ad_Server::instance(); $this->ad_server_settings = array('ad_server' => 'Ad_Layers_DFP', 'account_id' => '6355419', 'path_templates' => array(array('path_template' => '/#account_id#/#ad_unit#/front', 'page_type' => 'default')), 'breakpoints' => array(), 'ad_units' => array(array('code' => 'sidebar', 'sizes' => array(array('width' => 300, 'height' => 250, 'default_size' => 'default'), array('width' => 300, 'height' => 600))))); update_option($this->ad_server_parent->option_name, $this->ad_server_settings); // Add an ad layer $this->ad_layer = $this->factory->post->create(array('post_type' => 'ad-layer')); add_post_meta($this->ad_layer, 'ad_layer_ad_units', array(array('ad_unit' => 'sidebar'))); // add_post_meta( $this->ad_layer, 'ad_layer_page_types', array() ); Ad_Layers::instance()->setup(); $this->ad_server_parent->setup(); $this->ad_server = $this->ad_server_parent->get_ad_server(); }
protected function get_active_ad_layer() { $layers = get_option('ad_layers', array()); usort($layers, function ($a, $b) { if ($a['post_id'] == $b['post_id']) { return 0; } return $a['post_id'] < $b['post_id'] ? 1 : -1; }); update_option('ad_layers', $layers); Ad_Layers::instance()->setup(); Ad_Layers::instance()->set_active_ad_layer(); $layer = Ad_Layers::instance()->ad_layer; return !empty($layer['post_id']) ? $layer['post_id'] : false; }
$page_types = array_merge($page_types, array('author' => __('Author Archive', 'ad-layers'), 'date' => __('Date Archive', 'ad-layers'), 'notfound' => __('404 Page', 'ad-layers'), 'search' => __('Search Results', 'ad-layers'), 'default' => __('Default', 'ad-layers'))); $this->page_types = $page_types; } return apply_filters('ad_layers_page_types', $this->page_types); } /** * Get the current page type. * @access public * @return string */ public function get_current_page_type() { // Get the current page types $page_types = $this->get_page_types(); // Iterate for a match $page_type = ''; foreach ($page_types as $key => $label) { if (function_exists('is_' . $key) && true === call_user_func('is_' . $key) || 'post_tag' == $key && is_tag() || 'notfound' == $key && is_404() || 'archive::' === substr($key, 0, 9) && is_post_type_archive(substr($key, 9)) || post_type_exists($key) && is_singular($key) || taxonomy_exists($key) && is_tax($key)) { $page_type = $key; break; } } // Use default if no match if (empty($page_type)) { $page_type = 'default'; } return apply_filters('ad_layers_current_page_type', $page_type); } } Ad_Layers::instance(); }
public function test_cpt_with_archive_page_types() { $post_type = rand_str(20); register_post_type($post_type, array('public' => true, 'has_archive' => true)); $cpt_id = $this->factory->post->create(array('post_title' => 'hello-cpt', 'post_type' => $post_type)); Ad_Layers::instance()->page_types = null; // Singular view $this->go_to(get_permalink($cpt_id)); $this->assertTrue(is_singular($post_type)); $this->assertSame($post_type, Ad_Layers::instance()->get_current_page_type()); // Archive view $this->go_to(get_post_type_archive_link($post_type)); $this->assertTrue(is_post_type_archive($post_type)); $this->assertSame('archive::' . $post_type, Ad_Layers::instance()->get_current_page_type()); }
/** * Get the main details for all ads for use by the JS API. * * @return array */ protected function get_ad_details() { $return = array(); // Get the page type $page_type = Ad_Layers::instance()->get_current_page_type(); // Add the units foreach ($this->ad_units as $ad_unit => $custom_targeting) { // If no default size is defined, skip it if (empty($this->default_by_unit[$ad_unit])) { continue; } $return[$ad_unit] = array('path' => $this->get_path($page_type, $ad_unit), 'sizes' => $this->default_by_unit[$ad_unit], 'targeting' => array()); if (!empty($this->raw_targeting_by_unit[$ad_unit])) { $return[$ad_unit]['targeting'] = $this->get_targeting_array_from_custom_values($this->raw_targeting_by_unit[$ad_unit]); } } return $return; }
/** * Adds the meta boxes required to manage an ad layer. * * @param string|array $post_types * @param string $context * @param string $priority */ public function add_meta_boxes() { if (!class_exists('Fieldmanager_Field')) { return; } // Add ad units $ad_unit_args = array('name' => 'ad_layer_ad_units', 'limit' => 0, 'extra_elements' => 0, 'one_label_per_item' => false, 'sortable' => true, 'label' => __('Select one or more ad units.', 'ad-layers'), 'add_more_label' => __('Add an ad unit', 'ad-layers'), 'children' => array('ad_unit' => new Fieldmanager_Select(array('label' => __('Ad Unit', 'ad-layers'), 'options' => Ad_Layers_Ad_Server::instance()->get_ad_units())), 'do_not_render' => new Fieldmanager_Checkbox(__('Do not render the ad on load', 'ad-layers')))); $targeting_args = Ad_Layers_Ad_Server::instance()->get_custom_targeting_args('custom_targeting'); if (!empty($targeting_args)) { $ad_unit_args['children']['custom_targeting'] = new Fieldmanager_Group(apply_filters('ad_layers_custom_targeting_ad_unit_args', $targeting_args)); } $fm_ad_units = new Fieldmanager_Group(apply_filters('ad_layers_ad_units_field_args', $ad_unit_args)); $fm_ad_units->add_meta_box(__('Ad Units', 'ad-layers'), $this->post_type, 'normal', 'high'); // Add page types $fm_page_types = new Fieldmanager_Select(apply_filters('ad_layers_page_types_field_args', array('name' => 'ad_layer_page_types', 'limit' => 0, 'extra_elements' => 0, 'one_label_per_item' => false, 'label' => __('Select one or more page types to be targeted with this ad layer.', 'ad-layers'), 'add_more_label' => __('Add a page type', 'ad-layers'), 'options' => Ad_Layers::instance()->get_page_types()))); $fm_page_types->add_meta_box(__('Page Types', 'ad-layers'), $this->post_type, 'normal', 'high'); // Add taxonomies $fm_taxonomies = new Fieldmanager_Select(apply_filters('ad_layers_taxonomies_field_args', array('name' => 'ad_layer_taxonomies', 'limit' => 0, 'extra_elements' => 0, 'one_label_per_item' => false, 'label' => __('Select one or more optional taxonomies for targeting. Posts with any term in these taxonomies will get the ad layer.', 'ad-layers'), 'add_more_label' => __('Add a taxonomy', 'ad-layers'), 'options' => Ad_Layers::instance()->get_taxonomies()))); $fm_taxonomies->add_meta_box(__('Taxonomies', 'ad-layers'), $this->post_type, 'normal', 'high'); // Add post types $fm_post_types = new Fieldmanager_Select(apply_filters('ad_layers_post_types_field_args', array('name' => 'ad_layer_post_types', 'limit' => 0, 'extra_elements' => 0, 'one_label_per_item' => false, 'label' => __('Select one or more optional post types for targeting. Any post of this type will get the ad layer.', 'ad-layers'), 'add_more_label' => __('Add a post type', 'ad-layers'), 'options' => Ad_Layers::instance()->get_post_types()))); $fm_post_types->add_meta_box(__('Post Types', 'ad-layers'), $this->post_type, 'normal', 'high'); // Custom targeting variables $targeting_args = Ad_Layers_Ad_Server::instance()->get_custom_targeting_args(); if (!empty($targeting_args)) { $fm_custom = new Fieldmanager_Group(apply_filters('ad_layers_custom_targeting_field_args', $targeting_args)); $fm_custom->add_meta_box(__('Page Level Custom Targeting', 'ad-layers'), $this->post_type, 'normal', 'low'); } }
/** * Gets all available custom targeting sources. * * @access private * @return array */ private function get_custom_targeting_sources() { $options = array(); // Add all taxonomies available to ad layers $options = array_merge($options, Ad_Layers::instance()->get_taxonomies()); // Add additional options $options = array_merge($options, array('post_type' => __('Post Type', 'ad-layers'), 'author' => __('Author', 'ad-layers'), 'other' => __('Other', 'ad-layers'))); return apply_filters('ad_layers_custom_targeting_sources', $options); }