upgrades.php
| 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 | // don’t upgrade if no previous version existed |
| 17 | if( empty( $internal_options['version'] ) ) { |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | if ( version_compare( $internal_options['version'], '1.7' ) == -1 ) { |
| 22 | // run with wp_loaded action, because WP_Query is needed and some plugins inject data that is not yet initialized |
| 23 | add_action( 'wp_loaded', array( $this, 'upgrade_1_7') ); |
| 24 | } |
| 25 | |
| 26 | // the 'advanced_ads_edit_ads' capability was added to POST_TYPE_SLUG post type in this version |
| 27 | if ( version_compare( $internal_options['version'], '1.7.2', '<' ) ) { |
| 28 | Advanced_Ads_Plugin::get_instance()->create_capabilities(); |
| 29 | } |
| 30 | |
| 31 | // update version notices – if this doesn’t happen here, the upgrade might run multiple times and destroy updated data |
| 32 | Advanced_Ads_Admin_Notices::get_instance()->update_version_number(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * upgrade data to version 1.7 |
| 37 | * rewrite existing display conditions |
| 38 | */ |
| 39 | public function upgrade_1_7(){ |
| 40 | |
| 41 | // get all ads, regardless of the publish status |
| 42 | $args['post_status'] = 'any'; |
| 43 | $args['suppress_filters'] = true; // needed to remove issue with a broken plugin from the repository |
| 44 | $ads = Advanced_Ads::get_instance()->get_model()->get_ads( $args ); |
| 45 | |
| 46 | // iterate through ads |
| 47 | // error_log(print_r($ads, true)); |
| 48 | error_log(print_r('–– STARTING ADVANCED ADS data upgrade to version 1.7 ––', true)); |
| 49 | foreach( $ads as $_ad ){ |
| 50 | // ad options |
| 51 | $option_key = Advanced_Ads_Ad::$options_meta_field; |
| 52 | if( !isset( $_ad->ID ) || ! $option_key ){ |
| 53 | continue; |
| 54 | } |
| 55 | $options = get_post_meta( $_ad->ID, $option_key, true ); |
| 56 | // rewrite display conditions |
| 57 | if( ! isset( $options['conditions'] ) ){ |
| 58 | continue; |
| 59 | } |
| 60 | |
| 61 | error_log(print_r('AD ID: ' . $_ad->ID, true)); |
| 62 | error_log(print_r('OLD CONDITIONS', true)); |
| 63 | error_log(print_r($options['conditions'], true)); |
| 64 | |
| 65 | $old_conditions = $options['conditions']; |
| 66 | |
| 67 | // check if conditions are disabled |
| 68 | if( ! isset( $old_conditions['enabled'] ) || ! $old_conditions['enabled'] ){ |
| 69 | $new_conditions = ''; |
| 70 | } else { |
| 71 | $new_conditions = array(); |
| 72 | |
| 73 | // rewrite general conditions |
| 74 | $old_general_conditions = array( |
| 75 | 'is_front_page', |
| 76 | 'is_singular', |
| 77 | 'is_archive', |
| 78 | 'is_search', |
| 79 | 'is_404', |
| 80 | 'is_attachment', |
| 81 | 'is_main_query' |
| 82 | ); |
| 83 | $general = array(); |
| 84 | foreach( $old_general_conditions as $_general_condition ){ |
| 85 | if( isset( $old_conditions[ $_general_condition ] ) && $old_conditions[ $_general_condition ] ) { |
| 86 | $general[] = $_general_condition; |
| 87 | } |
| 88 | } |
| 89 | // move general conditions into display conditions |
| 90 | // only, if the number of conditions in the previous setting is lower, because only that means there is an active limitation |
| 91 | // not sure if allowing an empty array is logical, but some users might have set this up to hide an ad |
| 92 | if( count( $general ) < count( $old_general_conditions ) ){ |
| 93 | $new_conditions[] = array( |
| 94 | 'type' => 'general', |
| 95 | 'value' => $general |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | // rewrite post types condition |
| 100 | if( isset( $old_conditions[ 'posttypes' ]['include'] ) |
| 101 | && ( !isset ( $old_conditions[ 'posttypes' ]['all'] ) |
| 102 | || ! $old_conditions[ 'posttypes' ]['all'] ) ) { |
| 103 | if ( is_string( $old_conditions[ 'posttypes' ]['include']) ) { |
| 104 | $old_conditions[ 'posttypes' ]['include'] = explode( ',', $old_conditions[ 'posttypes' ]['include'] ); |
| 105 | } |
| 106 | $new_conditions[] = array( |
| 107 | 'type' => 'posttypes', |
| 108 | 'value' => $old_conditions[ 'posttypes' ]['include'] |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * rewrite category ids and category archive ids |
| 114 | * |
| 115 | * the problem is that before there was no connection between term ids and taxonomy, now, each taxonomy has its own condition |
| 116 | */ |
| 117 | // check, if there are even such options set |
| 118 | if( ( isset( $old_conditions[ 'categoryids' ] ) |
| 119 | && ( !isset ( $old_conditions[ 'categoryids' ]['all'] ) |
| 120 | || ! $old_conditions[ 'categoryids' ]['all'] ) ) |
| 121 | || ( isset( $old_conditions[ 'categoryarchiveids' ] ) |
| 122 | && ( !isset ( $old_conditions[ 'categoryarchiveids' ]['all'] ) |
| 123 | || ! $old_conditions[ 'categoryarchiveids' ]['all'] ) )) |
| 124 | { |
| 125 | |
| 126 | // get all taxonomies |
| 127 | $taxonomies = get_taxonomies( array('public' => true, 'publicly_queryable' => true), 'objects', 'or' ); |
| 128 | $taxonomy_terms = array(); |
| 129 | foreach ( $taxonomies as $_tax ) { |
| 130 | if( $_tax->name === 'advanced_ads_groups' ){ |
| 131 | continue; |
| 132 | } |
| 133 | // get all terms |
| 134 | $terms = get_terms( $_tax->name, array('hide_empty' => false, 'number' => 0, 'fields' => 'ids' ) ); |
| 135 | if ( is_wp_error( $terms ) || ! count( $terms ) ){ |
| 136 | continue; |
| 137 | } else { |
| 138 | $taxonomy_terms[ $_tax->name ] = $terms; |
| 139 | } |
| 140 | |
| 141 | // get terms that are in all terms and in active terms |
| 142 | if( isset( $old_conditions[ 'categoryids' ] ) |
| 143 | && ( !isset ( $old_conditions[ 'categoryids' ]['all'] ) |
| 144 | || ! $old_conditions[ 'categoryids' ]['all'] ) ) |
| 145 | { |
| 146 | // honor "include" option first |
| 147 | if( isset ( $old_conditions[ 'categoryids' ]['include'] ) && count( $old_conditions[ 'categoryids' ]['include'] ) |
| 148 | && $same_values = array_intersect($terms, $old_conditions[ 'categoryids' ]['include']) ){ |
| 149 | $new_conditions[] = array( |
| 150 | 'type' => 'taxonomy_' . $_tax->name , |
| 151 | 'operator' => 'is', |
| 152 | 'value' => $same_values |
| 153 | ); |
| 154 | } elseif ( isset ( $old_conditions[ 'categoryids' ]['exclude'] ) && count( $old_conditions[ 'categoryids' ]['exclude'] ) |
| 155 | && $same_values = array_intersect($terms, $old_conditions[ 'categoryids' ]['exclude']) ){ |
| 156 | $new_conditions[] = array( |
| 157 | 'type' => 'taxonomy_' . $_tax->name , |
| 158 | 'operator' => 'is_not', |
| 159 | 'value' => $same_values |
| 160 | ); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // get terms that are in all terms and in active terms |
| 165 | if( isset( $old_conditions[ 'categoryarchiveids' ] ) |
| 166 | && ( !isset ( $old_conditions[ 'categoryarchiveids' ]['all'] ) |
| 167 | || ! $old_conditions[ 'categoryarchiveids' ]['all'] ) ) |
| 168 | { |
| 169 | // honor "include" option first |
| 170 | if( isset ( $old_conditions[ 'categoryarchiveids' ]['include'] ) && count( $old_conditions[ 'categoryarchiveids' ]['include'] ) |
| 171 | && $same_values = array_intersect($terms, $old_conditions[ 'categoryarchiveids' ]['include']) ){ |
| 172 | $new_conditions[] = array( |
| 173 | 'type' => 'archive_' . $_tax->name , |
| 174 | 'operator' => 'is', |
| 175 | 'value' => $same_values |
| 176 | ); |
| 177 | } elseif ( isset ( $old_conditions[ 'categoryarchiveids' ]['exclude'] ) && count( $old_conditions[ 'categoryarchiveids' ]['exclude'] ) |
| 178 | && $same_values = array_intersect($terms, $old_conditions[ 'categoryarchiveids' ]['exclude']) ){ |
| 179 | $new_conditions[] = array( |
| 180 | 'type' => 'archive_' . $_tax->name , |
| 181 | 'operator' => 'is_not', |
| 182 | 'value' => $same_values |
| 183 | ); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // rewrite single post ids |
| 190 | if( isset ( $old_conditions[ 'postids' ]['ids'] ) |
| 191 | && isset ( $old_conditions[ 'postids' ]['method'] ) |
| 192 | && $old_conditions[ 'postids' ]['method'] |
| 193 | && ( !isset ( $old_conditions[ 'postids' ]['all'] ) |
| 194 | || ! $old_conditions[ 'postids' ]['all'] ) ) { |
| 195 | $operator = ( $old_conditions[ 'postids' ]['method'] === 'exclude' ) ? 'is_not' : 'is'; |
| 196 | if ( is_string( $old_conditions[ 'postids' ]['ids']) ) { |
| 197 | $old_conditions[ 'postids' ]['ids'] = explode( ',', $old_conditions[ 'postids' ]['ids'] ); |
| 198 | } |
| 199 | $new_conditions[] = array( |
| 200 | 'type' => 'postids', |
| 201 | 'operator' => $operator, |
| 202 | 'value' => $old_conditions[ 'postids' ]['ids'] |
| 203 | ); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | error_log(print_r('NEW CONDITIONS', true)); |
| 208 | error_log(print_r($new_conditions, true)); |
| 209 | |
| 210 | $options['conditions'] = $new_conditions; |
| 211 | |
| 212 | // save conditions |
| 213 | update_post_meta( $_ad->ID, $option_key, $options ); |
| 214 | } |
| 215 | |
| 216 | error_log(print_r('up to 1.7', true)); |
| 217 | } |
| 218 | |
| 219 | } |