class-ad-groups-list.php
9 years ago
class-ad-type.php
9 years ago
class-licenses.php
9 years ago
class-menu.php
9 years ago
class-meta-box.php
9 years ago
class-notices.php
9 years ago
class-options.php
9 years ago
class-overview-widgets.php
9 years ago
class-settings.php
9 years ago
class-shortcode-creator.php
9 years ago
notices.php
9 years ago
shortcode-creator-l10n.php
10 years ago
class-shortcode-creator.php
182 lines
| 1 | <?php |
| 2 | /** |
| 3 | * shortcode generator for TinyMCE editor |
| 4 | * |
| 5 | */ |
| 6 | class Advanced_Ads_Shortcode_Creator { |
| 7 | /** |
| 8 | * instance of this class. |
| 9 | * |
| 10 | * @var object |
| 11 | */ |
| 12 | protected static $instance = null; |
| 13 | |
| 14 | private function __construct() { |
| 15 | add_action( 'init', array ( $this, 'init' ) ); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Return an instance of this class. |
| 20 | * |
| 21 | * @return object A single instance of this class. |
| 22 | */ |
| 23 | public static function get_instance() { |
| 24 | // If the single instance hasn't been set, set it now. |
| 25 | if ( null == self::$instance ) { |
| 26 | self::$instance = new self; |
| 27 | } |
| 28 | |
| 29 | return self::$instance; |
| 30 | } |
| 31 | |
| 32 | public function init() { |
| 33 | $options = Advanced_Ads::get_instance()->options(); |
| 34 | |
| 35 | if ( 'true' != get_user_option( 'rich_editing' ) |
| 36 | || ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_place_ads' ) ) |
| 37 | || defined( 'ADVANCED_ADS_DISABLE_SHORTCODE_BUTTON' ) |
| 38 | || ! empty( $options['disable-shortcode-button'] ) |
| 39 | ) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | add_filter( 'mce_external_plugins', array( $this, 'add_plugin' ) ); |
| 44 | add_filter( 'mce_buttons', array( $this, 'register_buttons' ) ); |
| 45 | add_filter( 'mce_external_languages', array( $this, 'add_l10n' ) ); |
| 46 | add_action( 'wp_ajax_advads_content_for_shortcode_creator', array( $this, 'get_content_for_shortcode_creator' ) ); |
| 47 | |
| 48 | add_filter( 'the_editor', array( $this, 'add_addblocker_warning' ) ); |
| 49 | add_action( 'admin_footer', array( $this, 'maybe_show_adblocker_warning' ) ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * add the plugin to array of external TinyMCE plugins |
| 54 | * |
| 55 | */ |
| 56 | public function add_plugin( $plugin_array ) { |
| 57 | $plugin_array['advads_shortcode'] = ADVADS_BASE_URL . 'admin/assets/js/shortcode.js'; |
| 58 | return $plugin_array; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * add button to tinyMCE window |
| 63 | * |
| 64 | */ |
| 65 | public function register_buttons( $buttons ) { |
| 66 | $buttons[] = 'advads_shortcode_button'; |
| 67 | return $buttons; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * prints html select field for shortcode creator |
| 72 | * |
| 73 | */ |
| 74 | public function get_content_for_shortcode_creator() { |
| 75 | if ( ! ( current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' ) ) ) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | $items = self::items_for_select(); ?> |
| 80 | |
| 81 | <select id="advads-select-for-shortcode"> |
| 82 | <option value=""><?php _e( '--empty--', 'advanced-ads' ); ?></option> |
| 83 | <?php if ( isset( $items['ads'] ) ) : ?> |
| 84 | <optgroup label="<?php _e( 'Ads', 'advanced-ads' ); ?>"> |
| 85 | <?php foreach ( $items['ads'] as $_item_id => $_item_title ) : ?> |
| 86 | <option value="<?php echo $_item_id; ?>"><?php echo $_item_title; ?></option> |
| 87 | <?php endforeach; ?> |
| 88 | </optgroup> |
| 89 | <?php endif; ?> |
| 90 | <?php if ( isset( $items['groups'] ) ) : ?> |
| 91 | <optgroup label="<?php _e( 'Ad Groups', 'advanced-ads' ); ?>"> |
| 92 | <?php foreach ( $items['groups'] as $_item_id => $_item_title ) : ?> |
| 93 | <option value="<?php echo $_item_id; ?>"><?php echo $_item_title; ?></option> |
| 94 | <?php endforeach; ?> |
| 95 | </optgroup> |
| 96 | <?php endif; ?> |
| 97 | <?php if ( isset( $items['placements'] ) ) : ?> |
| 98 | <optgroup label="<?php _e( 'Placements', 'advanced-ads' ); ?>"> |
| 99 | <?php foreach ( $items['placements'] as $_item_id => $_item_title ) : ?> |
| 100 | <option value="<?php echo $_item_id; ?>"><?php echo $_item_title; ?></option> |
| 101 | <?php endforeach; ?> |
| 102 | </optgroup> |
| 103 | <?php endif; ?> |
| 104 | </select><?php |
| 105 | exit(); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * get items for item select field |
| 110 | * |
| 111 | * @return arr $select items for select field |
| 112 | */ |
| 113 | public static function items_for_select(){ |
| 114 | $select = array(); |
| 115 | $model = Advanced_Ads::get_instance()->get_model(); |
| 116 | |
| 117 | // load all ads |
| 118 | $ads = $model->get_ads( array( 'orderby' => 'title', 'order' => 'ASC' ) ); |
| 119 | foreach ( $ads as $_ad ){ |
| 120 | $select['ads']['ad_' . $_ad->ID] = $_ad->post_title; |
| 121 | } |
| 122 | |
| 123 | // load all ad groups |
| 124 | $groups = $model->get_ad_groups(); |
| 125 | foreach ( $groups as $_group ){ |
| 126 | $select['groups']['group_' . $_group->term_id] = $_group->name; |
| 127 | } |
| 128 | |
| 129 | // load all placements |
| 130 | $placements = $model->get_ad_placements_array(); |
| 131 | ksort( $placements ); |
| 132 | foreach ( $placements as $key => $_placement ) { |
| 133 | $select['placements']['placement_' . $key] = $_placement['name']; |
| 134 | } |
| 135 | |
| 136 | return $select; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * add localisation |
| 141 | */ |
| 142 | public function add_l10n( $mce_external_languages ) { |
| 143 | $mce_external_languages[ 'advads_shortcode' ] = ADVADS_BASE_PATH . 'admin/includes/shortcode-creator-l10n.php'; |
| 144 | return $mce_external_languages; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Add a warning above TinyMCE editor. |
| 149 | * |
| 150 | * @param string $output Editor's HTML markup. |
| 151 | */ |
| 152 | public function add_addblocker_warning( $output ) { |
| 153 | ob_start(); ?> |
| 154 | <div style="display: none; margin: 10px 8px; color: red;" class="advanced-ads-shortcode-button-warning"><?php |
| 155 | printf( __ ( 'Please, either switch off your ad blocker or disable the shortcode button in the <a href="%s" target="_blank">settings</a>.', 'advanced-ads' ), |
| 156 | admin_url( 'admin.php?page=advanced-ads-settings' ) ); ?> |
| 157 | </div> |
| 158 | <?php |
| 159 | return ob_get_clean() . $output; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Show a warning above TinyMCE editor when an adblock is enabled. |
| 164 | */ |
| 165 | public function maybe_show_adblocker_warning() { ?> |
| 166 | <script> |
| 167 | (function(){ |
| 168 | if ( 'undefined' === typeof advanced_ads_adblocker_test ) { |
| 169 | try { |
| 170 | var messages = document.querySelectorAll( '.advanced-ads-shortcode-button-warning' ) |
| 171 | } catch ( e ) { return; } |
| 172 | for ( var i = 0; i < messages.length; i++ ) { |
| 173 | messages[ i ].style.display = 'block'; |
| 174 | } |
| 175 | } |
| 176 | })(); |
| 177 | </script> |
| 178 | <?php |
| 179 | } |
| 180 | |
| 181 | } |
| 182 |