EDD_SL_Plugin_Updater.php
10 years ago
ad-ajax.php
10 years ago
ad-model.php
11 years ago
ad-select.php
10 years ago
ad.php
10 years ago
ad_ajax_callbacks.php
10 years ago
ad_group.php
10 years ago
ad_placements.php
10 years ago
ad_type_abstract.php
11 years ago
ad_type_content.php
10 years ago
ad_type_image.php
10 years ago
ad_type_plain.php
10 years ago
checks.php
10 years ago
display-conditions.php
10 years ago
plugin.php
10 years ago
upgrades.php
10 years ago
visitor-conditions.php
10 years ago
widget.php
10 years ago
plugin.php
481 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Wordpress integration and definitions: |
| 5 | * |
| 6 | * - posttypes |
| 7 | * - taxonomy |
| 8 | * - textdomain |
| 9 | * |
| 10 | * @since 1.5.0 |
| 11 | */ |
| 12 | class Advanced_Ads_Plugin { |
| 13 | /** |
| 14 | * |
| 15 | * @var Advanced_Ads_Plugin |
| 16 | */ |
| 17 | protected static $instance; |
| 18 | |
| 19 | /** |
| 20 | * |
| 21 | * @var Advanced_Ads_Model |
| 22 | */ |
| 23 | protected $model; |
| 24 | |
| 25 | /** |
| 26 | * plugin options |
| 27 | * |
| 28 | * @since 1.0.1 |
| 29 | * @var array (if loaded) |
| 30 | */ |
| 31 | protected $options; |
| 32 | |
| 33 | /** |
| 34 | * interal plugin options – set by the plugin |
| 35 | * |
| 36 | * @since 1.4.5 |
| 37 | * @var array (if loaded) |
| 38 | */ |
| 39 | protected $internal_options; |
| 40 | |
| 41 | /** |
| 42 | * default prefix of selectors (id, class) in the frontend |
| 43 | * can be changed by options |
| 44 | * |
| 45 | * @var Advanced_Ads_Plugin |
| 46 | */ |
| 47 | const DEFAULT_FRONTEND_PREFIX = 'advads-'; |
| 48 | |
| 49 | /** |
| 50 | * |
| 51 | * @var frontend prefix for classes and IDs |
| 52 | */ |
| 53 | private $frontend_prefix; |
| 54 | |
| 55 | |
| 56 | private function __construct() { |
| 57 | register_activation_hook( ADVADS_BASE, array( $this, 'activate' ) ); |
| 58 | register_deactivation_hook( ADVADS_BASE, array( $this, 'deactivate' ) ); |
| 59 | |
| 60 | add_action( 'plugins_loaded', array( $this, 'wp_plugins_loaded' ) ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * |
| 65 | * @return Advanced_Ads_Plugin |
| 66 | */ |
| 67 | public static function get_instance() { |
| 68 | // If the single instance hasn't been set, set it now. |
| 69 | if ( null === self::$instance ) { |
| 70 | self::$instance = new self; |
| 71 | } |
| 72 | |
| 73 | return self::$instance; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * |
| 78 | * @param Advanced_Ads_Model $model |
| 79 | */ |
| 80 | public function set_model(Advanced_Ads_Model $model) { |
| 81 | $this->model = $model; |
| 82 | } |
| 83 | |
| 84 | public function wp_plugins_loaded() { |
| 85 | // Load plugin text domain |
| 86 | $this->load_plugin_textdomain(); |
| 87 | |
| 88 | $internal_options = $this->internal_options(); |
| 89 | |
| 90 | /** |
| 91 | * run upgrades, if this is a new version |
| 92 | */ |
| 93 | if ( !defined( 'DOING_AJAX' ) && isset( $internal_options['version'] ) && version_compare( $internal_options['version'], ADVADS_VERSION, '<' ) ) { |
| 94 | new Advanced_Ads_Upgrades(); |
| 95 | } |
| 96 | |
| 97 | // activate plugin when new blog is added on multisites // -TODO this is admin-only |
| 98 | add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) ); |
| 99 | |
| 100 | // Load public-facing style sheet and JavaScript. |
| 101 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); |
| 102 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 103 | |
| 104 | // add short codes |
| 105 | add_shortcode( 'the_ad', array( $this, 'shortcode_display_ad' ) ); |
| 106 | add_shortcode( 'the_ad_group', array( $this, 'shortcode_display_ad_group' ) ); |
| 107 | add_shortcode( 'the_ad_placement', array( $this, 'shortcode_display_ad_placement' ) ); |
| 108 | |
| 109 | // remove default ad group menu item // -TODO only for admin |
| 110 | add_action( 'admin_menu', array( $this, 'remove_taxonomy_menu_item' ) ); |
| 111 | // load widgets |
| 112 | add_action( 'widgets_init', array( $this, 'widget_init' ) ); |
| 113 | |
| 114 | // load display conditions |
| 115 | Advanced_Ads_Display_Conditions::get_instance(); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Register and enqueue public-facing style sheet. |
| 120 | * |
| 121 | * @since 1.0.0 |
| 122 | */ |
| 123 | public function enqueue_styles() { |
| 124 | // wp_enqueue_style( $this->get_plugin_slug() . '-plugin-styles', plugins_url('assets/css/public.css', __FILE__), array(), ADVADS_VERSION); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Return the plugin slug. |
| 129 | * |
| 130 | * @since 1.0.0 |
| 131 | * @return Plugin slug variable. |
| 132 | */ |
| 133 | public function get_plugin_slug() { |
| 134 | return ADVADS_SLUG; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Register and enqueues public-facing JavaScript files. |
| 139 | * |
| 140 | * @since 1.0.0 |
| 141 | */ |
| 142 | public function enqueue_scripts() { |
| 143 | // wp_enqueue_script( $this->get_plugin_slug() . '-plugin-script', plugins_url('assets/js/public.js', __FILE__), array('jquery'), ADVADS_VERSION); |
| 144 | $options = $this->options(); |
| 145 | $activated_js = apply_filters( 'advanced-ads-activate-advanced-js', isset( $options['advanced-js'] ) ); |
| 146 | if ( $activated_js ){ |
| 147 | wp_enqueue_script( $this->get_plugin_slug() . '-advanced-js', ADVADS_BASE_URL . 'public/assets/js/advanced.js', array( 'jquery' ), ADVADS_VERSION ); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | public function widget_init() { |
| 152 | register_widget( 'Advanced_Ads_Widget' ); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Fired when a new site is activated with a WPMU environment. |
| 157 | * |
| 158 | * @since 1.0.0 |
| 159 | * @param int $blog_id ID of the new blog. |
| 160 | */ |
| 161 | public function activate_new_site($blog_id) { |
| 162 | |
| 163 | if ( 1 !== did_action( 'wpmu_new_blog' ) ) { |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | switch_to_blog( $blog_id ); |
| 168 | $this->single_activate(); |
| 169 | restore_current_blog(); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Fired for each blog when the plugin is activated. |
| 174 | * |
| 175 | * @since 1.0.0 |
| 176 | */ |
| 177 | protected function single_activate() { |
| 178 | $this->post_types_rewrite_flush(); |
| 179 | // -TODO inform modules |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Fired for each blog when the plugin is deactivated. |
| 184 | * |
| 185 | * @since 1.0.0 |
| 186 | */ |
| 187 | protected function single_deactivate() { |
| 188 | // -TODO inform modules |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Load the plugin text domain for translation. |
| 193 | * |
| 194 | * @since 1.0.0 |
| 195 | */ |
| 196 | public function load_plugin_textdomain() { |
| 197 | // $locale = apply_filters('advanced-ads-plugin-locale', get_locale(), $domain); |
| 198 | load_plugin_textdomain( ADVADS_SLUG, false, ADVADS_BASE_DIR . '/languages' ); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Fired when the plugin is activated. |
| 203 | * |
| 204 | * @since 1.0.0 |
| 205 | * @param boolean $network_wide True if WPMU superadmin uses |
| 206 | * "Network Activate" action, false if |
| 207 | * WPMU is disabled or plugin is |
| 208 | * activated on an individual blog. |
| 209 | */ |
| 210 | public function activate($network_wide) { |
| 211 | |
| 212 | return; |
| 213 | // was never used nor missed, but could come in handy one day |
| 214 | |
| 215 | if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
| 216 | |
| 217 | if ( $network_wide ) { |
| 218 | |
| 219 | // Get all blog ids |
| 220 | $blog_ids = $this->model->get_blog_ids(); |
| 221 | |
| 222 | foreach ( $blog_ids as $blog_id ) { |
| 223 | |
| 224 | switch_to_blog( $blog_id ); |
| 225 | $this->single_activate(); |
| 226 | } |
| 227 | |
| 228 | restore_current_blog(); |
| 229 | } else { |
| 230 | $this->single_activate(); |
| 231 | } |
| 232 | } else { |
| 233 | $this->single_activate(); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Fired when the plugin is deactivated. |
| 239 | * |
| 240 | * @since 1.0.0 |
| 241 | * @param boolean $network_wide |
| 242 | * |
| 243 | * True if WPMU superadmin uses |
| 244 | * "Network Deactivate" action, false if |
| 245 | * WPMU is disabled or plugin is |
| 246 | * deactivated on an individual blog. |
| 247 | */ |
| 248 | public function deactivate($network_wide) { |
| 249 | |
| 250 | return; |
| 251 | |
| 252 | if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
| 253 | |
| 254 | if ( $network_wide ) { |
| 255 | |
| 256 | // Get all blog ids |
| 257 | $blog_ids = $this->model->get_blog_ids(); |
| 258 | |
| 259 | foreach ( $blog_ids as $blog_id ) { |
| 260 | |
| 261 | switch_to_blog( $blog_id ); |
| 262 | $this->single_deactivate(); |
| 263 | } |
| 264 | |
| 265 | restore_current_blog(); |
| 266 | } else { |
| 267 | $this->single_deactivate(); |
| 268 | } |
| 269 | } else { |
| 270 | $this->single_deactivate(); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * flush rewrites on plugin activation so permalinks for them work from the beginning on |
| 276 | * |
| 277 | * @since 1.0.0 |
| 278 | * @link http://codex.wordpress.org/Function_Reference/register_post_type#Flushing_Rewrite_on_Activation |
| 279 | */ |
| 280 | /*public function post_types_rewrite_flush(){ |
| 281 | // load custom post type |
| 282 | Advanced_Ads::get_instance()->create_post_types(); |
| 283 | // flush rewrite rules |
| 284 | flush_rewrite_rules(); |
| 285 | }*/ |
| 286 | |
| 287 | /** |
| 288 | * remove WP tag edit page for the ad group taxonomy |
| 289 | * needed, because we can’t remove it with `show_ui` without also removing the meta box |
| 290 | * |
| 291 | * @since 1.0.0 |
| 292 | */ |
| 293 | public function remove_taxonomy_menu_item() { |
| 294 | remove_submenu_page( 'edit.php?post_type=advanced_ads', 'edit-tags.php?taxonomy=advanced_ads_groups&post_type=advanced_ads' ); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * shortcode to include ad in frontend |
| 299 | * |
| 300 | * @since 1.0.0 |
| 301 | * @param arr $atts |
| 302 | */ |
| 303 | public function shortcode_display_ad($atts){ |
| 304 | $id = isset($atts['id']) ? (int) $atts['id'] : 0; |
| 305 | |
| 306 | // use the public available function here |
| 307 | return get_ad( $id ); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * shortcode to include ad from an ad group in frontend |
| 312 | * |
| 313 | * @since 1.0.0 |
| 314 | * @param arr $atts |
| 315 | */ |
| 316 | public function shortcode_display_ad_group($atts){ |
| 317 | $id = isset($atts['id']) ? (int) $atts['id'] : 0; |
| 318 | |
| 319 | // use the public available function here |
| 320 | return get_ad_group( $id ); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * shortcode to display content of an ad placement in frontend |
| 325 | * |
| 326 | * @since 1.1.0 |
| 327 | * @param arr $atts |
| 328 | */ |
| 329 | public function shortcode_display_ad_placement($atts){ |
| 330 | $id = isset($atts['id']) ? (string) $atts['id'] : ''; |
| 331 | |
| 332 | // use the public available function here |
| 333 | return get_ad_placement( $id ); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * return plugin options |
| 338 | * these are the options updated by the user |
| 339 | * |
| 340 | * @since 1.0.1 |
| 341 | * @return array $options |
| 342 | * @todo parse default options |
| 343 | */ |
| 344 | public function options() { |
| 345 | if ( ! isset( $this->options ) ) { |
| 346 | $this->options = get_option( ADVADS_SLUG, array() ); |
| 347 | } |
| 348 | |
| 349 | return $this->options; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * update plugin options (not for settings page, but if automatic options are needed) |
| 354 | * |
| 355 | * @since 1.5.1 |
| 356 | * @param array $options new options |
| 357 | */ |
| 358 | public function update_options( array $options ) { |
| 359 | // do not allow to clear options |
| 360 | if ( $options === array() ) { |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | $this->options = $options; |
| 365 | update_option( ADVADS_SLUG, $options ); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * return internal plugin options |
| 370 | * these are options set by the plugin |
| 371 | * |
| 372 | * @since 1.0.1 |
| 373 | * @return array $options |
| 374 | * @todo parse default options |
| 375 | */ |
| 376 | public function internal_options() { |
| 377 | if ( ! isset( $this->internal_options ) ) { |
| 378 | $defaults = array( |
| 379 | 'version' => ADVADS_VERSION, |
| 380 | 'installed' => time(), // when was this installed |
| 381 | ); |
| 382 | $this->internal_options = get_option( ADVADS_SLUG . '-internal', array() ); |
| 383 | |
| 384 | // save defaults |
| 385 | if($this->internal_options === array()){ |
| 386 | $this->internal_options = $defaults; |
| 387 | $this->update_internal_options($this->internal_options); |
| 388 | } |
| 389 | |
| 390 | // for versions installed prior to 1.5.3 set installed date for now |
| 391 | if( ! isset( $this->internal_options['installed'] )){ |
| 392 | $this->internal_options['installed'] = time(); |
| 393 | $this->update_internal_options($this->internal_options); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | return $this->internal_options; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * update internal plugin options |
| 402 | * |
| 403 | * @since 1.5.1 |
| 404 | * @param array $options new internal options |
| 405 | */ |
| 406 | public function update_internal_options( array $options ) { |
| 407 | // do not allow to clear options |
| 408 | if ( $options === array() ) { |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | $this->internal_options = $options; |
| 413 | update_option( ADVADS_SLUG . '-internal', $options ); |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * get prefix used for frontend elements |
| 418 | * |
| 419 | * @since 1.6.8.2 |
| 420 | */ |
| 421 | public function get_frontend_prefix(){ |
| 422 | if ( ! $this->frontend_prefix ) { |
| 423 | $options = $this->options(); |
| 424 | |
| 425 | if ( ! isset( $options['front-prefix'] ) ) { |
| 426 | if ( isset( $options['id-prefix'] ) ) { |
| 427 | // deprecated: keeps widgets working that previously received an id based on the front-prefix |
| 428 | $this->frontend_prefix = esc_attr( $options['id-prefix'] ); |
| 429 | } else { |
| 430 | $host = parse_url( get_home_url(), PHP_URL_HOST ); |
| 431 | $this->frontend_prefix = preg_match( '/[A-Za-z][A-Za-z0-9_-]{4}/', $host, $result ) ? $result[0] . '-' : Advanced_Ads_Plugin::DEFAULT_FRONTEND_PREFIX; |
| 432 | } |
| 433 | } else { |
| 434 | $this->frontend_prefix = $options['front-prefix']; |
| 435 | } |
| 436 | } |
| 437 | return $this->frontend_prefix; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * get priority used for injection inside content |
| 442 | * |
| 443 | * @since 1.6.10.2 |
| 444 | */ |
| 445 | public function get_content_injection_priority(){ |
| 446 | $options = $this->options(); |
| 447 | |
| 448 | return isset( $options['content-injection-priority'] ) ? intval( $options['content-injection-priority'] ) : 100; |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * returns the capability needed to perform an action |
| 453 | * |
| 454 | * @since 1.6.14 |
| 455 | * @param str $capability a capability to check, can be internal to Advanced Ads |
| 456 | * @return str $capability a valid WordPress capability |
| 457 | */ |
| 458 | public static function user_cap( $capability = 'manage_options' ){ |
| 459 | |
| 460 | global $advanced_ads_capabilities; |
| 461 | |
| 462 | // admins can do everything |
| 463 | // is also a fallback if no option or more specific capability is given |
| 464 | if( current_user_can( 'manage_options' ) ){ |
| 465 | return 'manage_options'; |
| 466 | } |
| 467 | |
| 468 | return apply_filters( 'advanced-ads-capability', $capability ); |
| 469 | |
| 470 | // check, if capability is mapped to an existing WP capability |
| 471 | /*if( isset( $advanced_ads_capabilities[ $capability ] ) ){ |
| 472 | return apply_filters( 'advanced-ads-capability', $advanced_ads_capabilities[ $capability ], $capability ); |
| 473 | } else { |
| 474 | // if not, use 'manage_posts' capability |
| 475 | return apply_filters( 'advanced-ads-capability', 'manage_options', $capability ); |
| 476 | }*/ |
| 477 | |
| 478 | } |
| 479 | |
| 480 | } |
| 481 |