ad-ajax.php
11 years ago
ad-model.php
11 years ago
ad-select.php
11 years ago
ad.php
11 years ago
ad_ajax_callbacks.php
11 years ago
ad_group.php
11 years ago
ad_placements.php
11 years ago
ad_type_abstract.php
11 years ago
ad_type_content.php
11 years ago
ad_type_plain.php
11 years ago
plugin.php
11 years ago
widget.php
11 years ago
plugin.php
336 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 = false; |
| 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 = false; |
| 41 | |
| 42 | private function __construct() { |
| 43 | register_activation_hook( dirname( __FILE__ ), array( $this, 'activate' ) ); |
| 44 | register_deactivation_hook( dirname( __FILE__ ), array( $this, 'deactivate' ) ); |
| 45 | |
| 46 | add_action( 'plugins_loaded', array( $this, 'wp_plugins_loaded' ) ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * |
| 51 | * @return Advanced_Ads_Plugin |
| 52 | */ |
| 53 | public static function get_instance() { |
| 54 | // If the single instance hasn't been set, set it now. |
| 55 | if ( null === self::$instance ) { |
| 56 | self::$instance = new self; |
| 57 | } |
| 58 | |
| 59 | return self::$instance; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * |
| 64 | * @param Advanced_Ads_Model $model |
| 65 | */ |
| 66 | public function set_model(Advanced_Ads_Model $model) { |
| 67 | $this->model = $model; |
| 68 | } |
| 69 | |
| 70 | public function wp_plugins_loaded() { |
| 71 | // Load plugin text domain |
| 72 | $this->load_plugin_textdomain(); |
| 73 | |
| 74 | // activate plugin when new blog is added on multisites // -TODO this is admin-only |
| 75 | add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) ); |
| 76 | |
| 77 | // Load public-facing style sheet and JavaScript. |
| 78 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); |
| 79 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 80 | |
| 81 | // add short codes |
| 82 | add_shortcode( 'the_ad', array( $this, 'shortcode_display_ad' ) ); |
| 83 | add_shortcode( 'the_ad_group', array( $this, 'shortcode_display_ad_group' ) ); |
| 84 | add_shortcode( 'the_ad_placement', array( $this, 'shortcode_display_ad_placement' ) ); |
| 85 | |
| 86 | // remove default ad group menu item // -TODO only for admin |
| 87 | add_action( 'admin_menu', array( $this, 'remove_taxonomy_menu_item' ) ); |
| 88 | // load widgets |
| 89 | add_action( 'widgets_init', array( $this, 'advads_widget_init' ) ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Register and enqueue public-facing style sheet. |
| 94 | * |
| 95 | * @since 1.0.0 |
| 96 | */ |
| 97 | public function enqueue_styles() { |
| 98 | // wp_enqueue_style( $this->get_plugin_slug() . '-plugin-styles', plugins_url('assets/css/public.css', __FILE__), array(), Advanced_Ads::VERSION); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Return the plugin slug. |
| 103 | * |
| 104 | * @since 1.0.0 |
| 105 | * @return Plugin slug variable. |
| 106 | */ |
| 107 | public function get_plugin_slug() { |
| 108 | return ADVADS_SLUG; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Register and enqueues public-facing JavaScript files. |
| 113 | * |
| 114 | * @since 1.0.0 |
| 115 | */ |
| 116 | public function enqueue_scripts() { |
| 117 | // wp_enqueue_script( $this->get_plugin_slug() . '-plugin-script', plugins_url('assets/js/public.js', __FILE__), array('jquery'), Advanced_Ads::VERSION); |
| 118 | $options = $this->options(); |
| 119 | if ( ! empty($options['advanced-js']) ){ |
| 120 | wp_enqueue_script( $this->get_plugin_slug() . '-advanced-js', ADVADS_BASE_URL . 'assets/js/advanced.js', array( 'jquery' ), Advanced_Ads::VERSION ); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | public function advads_widget_init() { |
| 125 | register_widget( 'Advads_Widget' ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Fired when a new site is activated with a WPMU environment. |
| 130 | * |
| 131 | * @since 1.0.0 |
| 132 | * @param int $blog_id ID of the new blog. |
| 133 | */ |
| 134 | public function activate_new_site($blog_id) { |
| 135 | |
| 136 | if ( 1 !== did_action( 'wpmu_new_blog' ) ) { |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | switch_to_blog( $blog_id ); |
| 141 | $this->single_activate(); |
| 142 | restore_current_blog(); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Fired for each blog when the plugin is activated. |
| 147 | * |
| 148 | * @since 1.0.0 |
| 149 | */ |
| 150 | protected function single_activate() { |
| 151 | $this->post_types_rewrite_flush(); |
| 152 | // -TODO inform modules |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Fired for each blog when the plugin is deactivated. |
| 157 | * |
| 158 | * @since 1.0.0 |
| 159 | */ |
| 160 | protected function single_deactivate() { |
| 161 | // -TODO inform modules |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Load the plugin text domain for translation. |
| 166 | * |
| 167 | * @since 1.0.0 |
| 168 | */ |
| 169 | public function load_plugin_textdomain() { |
| 170 | // $locale = apply_filters('advanced-ads-plugin-locale', get_locale(), $domain); |
| 171 | load_plugin_textdomain( ADVADS_SLUG, false, ADVADS_BASE_DIR . '/languages' ); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Fired when the plugin is activated. |
| 176 | * |
| 177 | * @since 1.0.0 |
| 178 | * @param boolean $network_wide True if WPMU superadmin uses |
| 179 | * "Network Activate" action, false if |
| 180 | * WPMU is disabled or plugin is |
| 181 | * activated on an individual blog. |
| 182 | */ |
| 183 | public function activate($network_wide) { |
| 184 | |
| 185 | if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
| 186 | |
| 187 | if ( $network_wide ) { |
| 188 | |
| 189 | // Get all blog ids |
| 190 | $blog_ids = $this->model->get_blog_ids(); |
| 191 | |
| 192 | foreach ( $blog_ids as $blog_id ) { |
| 193 | |
| 194 | switch_to_blog( $blog_id ); |
| 195 | $this->single_activate(); |
| 196 | } |
| 197 | |
| 198 | restore_current_blog(); |
| 199 | } else { |
| 200 | $this->single_activate(); |
| 201 | } |
| 202 | } else { |
| 203 | $this->single_activate(); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Fired when the plugin is deactivated. |
| 209 | * |
| 210 | * @since 1.0.0 |
| 211 | * @param boolean $network_wide |
| 212 | * |
| 213 | * True if WPMU superadmin uses |
| 214 | * "Network Deactivate" action, false if |
| 215 | * WPMU is disabled or plugin is |
| 216 | * deactivated on an individual blog. |
| 217 | */ |
| 218 | public function deactivate($network_wide) { |
| 219 | |
| 220 | if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
| 221 | |
| 222 | if ( $network_wide ) { |
| 223 | |
| 224 | // Get all blog ids |
| 225 | $blog_ids = $this->model->get_blog_ids(); |
| 226 | |
| 227 | foreach ( $blog_ids as $blog_id ) { |
| 228 | |
| 229 | switch_to_blog( $blog_id ); |
| 230 | $this->single_deactivate(); |
| 231 | } |
| 232 | |
| 233 | restore_current_blog(); |
| 234 | } else { |
| 235 | $this->single_deactivate(); |
| 236 | } |
| 237 | } else { |
| 238 | $this->single_deactivate(); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * flush rewrites on plugin activation so permalinks for them work from the beginning on |
| 244 | * |
| 245 | * @since 1.0.0 |
| 246 | * @link http://codex.wordpress.org/Function_Reference/register_post_type#Flushing_Rewrite_on_Activation |
| 247 | */ |
| 248 | public function post_types_rewrite_flush(){ |
| 249 | // load custom post type |
| 250 | Advanced_Ads::get_instance()->create_post_types(); |
| 251 | // flush rewrite rules |
| 252 | flush_rewrite_rules(); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * remove WP tag edit page for the ad group taxonomy |
| 257 | * needed, because we can’t remove it with `show_ui` without also removing the meta box |
| 258 | * |
| 259 | * @since 1.0.0 |
| 260 | */ |
| 261 | public function remove_taxonomy_menu_item() { |
| 262 | remove_submenu_page( 'edit.php?post_type=advanced_ads', 'edit-tags.php?taxonomy=advanced_ads_groups&post_type=advanced_ads' ); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * shortcode to include ad in frontend |
| 267 | * |
| 268 | * @since 1.0.0 |
| 269 | * @param arr $atts |
| 270 | */ |
| 271 | public function shortcode_display_ad($atts){ |
| 272 | $id = isset($atts['id']) ? (int) $atts['id'] : 0; |
| 273 | |
| 274 | // use the public available function here |
| 275 | return get_ad( $id ); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * shortcode to include ad from an ad group in frontend |
| 280 | * |
| 281 | * @since 1.0.0 |
| 282 | * @param arr $atts |
| 283 | */ |
| 284 | public function shortcode_display_ad_group($atts){ |
| 285 | $id = isset($atts['id']) ? (int) $atts['id'] : 0; |
| 286 | |
| 287 | // use the public available function here |
| 288 | return get_ad_group( $id ); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * shortcode to display content of an ad placement in frontend |
| 293 | * |
| 294 | * @since 1.1.0 |
| 295 | * @param arr $atts |
| 296 | */ |
| 297 | public function shortcode_display_ad_placement($atts){ |
| 298 | $id = isset($atts['id']) ? (string) $atts['id'] : ''; |
| 299 | |
| 300 | // use the public available function here |
| 301 | return get_ad_placement( $id ); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * return plugin options |
| 306 | * these are the options updated by the user |
| 307 | * |
| 308 | * @since 1.0.1 |
| 309 | * @return array $options |
| 310 | * @todo parse default options |
| 311 | */ |
| 312 | public function options() { |
| 313 | if ( false === $this->options ){ |
| 314 | $this->options = get_option( ADVADS_SLUG, array() ); |
| 315 | } |
| 316 | |
| 317 | return $this->options; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * return internal plugin options |
| 322 | * these are options set by the plugin |
| 323 | * |
| 324 | * @since 1.0.1 |
| 325 | * @return array $options |
| 326 | * @todo parse default options |
| 327 | */ |
| 328 | public function internal_options() { |
| 329 | if ( false === $this->internal_options ){ |
| 330 | $this->internal_options = get_option( ADVADS_SLUG . '-internal', array() ); |
| 331 | } |
| 332 | |
| 333 | return $this->internal_options; |
| 334 | } |
| 335 | } |
| 336 |