EDD_SL_Plugin_Updater.php
7 years ago
ad-ajax.php
8 years ago
ad-debug.php
8 years ago
ad-model.php
8 years ago
ad-select.php
9 years ago
ad.php
7 years ago
ad_ajax_callbacks.php
7 years ago
ad_group.php
7 years ago
ad_placements.php
7 years ago
ad_type_abstract.php
8 years ago
ad_type_content.php
8 years ago
ad_type_dummy.php
8 years ago
ad_type_group.php
8 years ago
ad_type_image.php
7 years ago
ad_type_plain.php
8 years ago
checks.php
7 years ago
compatibility.php
7 years ago
display-conditions.php
7 years ago
filesystem.php
8 years ago
frontend_checks.php
7 years ago
plugin.php
7 years ago
upgrades.php
9 years ago
utils.php
7 years ago
visitor-conditions.php
7 years ago
widget.php
7 years ago
upgrades.php
268 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Upgrade logic from older data to new one |
| 5 | * |
| 6 | * the version number itself is changed in /admin/includes/class-notices.php::register_version_notices() |
| 7 | * |
| 8 | * @since 1.7 |
| 9 | */ |
| 10 | class Advanced_Ads_Upgrades { |
| 11 | |
| 12 | public function __construct(){ |
| 13 | |
| 14 | $internal_options = Advanced_Ads_Plugin::get_instance()->internal_options(); |
| 15 | |
| 16 | // the 'advanced_ads_edit_ads' capability was added to POST_TYPE_SLUG post type in this version |
| 17 | if ( ! isset( $internal_options['version'] ) || version_compare( $internal_options['version'], '1.7.2', '<' ) ) { |
| 18 | Advanced_Ads_Plugin::get_instance()->create_capabilities(); |
| 19 | } |
| 20 | |
| 21 | // suppress version update? |
| 22 | $suppress_version_number_update = false; |
| 23 | |
| 24 | // don’t upgrade if no previous version existed |
| 25 | if( ! empty( $internal_options['version'] ) ) { |
| 26 | if ( version_compare( $internal_options['version'], '1.7' ) == -1 ) { |
| 27 | // run with wp_loaded action, because WP_Query is needed and some plugins inject data that is not yet initialized |
| 28 | add_action( 'wp_loaded', array( $this, 'upgrade_1_7') ); |
| 29 | } |
| 30 | |
| 31 | if ( version_compare( $internal_options['version'], '1.7.4' ) == -1 ) { |
| 32 | // upgrate version number only after this ran through, because of the used filter only available in admin |
| 33 | if( ! is_admin() ){ |
| 34 | $suppress_version_number_update = true; |
| 35 | // run with wp_loaded action, because Upgrades are checked in the plugins_loaded hook |
| 36 | } else { |
| 37 | add_action( 'wp_loaded', array( $this, 'upgrade_1_7_4') ); |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // update version notices – if this doesn’t happen here, the upgrade might run multiple times and destroy updated data |
| 43 | if( ! $suppress_version_number_update ){ |
| 44 | Advanced_Ads_Admin_Notices::get_instance()->update_version_number(); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * upgrade data to version 1.7 |
| 50 | * rewrite existing display conditions |
| 51 | */ |
| 52 | public function upgrade_1_7(){ |
| 53 | |
| 54 | // get all ads, regardless of the publish status |
| 55 | $args['post_status'] = 'any'; |
| 56 | $args['suppress_filters'] = true; // needed to remove issue with a broken plugin from the repository |
| 57 | $ads = Advanced_Ads::get_instance()->get_model()->get_ads( $args ); |
| 58 | |
| 59 | // iterate through ads |
| 60 | // error_log(print_r($ads, true)); |
| 61 | error_log(print_r('–– STARTING ADVANCED ADS data upgrade to version 1.7 ––', true)); |
| 62 | foreach( $ads as $_ad ){ |
| 63 | // ad options |
| 64 | $option_key = Advanced_Ads_Ad::$options_meta_field; |
| 65 | if( !isset( $_ad->ID ) || ! $option_key ){ |
| 66 | continue; |
| 67 | } |
| 68 | $options = get_post_meta( $_ad->ID, $option_key, true ); |
| 69 | // rewrite display conditions |
| 70 | if( ! isset( $options['conditions'] ) ){ |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | error_log(print_r('AD ID: ' . $_ad->ID, true)); |
| 75 | error_log(print_r('OLD CONDITIONS', true)); |
| 76 | error_log(print_r($options['conditions'], true)); |
| 77 | |
| 78 | $old_conditions = $options['conditions']; |
| 79 | |
| 80 | // check if conditions are disabled |
| 81 | if( ! isset( $old_conditions['enabled'] ) || ! $old_conditions['enabled'] ){ |
| 82 | $new_conditions = ''; |
| 83 | } else { |
| 84 | $new_conditions = array(); |
| 85 | |
| 86 | // rewrite general conditions |
| 87 | $old_general_conditions = array( |
| 88 | 'is_front_page', |
| 89 | 'is_singular', |
| 90 | 'is_archive', |
| 91 | 'is_search', |
| 92 | 'is_404', |
| 93 | 'is_attachment', |
| 94 | 'is_main_query' |
| 95 | ); |
| 96 | $general = array(); |
| 97 | foreach( $old_general_conditions as $_general_condition ){ |
| 98 | if( isset( $old_conditions[ $_general_condition ] ) && $old_conditions[ $_general_condition ] ) { |
| 99 | $general[] = $_general_condition; |
| 100 | } |
| 101 | } |
| 102 | // move general conditions into display conditions |
| 103 | // only, if the number of conditions in the previous setting is lower, because only that means there is an active limitation |
| 104 | // not sure if allowing an empty array is logical, but some users might have set this up to hide an ad |
| 105 | if( count( $general ) < count( $old_general_conditions ) ){ |
| 106 | $new_conditions[] = array( |
| 107 | 'type' => 'general', |
| 108 | 'value' => $general |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | // rewrite post types condition |
| 113 | if( isset( $old_conditions[ 'posttypes' ]['include'] ) |
| 114 | && ( !isset ( $old_conditions[ 'posttypes' ]['all'] ) |
| 115 | || ! $old_conditions[ 'posttypes' ]['all'] ) ) { |
| 116 | if ( is_string( $old_conditions[ 'posttypes' ]['include']) ) { |
| 117 | $old_conditions[ 'posttypes' ]['include'] = explode( ',', $old_conditions[ 'posttypes' ]['include'] ); |
| 118 | } |
| 119 | $new_conditions[] = array( |
| 120 | 'type' => 'posttypes', |
| 121 | 'value' => $old_conditions[ 'posttypes' ]['include'] |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * rewrite category ids and category archive ids |
| 127 | * |
| 128 | * the problem is that before there was no connection between term ids and taxonomy, now, each taxonomy has its own condition |
| 129 | */ |
| 130 | // check, if there are even such options set |
| 131 | if( ( isset( $old_conditions[ 'categoryids' ] ) |
| 132 | && ( !isset ( $old_conditions[ 'categoryids' ]['all'] ) |
| 133 | || ! $old_conditions[ 'categoryids' ]['all'] ) ) |
| 134 | || ( isset( $old_conditions[ 'categoryarchiveids' ] ) |
| 135 | && ( !isset ( $old_conditions[ 'categoryarchiveids' ]['all'] ) |
| 136 | || ! $old_conditions[ 'categoryarchiveids' ]['all'] ) )) |
| 137 | { |
| 138 | |
| 139 | // get all taxonomies |
| 140 | $taxonomies = get_taxonomies( array('public' => true, 'publicly_queryable' => true), 'objects', 'or' ); |
| 141 | $taxonomy_terms = array(); |
| 142 | foreach ( $taxonomies as $_tax ) { |
| 143 | if( $_tax->name === 'advanced_ads_groups' ){ |
| 144 | continue; |
| 145 | } |
| 146 | // get all terms |
| 147 | $terms = get_terms( $_tax->name, array('hide_empty' => false, 'number' => 0, 'fields' => 'ids' ) ); |
| 148 | if ( is_wp_error( $terms ) || ! count( $terms ) ){ |
| 149 | continue; |
| 150 | } else { |
| 151 | $taxonomy_terms[ $_tax->name ] = $terms; |
| 152 | } |
| 153 | |
| 154 | // get terms that are in all terms and in active terms |
| 155 | if( isset( $old_conditions[ 'categoryids' ] ) |
| 156 | && ( !isset ( $old_conditions[ 'categoryids' ]['all'] ) |
| 157 | || ! $old_conditions[ 'categoryids' ]['all'] ) ) |
| 158 | { |
| 159 | // honor "include" option first |
| 160 | if( isset ( $old_conditions[ 'categoryids' ]['include'] ) && count( $old_conditions[ 'categoryids' ]['include'] ) |
| 161 | && $same_values = array_intersect($terms, $old_conditions[ 'categoryids' ]['include']) ){ |
| 162 | $new_conditions[] = array( |
| 163 | 'type' => 'taxonomy_' . $_tax->name , |
| 164 | 'operator' => 'is', |
| 165 | 'value' => $same_values |
| 166 | ); |
| 167 | } elseif ( isset ( $old_conditions[ 'categoryids' ]['exclude'] ) && count( $old_conditions[ 'categoryids' ]['exclude'] ) |
| 168 | && $same_values = array_intersect($terms, $old_conditions[ 'categoryids' ]['exclude']) ){ |
| 169 | $new_conditions[] = array( |
| 170 | 'type' => 'taxonomy_' . $_tax->name , |
| 171 | 'operator' => 'is_not', |
| 172 | 'value' => $same_values |
| 173 | ); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // get terms that are in all terms and in active terms |
| 178 | if( isset( $old_conditions[ 'categoryarchiveids' ] ) |
| 179 | && ( !isset ( $old_conditions[ 'categoryarchiveids' ]['all'] ) |
| 180 | || ! $old_conditions[ 'categoryarchiveids' ]['all'] ) ) |
| 181 | { |
| 182 | // honor "include" option first |
| 183 | if( isset ( $old_conditions[ 'categoryarchiveids' ]['include'] ) && count( $old_conditions[ 'categoryarchiveids' ]['include'] ) |
| 184 | && $same_values = array_intersect($terms, $old_conditions[ 'categoryarchiveids' ]['include']) ){ |
| 185 | $new_conditions[] = array( |
| 186 | 'type' => 'archive_' . $_tax->name , |
| 187 | 'operator' => 'is', |
| 188 | 'value' => $same_values |
| 189 | ); |
| 190 | } elseif ( isset ( $old_conditions[ 'categoryarchiveids' ]['exclude'] ) && count( $old_conditions[ 'categoryarchiveids' ]['exclude'] ) |
| 191 | && $same_values = array_intersect($terms, $old_conditions[ 'categoryarchiveids' ]['exclude']) ){ |
| 192 | $new_conditions[] = array( |
| 193 | 'type' => 'archive_' . $_tax->name , |
| 194 | 'operator' => 'is_not', |
| 195 | 'value' => $same_values |
| 196 | ); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // rewrite single post ids |
| 203 | if( isset ( $old_conditions[ 'postids' ]['ids'] ) |
| 204 | && isset ( $old_conditions[ 'postids' ]['method'] ) |
| 205 | && $old_conditions[ 'postids' ]['method'] |
| 206 | && ( !isset ( $old_conditions[ 'postids' ]['all'] ) |
| 207 | || ! $old_conditions[ 'postids' ]['all'] ) ) { |
| 208 | $operator = ( $old_conditions[ 'postids' ]['method'] === 'exclude' ) ? 'is_not' : 'is'; |
| 209 | if ( is_string( $old_conditions[ 'postids' ]['ids']) ) { |
| 210 | $old_conditions[ 'postids' ]['ids'] = explode( ',', $old_conditions[ 'postids' ]['ids'] ); |
| 211 | } |
| 212 | $new_conditions[] = array( |
| 213 | 'type' => 'postids', |
| 214 | 'operator' => $operator, |
| 215 | 'value' => $old_conditions[ 'postids' ]['ids'] |
| 216 | ); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | error_log(print_r('NEW CONDITIONS', true)); |
| 221 | error_log(print_r($new_conditions, true)); |
| 222 | |
| 223 | $options['conditions'] = $new_conditions; |
| 224 | |
| 225 | // save conditions |
| 226 | update_post_meta( $_ad->ID, $option_key, $options ); |
| 227 | } |
| 228 | |
| 229 | error_log(print_r('up to 1.7', true)); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * upgrades for version 1.7.4 |
| 234 | * reactivate active add-on licenses, needed only once after upgrade of the plugin shop |
| 235 | */ |
| 236 | public function upgrade_1_7_4(){ |
| 237 | |
| 238 | // ignore, if not main blog |
| 239 | if( is_multisite() && ! is_main_site() ){ |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | $add_ons = apply_filters( 'advanced-ads-add-ons', array() ); |
| 244 | |
| 245 | // return if no add-ons found |
| 246 | if( $add_ons === array() ) { |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | error_log(print_r('–– STARTING ADVANCED ADS 1.7.4 upgrade ––', true)); |
| 251 | foreach( $add_ons as $_add_on_key => $_add_on ){ |
| 252 | |
| 253 | // check status |
| 254 | if(Advanced_Ads_Admin_Licenses::get_instance()->get_license_status( $_add_on['options_slug'] ) !== 'valid' ) { |
| 255 | continue; |
| 256 | } |
| 257 | |
| 258 | // retrieve our license key from the DB |
| 259 | $licenses = get_option(ADVADS_SLUG . '-licenses', array()); |
| 260 | $license_key = isset($licenses[$_add_on_key]) ? $licenses[$_add_on_key] : ''; |
| 261 | |
| 262 | $result = Advanced_Ads_Admin::get_instance()->activate_license( $_add_on_key, $_add_on['name'], $_add_on['options_slug'], $license_key ); |
| 263 | error_log( sprintf( 'Register license key for %s: %s', $_add_on['name'], $result ) ); |
| 264 | |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | } |