class-advanced-ads.php
719 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Advanced Ads. |
| 5 | * |
| 6 | * @package Advanced_Ads_Admin |
| 7 | * @author Thomas Maier <thomas.maier@webgilde.com> |
| 8 | * @license GPL-2.0+ |
| 9 | * @link http://webgilde.com |
| 10 | * @copyright 2013-2015 Thomas Maier, webgilde GmbH |
| 11 | */ |
| 12 | |
| 13 | /** |
| 14 | * Plugin class. This class should ideally be used to work with the |
| 15 | * public-facing side of the WordPress site. |
| 16 | * |
| 17 | * @package Advanced_Ads |
| 18 | * @author Thomas Maier <thomas.maier@webgilde.com> |
| 19 | */ |
| 20 | class Advanced_Ads { |
| 21 | |
| 22 | /** |
| 23 | * post type slug |
| 24 | * |
| 25 | * @since 1.0.0 |
| 26 | * @var string |
| 27 | */ |
| 28 | const POST_TYPE_SLUG = 'advanced_ads'; |
| 29 | |
| 30 | /** |
| 31 | * ad group slug |
| 32 | * |
| 33 | * @since 1.0.0 |
| 34 | * @var string |
| 35 | */ |
| 36 | const AD_GROUP_TAXONOMY = 'advanced_ads_groups'; |
| 37 | |
| 38 | /** |
| 39 | * Instance of this class. |
| 40 | * |
| 41 | * @since 1.0.0 |
| 42 | * @var object |
| 43 | */ |
| 44 | private static $instance = null; |
| 45 | |
| 46 | /** |
| 47 | * array with ads currently delivered in the frontend |
| 48 | */ |
| 49 | public $current_ads = array(); |
| 50 | |
| 51 | /** |
| 52 | * ad types |
| 53 | */ |
| 54 | public $ad_types = array(); |
| 55 | |
| 56 | /** |
| 57 | * plugin options |
| 58 | * |
| 59 | * @since 1.0.1 |
| 60 | * @var array (if loaded) |
| 61 | */ |
| 62 | protected $options = false; |
| 63 | |
| 64 | /** |
| 65 | * interal plugin options – set by the plugin |
| 66 | * |
| 67 | * @since 1.4.5 |
| 68 | * @var array (if loaded) |
| 69 | */ |
| 70 | protected $internal_options = false; |
| 71 | |
| 72 | /** |
| 73 | * list of bots and crawlers to exclude from ad impressions |
| 74 | * |
| 75 | * @since 1.4.9 |
| 76 | * @var array list of bots |
| 77 | */ |
| 78 | protected $bots = array('008','ABACHOBot','Accoona-AI-Agent','AddSugarSpiderBot','ADmantX','AhrefsBot','alexa','AnyApexBot','appie','Apple-PubSub','Arachmo','Ask Jeeves','avira.com','B-l-i-t-z-B-O-T','Baiduspider','BecomeBot','BeslistBot','BillyBobBot','Bimbot','Bingbot','BLEXBot','BlitzBOT','boitho.com-dc','boitho.com-robot','bot','btbot','CatchBot','Cerberian Drtrs','Charlotte','ConveraCrawler','cosmos','Covario IDS','crawler','CrystalSemanticsBot','curl','DataparkSearch','DiamondBot','Discobot','Dotbot','EmeraldShield.com WebBot','envolk[ITS]spider','EsperanzaBot','Exabot','expo9','facebookexternalhit','FAST Enterprise Crawler','FAST-WebCrawler','FDSE robot','Feedfetcher-Google','FindLinks','Firefly','froogle','FurlBot','FyberSpider','g2crawler','Gaisbot','GalaxyBot','genieBot','Genieo','Gigabot','Girafabot','Googlebot','Googlebot-Image','GrapeshotCrawler','GurujiBot','HappyFunBot','heritrix','hl_ftien_spider','Holmes','htdig','https://developers.google.com','ia_archiver','iaskspider','iCCrawler','ichiro','igdeSpyder','InfoSeek','inktomi','IRLbot','IssueCrawler','Jaxified Bot','Jyxobot','KoepaBot','Kraken','L.webis','LapozzBot','Larbin','LDSpider','LexxeBot','Linguee','Bot','LinkWalker','lmspider','looksmart','lwp-trivial','mabontland','magpie-crawler','Mail.RU_Bot','MaxPointCrawler','Mediapartners-Google','MJ12bot','Mnogosearch','mogimogi','MojeekBot','Moreoverbot','Morning Paper','msnbot','MSRBot','MVAClient','mxbot','NationalDirectory','NetResearchServer','NetSeer Crawler','NewsGator','NG-Search','nicebot','noxtrumbot','Nusearch','Spider','Nutch crawler','NutchCVS','Nymesis','obot','oegp','omgilibot','OmniExplorer_Bot','OOZBOT','Orbiter','PageBitesHyperBot','Peew','polybot','Pompos','PostPost','proximic','Psbot','PycURL','Qseero','rabaz','Radian6','RAMPyBot','Rankivabot','RufusBot','SandCrawler','savetheworldheritage','SBIder','Scooter','ScoutJet','Scrubby','SearchSight','Seekbot','semanticdiscovery','Sensis','Web Crawler','SEOChat::Bot','SeznamBot','Shim-Crawler','ShopWiki','Shoula robot','silk','Sitebot','Snappy','sogou spider','Sogou web spider','Sosospider','Spade','Speedy Spider','Sqworm','StackRambler','suggybot','SurveyBot','SynooBot','TechnoratiSnoop','TECNOSEEK','Teoma','TerrawizBot','TheSuBot','Thumbnail.CZ','robot','TinEye','truwoGPS','TurnitinBot','TweetedTimes Bot','TwengaBot','updated','URL_Spider_SQL','Urlfilebot','Vagabondo','VoilaBot','voltron','Vortex','voyager','VYU2','WebAlta Crawler','WebBug','webcollage','WebFindBot','WebIndex','Websquash.com','WeSEE:Ads','wf84','Wget','WoFindeIch Robot','WomlpeFactory','WordPress','Xaldon_WebSpider','yacy','Yahoo! Slurp','Yahoo! Slurp China','YahooSeeker','YahooSeeker-Testing','YandexBot','YandexImages','Yasaklibot','Yeti','YodaoBot','yoogliFetchAgent','YoudaoBot','Zao','Zealbot','zspider','ZyBorg'); |
| 79 | |
| 80 | /** |
| 81 | * |
| 82 | * @var Advanced_Ads_Model |
| 83 | */ |
| 84 | protected $model; |
| 85 | |
| 86 | /** |
| 87 | * |
| 88 | * @var Advanced_Ads_Plugin |
| 89 | */ |
| 90 | protected $plugin; |
| 91 | |
| 92 | /** |
| 93 | * |
| 94 | * @var Advanced_Ads_Select |
| 95 | */ |
| 96 | protected $ad_selector; |
| 97 | |
| 98 | /** |
| 99 | * is the query the main query?, when WP_Query is used |
| 100 | * |
| 101 | * @var bool |
| 102 | */ |
| 103 | private $is_main_query; |
| 104 | |
| 105 | |
| 106 | private function __construct() { |
| 107 | $this->plugin = Advanced_Ads_Plugin::get_instance(); |
| 108 | $this->plugin->set_model($this->get_model()); |
| 109 | $this->ad_selector = Advanced_Ads_Select::get_instance(); |
| 110 | |
| 111 | // initialize plugin specific functions |
| 112 | add_action( 'init', array( $this, 'wp_init' ) ); |
| 113 | |
| 114 | // only when not doing ajax |
| 115 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 116 | Advanced_Ads_Ajax::get_instance(); |
| 117 | } |
| 118 | add_action( 'plugins_loaded', array( $this, 'wp_plugins_loaded' ) ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Return an instance of this class. |
| 123 | * |
| 124 | * @since 1.0.0 |
| 125 | * @return Advanced_Ads A single instance of this class. |
| 126 | */ |
| 127 | public static function get_instance() { |
| 128 | |
| 129 | // If the single instance hasn't been set, set it now. |
| 130 | if ( null === self::$instance ) { |
| 131 | self::$instance = new self; |
| 132 | } |
| 133 | |
| 134 | return self::$instance; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * |
| 139 | * @return Advanced_Ads_Model |
| 140 | */ |
| 141 | public function get_model() { |
| 142 | |
| 143 | global $wpdb; |
| 144 | |
| 145 | if ( ! isset($this->model) ) { |
| 146 | $this->model = new Advanced_Ads_Model( $wpdb ); |
| 147 | } |
| 148 | |
| 149 | return $this->model; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Initialize the plugin by setting localization and loading public scripts |
| 154 | * and styles. |
| 155 | * |
| 156 | * @since 1.0.0 |
| 157 | */ |
| 158 | public function wp_plugins_loaded() |
| 159 | { |
| 160 | // register hook for global constants |
| 161 | add_action( 'wp', array( $this, 'set_disabled_constant' ) ); |
| 162 | |
| 163 | // setup default ad types |
| 164 | add_filter( 'advanced-ads-ad-types', array( $this, 'setup_default_ad_types' ), 5 ); |
| 165 | |
| 166 | // register hooks and filters for auto ad injection |
| 167 | $this->init_injection(); |
| 168 | |
| 169 | // allow add-ons to hook |
| 170 | do_action( 'advanced-ads-plugin-loaded' ); |
| 171 | |
| 172 | // manipulate sidebar widget |
| 173 | add_filter( 'dynamic_sidebar_params', array( $this, 'manipulate_widget_output' ) ); |
| 174 | |
| 175 | // add meta robots noindex, nofollow to images, which are part of 'Image ad' ad type |
| 176 | add_action( 'wp_head', array( $this, 'noindex_attachment_images' ) ); |
| 177 | |
| 178 | // check if ads are disabled in secondary queries |
| 179 | add_action( 'the_post', array( $this, 'set_query_type' ), 10, 2 ); |
| 180 | |
| 181 | |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * init / load plugin specific functions and settings |
| 186 | * |
| 187 | * @since 1.0.0 |
| 188 | */ |
| 189 | public function wp_init(){ |
| 190 | // load ad post types |
| 191 | $this->create_post_types(); |
| 192 | // set ad types array |
| 193 | $this->set_ad_types(); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * define ad types with their options |
| 198 | * |
| 199 | * name => publically readable name |
| 200 | * description => publically readable description |
| 201 | * editor => kind of editor: text (normal text field), content (WP content field), none (no content field) |
| 202 | * will display text field, if left empty |
| 203 | * |
| 204 | * @since 1.0.0 |
| 205 | */ |
| 206 | public function set_ad_types() { |
| 207 | |
| 208 | /** |
| 209 | * load default ad type files |
| 210 | * custom ad types can also be loaded in your own plugin or functions.php |
| 211 | */ |
| 212 | $types = array(); |
| 213 | |
| 214 | /** |
| 215 | * developers can add new ad types using this filter |
| 216 | * see classes/ad-type-content.php for an example for an ad type and usage of this filter |
| 217 | */ |
| 218 | $this->ad_types = apply_filters( 'advanced-ads-ad-types', $types ); |
| 219 | } |
| 220 | |
| 221 | public function init_injection() { |
| 222 | // -TODO abstract |
| 223 | add_action( 'wp_head', array( $this, 'inject_header' ), 20 ); |
| 224 | add_action( 'wp_footer', array( $this, 'inject_footer' ), 20 ); |
| 225 | add_filter( 'the_content', array( $this, 'inject_content' ), $this->plugin->get_content_injection_priority() ); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * set global constant that prevents ads from being displayed on the current page view |
| 230 | * |
| 231 | * @since 1.3.10 |
| 232 | */ |
| 233 | public function set_disabled_constant(){ |
| 234 | |
| 235 | global $post, $wp_the_query; |
| 236 | |
| 237 | // don't set the constant if already defined |
| 238 | if ( defined( 'ADVADS_ADS_DISABLED' ) ) { return; } |
| 239 | |
| 240 | $options = $this->plugin->options(); |
| 241 | |
| 242 | // check if ads are disabled completely |
| 243 | if ( ! empty($options['disabled-ads']['all']) ){ |
| 244 | define( 'ADVADS_ADS_DISABLED', true ); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | // check if ads are disabled from 404 pages |
| 249 | if ( $wp_the_query->is_404() && ! empty($options['disabled-ads']['404']) ){ |
| 250 | define( 'ADVADS_ADS_DISABLED', true ); |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | // check if ads are disabled from non singular pages (often = archives) |
| 255 | if ( ! $wp_the_query->is_singular() && ! empty($options['disabled-ads']['archives']) ){ |
| 256 | define( 'ADVADS_ADS_DISABLED', true ); |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | // check if ads are disabled in Feed |
| 261 | if ( $wp_the_query->is_feed() && ( ! isset( $options['disabled-ads']['feed'] ) || $options['disabled-ads']['feed'] ) ) { |
| 262 | define( 'ADVADS_ADS_DISABLED', true ); |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | // check if ads are disabled on the current page |
| 267 | if ( $wp_the_query->is_singular() && isset($post->ID) ){ |
| 268 | $post_ad_options = get_post_meta( $post->ID, '_advads_ad_settings', true ); |
| 269 | |
| 270 | if ( ! empty($post_ad_options['disable_ads']) ){ |
| 271 | define( 'ADVADS_ADS_DISABLED', true ); |
| 272 | } |
| 273 | }; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Return the plugin slug. |
| 278 | * |
| 279 | * @since 1.0.0 |
| 280 | * @return Plugin slug variable. |
| 281 | */ |
| 282 | public function get_plugin_slug() { |
| 283 | return $this->plugin->get_plugin_slug(); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * add plain and content ad types to the default ads of the plugin using a filter |
| 288 | * |
| 289 | * @since 1.0.0 |
| 290 | * |
| 291 | */ |
| 292 | function setup_default_ad_types($types){ |
| 293 | $types['plain'] = new Advanced_Ads_Ad_Type_Plain(); /* plain text and php code */ |
| 294 | $types['content'] = new Advanced_Ads_Ad_Type_Content(); /* rich content editor */ |
| 295 | $types['image'] = new Advanced_Ads_Ad_Type_Image(); /* image ads */ |
| 296 | $types['group'] = new Advanced_Ads_Ad_Type_Group(); /* group ad */ |
| 297 | return $types; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * log error messages when debug is enabled |
| 302 | * |
| 303 | * @since 1.0.0 |
| 304 | * @link http://www.smashingmagazine.com/2011/03/08/ten-things-every-wordpress-plugin-developer-should-know/ |
| 305 | */ |
| 306 | static function log($message) { |
| 307 | if ( true === WP_DEBUG ) { |
| 308 | if ( is_array( $message ) || is_object( $message ) ) { |
| 309 | error_log( __('Advanced Ads Error following:', 'advanced-ads' ) ); |
| 310 | error_log( print_r( $message, true ) ); |
| 311 | } else { |
| 312 | $message = sprintf( __( 'Advanced Ads Error: %s', 'advanced-ads' ), $message ); |
| 313 | error_log( $message ); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | // compat method |
| 319 | public function options() { |
| 320 | return $this->plugin->options(); |
| 321 | } |
| 322 | |
| 323 | // compat method |
| 324 | public function internal_options() { |
| 325 | return $this->plugin->internal_options(); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * injected ad into header |
| 330 | * |
| 331 | * @since 1.1.0 |
| 332 | */ |
| 333 | public function inject_header(){ |
| 334 | $placements = get_option( 'advads-ads-placements', array() ); |
| 335 | foreach ( $placements as $_placement_id => $_placement ){ |
| 336 | if ( isset($_placement['type']) && 'header' == $_placement['type'] ){ |
| 337 | $_options = isset( $_placement['options'] ) ? $_placement['options'] : array(); |
| 338 | echo Advanced_Ads_Select::get_instance()->get_ad_by_method( $_placement_id, Advanced_Ads_Select::PLACEMENT, $_options ); |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * injected ads into footer |
| 345 | * |
| 346 | * @since 1.1.0 |
| 347 | */ |
| 348 | public function inject_footer(){ |
| 349 | $placements = get_option( 'advads-ads-placements', array() ); |
| 350 | foreach ( $placements as $_placement_id => $_placement ){ |
| 351 | if ( isset($_placement['type']) && 'footer' == $_placement['type'] ){ |
| 352 | $_options = isset( $_placement['options'] ) ? $_placement['options'] : array(); |
| 353 | echo Advanced_Ads_Select::get_instance()->get_ad_by_method( $_placement_id, Advanced_Ads_Select::PLACEMENT, $_options ); |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * injected ad into content (before and after) |
| 360 | * displays ALL ads |
| 361 | * |
| 362 | * @since 1.1.0 |
| 363 | * @param str $content post content |
| 364 | */ |
| 365 | public function inject_content($content = ''){ |
| 366 | $options = $this->plugin->options(); |
| 367 | |
| 368 | // check if ads are disabled in secondary queries and this function was called by ajax (in secondary query) |
| 369 | if ( ! empty( $options['disabled-ads']['secondary'] ) && ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
| 370 | return $content; |
| 371 | } |
| 372 | |
| 373 | // run only within the loop on single pages of public post types |
| 374 | $public_post_types = get_post_types( array( 'public' => true, 'publicly_queryable' => true ), 'names', 'or' ); |
| 375 | |
| 376 | // make sure that no ad is injected into another ad |
| 377 | if ( get_post_type() == self::POST_TYPE_SLUG ){ |
| 378 | return $content; |
| 379 | } |
| 380 | |
| 381 | // check if admin allows injection in all places |
| 382 | if( ! isset( $options['content-injection-everywhere'] ) ){ |
| 383 | // check if this is a singular page within the loop |
| 384 | if ( ( ! is_singular( $public_post_types ) && ! is_feed() ) || ! in_the_loop() ) { return $content; } |
| 385 | } |
| 386 | |
| 387 | $placements = get_option( 'advads-ads-placements', array() ); |
| 388 | |
| 389 | if( ! apply_filters( 'advanced-ads-can-inject-into-content', true, $content, $placements )){ |
| 390 | return $content; |
| 391 | } |
| 392 | |
| 393 | foreach ( $placements as $_placement_id => $_placement ){ |
| 394 | if ( empty($_placement['item']) || ! isset($_placement['type']) ) { continue; } |
| 395 | $_options = isset( $_placement['options'] ) ? $_placement['options'] : array(); |
| 396 | |
| 397 | // check if injection is ok for a specific placement id |
| 398 | if( ! apply_filters( 'advanced-ads-can-inject-into-content-' . $_placement_id, true, $content, $_placement_id )){ |
| 399 | continue; |
| 400 | } |
| 401 | |
| 402 | switch ( $_placement['type'] ) { |
| 403 | case 'post_top': |
| 404 | // TODO broken: does not serve placement but serves ad directly |
| 405 | $content = Advanced_Ads_Select::get_instance()->get_ad_by_method( $_placement_id, Advanced_Ads_Select::PLACEMENT, $_options ) . $content; |
| 406 | break; |
| 407 | case 'post_bottom': |
| 408 | $content .= Advanced_Ads_Select::get_instance()->get_ad_by_method( $_placement_id, Advanced_Ads_Select::PLACEMENT, $_options ); |
| 409 | break; |
| 410 | case 'post_content': |
| 411 | $content = Advanced_Ads_Placements::inject_in_content( $_placement_id, $_options, $content ); |
| 412 | break; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | return $content; |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * load all ads based on WP_Query conditions |
| 421 | * |
| 422 | * @deprecated 1.4.8 use model class |
| 423 | * @since 1.1.0 |
| 424 | * @param arr $args WP_Query arguments that are more specific that default |
| 425 | * @return arr $ads array with post objects |
| 426 | */ |
| 427 | static function get_ads($args = array()){ |
| 428 | return self::get_instance()->get_model()->get_ads($args); |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * load all ad groups |
| 433 | * |
| 434 | * @deprecated 1.4.8 use model class |
| 435 | * @since 1.1.0 |
| 436 | * @param arr $args array with options |
| 437 | * @return arr $groups array with ad groups |
| 438 | * @link http://codex.wordpress.org/Function_Reference/get_terms |
| 439 | */ |
| 440 | static function get_ad_groups($args = array()){ |
| 441 | return self::get_instance()->get_model()->get_ad_groups($args); |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * get the array with ad placements |
| 446 | * |
| 447 | * @since 1.1.0 |
| 448 | * @deprecated 1.4.8 use model |
| 449 | * @return arr $ad_placements |
| 450 | */ |
| 451 | static public function get_ad_placements_array(){ |
| 452 | return self::get_instance()->get_model()->get_ad_placements_array(); |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * |
| 457 | * @deprecated 1.4.8 use model |
| 458 | * @return array |
| 459 | */ |
| 460 | public static function get_ad_conditions() { |
| 461 | return self::get_instance()->get_model()->get_ad_conditions(); |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * general check if ads can be displayed for the whole page impression |
| 466 | * |
| 467 | * @since 1.4.9 |
| 468 | * @return bool true, if ads can be displayed |
| 469 | * @todo move this to set_disabled_constant() |
| 470 | */ |
| 471 | public function can_display_ads(){ |
| 472 | |
| 473 | // check global constant if ads are enabled or disabled |
| 474 | if ( defined( 'ADVADS_ADS_DISABLED' ) ) { |
| 475 | return false; |
| 476 | } |
| 477 | |
| 478 | $options = $this->options(); |
| 479 | |
| 480 | // check if ads are disabled in secondary queries |
| 481 | // and this is not main query and this is not ajax (because main query does not exist in ajax but ad needs to be shown) |
| 482 | if ( ! empty( $options['disabled-ads']['secondary'] ) && ! $this->is_main_query() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
| 483 | return false; |
| 484 | } |
| 485 | |
| 486 | $see_ads_capability = isset($options['hide-for-user-role']) && $options['hide-for-user-role'] != '' ? $options['hide-for-user-role'] : false; |
| 487 | |
| 488 | // check if user is logged in and if so if users with his rights can see ads |
| 489 | if ( $see_ads_capability && is_user_logged_in() && current_user_can( $see_ads_capability ) ) { |
| 490 | return false; |
| 491 | } |
| 492 | |
| 493 | // check bots if option is enabled |
| 494 | if( isset($options['block-bots']) && $options['block-bots'] && $this->is_bot() ) { |
| 495 | return false; |
| 496 | } |
| 497 | |
| 498 | return true; |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * check if the current user agent is given or a bot |
| 503 | * |
| 504 | * @since 1.4.9 |
| 505 | * @return bool true if the current user agent is empty or a bot |
| 506 | */ |
| 507 | public function is_bot(){ |
| 508 | $bots = apply_filters('advanced-ads-bots', $this->bots); |
| 509 | $bots = implode('|', $bots); |
| 510 | $bots = preg_replace('@[^-_;/|\][ a-z0-9]@i', '', $bots); |
| 511 | $regex = "@$bots@i"; |
| 512 | |
| 513 | if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT'] !== '') { |
| 514 | $agent = $_SERVER['HTTP_USER_AGENT']; |
| 515 | |
| 516 | return preg_match($regex, $agent) === 1; |
| 517 | } |
| 518 | |
| 519 | return true; |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Registers ad post type and group taxonomies |
| 524 | * |
| 525 | * @since 1.0.0 |
| 526 | */ |
| 527 | public function create_post_types() { |
| 528 | if ( 1 !== did_action( 'init' ) ) { |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | // register ad group taxonomy |
| 533 | if ( ! taxonomy_exists( Advanced_Ads::AD_GROUP_TAXONOMY ) ){ |
| 534 | $post_type_params = $this->get_group_taxonomy_params(); |
| 535 | register_taxonomy( Advanced_Ads::AD_GROUP_TAXONOMY, array( Advanced_Ads::POST_TYPE_SLUG ), $post_type_params ); |
| 536 | } |
| 537 | |
| 538 | // register ad post type |
| 539 | if ( ! post_type_exists( Advanced_Ads::POST_TYPE_SLUG ) ) { |
| 540 | $post_type_params = $this->get_post_type_params(); |
| 541 | register_post_type( Advanced_Ads::POST_TYPE_SLUG, $post_type_params ); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Defines the parameters for the ad post type taxonomy |
| 547 | * |
| 548 | * @since 1.0.0 |
| 549 | * @return array |
| 550 | */ |
| 551 | protected function get_group_taxonomy_params(){ |
| 552 | $labels = array( |
| 553 | 'name' => _x( 'Ad Groups', 'ad group general name', 'advanced-ads' ), |
| 554 | 'singular_name' => _x( 'Ad Group', 'ad group singular name', 'advanced-ads' ), |
| 555 | 'search_items' => __( 'Search Ad Groups', 'advanced-ads' ), |
| 556 | 'all_items' => __( 'All Ad Groups', 'advanced-ads' ), |
| 557 | 'parent_item' => __( 'Parent Ad Groups', 'advanced-ads' ), |
| 558 | 'parent_item_colon' => __( 'Parent Ad Groups:', 'advanced-ads' ), |
| 559 | 'edit_item' => __( 'Edit Ad Group', 'advanced-ads' ), |
| 560 | 'update_item' => __( 'Update Ad Group', 'advanced-ads' ), |
| 561 | 'add_new_item' => __( 'Add New Ad Group', 'advanced-ads' ), |
| 562 | 'new_item_name' => __( 'New Ad Groups Name', 'advanced-ads' ), |
| 563 | 'menu_name' => __( 'Groups', 'advanced-ads' ), |
| 564 | 'not_found' => __( 'No Ad Group found', 'advanced-ads' ), |
| 565 | ); |
| 566 | |
| 567 | $args = array( |
| 568 | 'public' => false, |
| 569 | 'hierarchical' => true, |
| 570 | 'labels' => $labels, |
| 571 | 'show_ui' => true, |
| 572 | 'show_in_nav_menus' => false, |
| 573 | 'show_tagcloud' => false, |
| 574 | 'show_admin_column' => true, |
| 575 | 'query_var' => false, |
| 576 | 'rewrite' => false, |
| 577 | ); |
| 578 | |
| 579 | return $args; |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * Defines the parameters for the custom post type |
| 584 | * |
| 585 | * @since 1.0.0 |
| 586 | * @return array |
| 587 | */ |
| 588 | protected function get_post_type_params() { |
| 589 | $labels = array( |
| 590 | 'name' => __( 'Ads', 'advanced-ads' ), |
| 591 | 'singular_name' => __( 'Ad', 'advanced-ads' ), |
| 592 | 'add_new' => __( 'New Ad', 'advanced-ads' ), |
| 593 | 'add_new_item' => __( 'Add New Ad', 'advanced-ads' ), |
| 594 | 'edit' => __( 'Edit', 'advanced-ads' ), |
| 595 | 'edit_item' => __( 'Edit Ad', 'advanced-ads' ), |
| 596 | 'new_item' => __( 'New Ad', 'advanced-ads' ), |
| 597 | 'view' => __( 'View', 'advanced-ads' ), |
| 598 | 'view_item' => __( 'View the Ad', 'advanced-ads' ), |
| 599 | 'search_items' => __( 'Search Ads', 'advanced-ads' ), |
| 600 | 'not_found' => __( 'No Ads found', 'advanced-ads' ), |
| 601 | 'not_found_in_trash' => __( 'No Ads found in Trash', 'advanced-ads' ), |
| 602 | 'parent' => __( 'Parent Ad', 'advanced-ads' ), |
| 603 | ); |
| 604 | |
| 605 | $post_type_params = array( |
| 606 | 'labels' => $labels, |
| 607 | 'public' => false, |
| 608 | 'show_ui' => true, |
| 609 | 'show_in_menu' => false, |
| 610 | 'hierarchical' => false, |
| 611 | 'capabilities' => array( |
| 612 | // Meta capabilities |
| 613 | 'edit_post' => 'advanced_ads_edit_ads', |
| 614 | 'read_post' => 'advanced_ads_edit_ads', |
| 615 | 'delete_post' => 'advanced_ads_edit_ads', |
| 616 | // Primitive capabilities used outside of map_meta_cap() |
| 617 | 'edit_posts' => 'advanced_ads_edit_ads', |
| 618 | 'edit_others_posts' => 'advanced_ads_edit_ads', |
| 619 | 'publish_posts' => 'advanced_ads_edit_ads', |
| 620 | 'read_private_posts' => 'advanced_ads_edit_ads', |
| 621 | // Primitive capabilities used within map_meta_cap(): |
| 622 | 'read' => 'advanced_ads_edit_ads', |
| 623 | 'delete_posts' => 'advanced_ads_edit_ads', |
| 624 | 'delete_private_posts' => 'advanced_ads_edit_ads', |
| 625 | 'delete_published_posts' => 'advanced_ads_edit_ads', |
| 626 | 'delete_others_posts' => 'advanced_ads_edit_ads', |
| 627 | 'edit_private_posts' => 'advanced_ads_edit_ads', |
| 628 | 'edit_published_posts' => 'advanced_ads_edit_ads', |
| 629 | 'create_posts' => 'advanced_ads_edit_ads', |
| 630 | ), |
| 631 | 'has_archive' => false, |
| 632 | 'rewrite' => array( 'slug' => ADVADS_SLUG ), |
| 633 | 'query_var' => true, |
| 634 | 'supports' => array( 'title' ), |
| 635 | 'taxonomies' => array( Advanced_Ads::AD_GROUP_TAXONOMY ) |
| 636 | ); |
| 637 | |
| 638 | return apply_filters( 'advanced-ads-post-type-params', $post_type_params ); |
| 639 | } |
| 640 | |
| 641 | /** |
| 642 | * manipulate output of ad widget |
| 643 | * |
| 644 | * @since 1.6.8.2 |
| 645 | * @param arr $params widget and sidebar params |
| 646 | */ |
| 647 | public function manipulate_widget_output( $params = array() ){ |
| 648 | |
| 649 | if( $params[0]['widget_name'] === 'Advanced Ads' ){ |
| 650 | |
| 651 | $options = $this->plugin->options(); |
| 652 | // hide id by default (when options are empty) or when option is enabled |
| 653 | if( $options === array() || ( isset( $options['remove-widget-id'] ) && $options['remove-widget-id'] ) ){ |
| 654 | $pattern = '#\s(id)=("|\')[^"^\']+("|\')#'; |
| 655 | $params[0]['before_widget'] = preg_replace( $pattern, '', $params[0]['before_widget']); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | return $params; |
| 660 | } |
| 661 | |
| 662 | /** |
| 663 | * Add meta robots noindex, nofollow to images, which are part of 'Image ad' ad type |
| 664 | */ |
| 665 | function noindex_attachment_images() { |
| 666 | global $post; |
| 667 | // if the image was not attached to any post |
| 668 | if ( is_attachment() && is_object( $post ) && isset( $post->post_parent ) && $post->post_parent === 0 ) { |
| 669 | // if at least one ad contains the image |
| 670 | if ( get_post_meta( get_the_ID(), '_advanced-ads_parent_id', true ) > 0 ) { |
| 671 | echo '<meta name="robots" content="noindex, nofollow" />'; |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | /** |
| 677 | * Supports the "$this->is_main_query=true" while main query is being executed |
| 678 | * |
| 679 | * @param WP_Post &$post The Post object (passed by reference). |
| 680 | * @param WP_Query &$this The current Query object (passed by reference). |
| 681 | */ |
| 682 | function set_query_type( $post, $query = null ) { |
| 683 | if ( $query instanceof WP_Query ) { |
| 684 | $this->is_main_query = $query->is_main_query(); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * Check if main query is being executed |
| 690 | * |
| 691 | * @return bool true while main query is being executed or not in the loop, false otherwise |
| 692 | */ |
| 693 | public function is_main_query() { |
| 694 | if ( ! in_the_loop() ) { |
| 695 | // the secondary query check only designed for within post content |
| 696 | return true; |
| 697 | } |
| 698 | |
| 699 | return $this->is_main_query === true; |
| 700 | } |
| 701 | |
| 702 | /** |
| 703 | * get an "Advertisement" label to use before single ad or before first ad in a group |
| 704 | * |
| 705 | * @return string label, bool false if label should not be displayed |
| 706 | */ |
| 707 | public function get_label() { |
| 708 | $advads_options = Advanced_Ads::get_instance()->options(); |
| 709 | |
| 710 | if ( isset( $advads_options['custom-label']['enabled'] ) ) { |
| 711 | $label = ! empty( $advads_options['custom-label']['text'] ) ? esc_html( $advads_options['custom-label']['text'] ) : _x( 'Advertisements', 'label before ads' ); |
| 712 | |
| 713 | $template = sprintf( '<div class="%s">%s</div>', Advanced_Ads_Plugin::get_instance()->get_frontend_prefix() . 'adlabel', $label ); |
| 714 | return apply_filters( 'advanced-ads-custom-label', $template, $label ); |
| 715 | } |
| 716 | return false; |
| 717 | } |
| 718 | } |
| 719 |