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