abstract-importer.php
3 months ago
class-ad-inserter.php
3 months ago
class-ads-wp-ads.php
3 months ago
class-amp-wp-ads.php
3 months ago
class-api-ads.php
1 year ago
class-google-sheet.php
5 months ago
class-manager.php
1 year ago
class-plugin-exporter.php
1 week ago
class-quick-adsense.php
1 year ago
class-tutorials.php
1 year ago
class-wp-quads.php
1 year ago
class-xml-encoder.php
1 year ago
class-xml-importer.php
5 days ago
class-plugin-exporter.php
392 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin exporter. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.50.0 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Importers; |
| 11 | |
| 12 | use WP_Error; |
| 13 | use Exception; |
| 14 | use XML_Encoder; |
| 15 | use AdvancedAds\Options; |
| 16 | use Advanced_Ads_Privacy; |
| 17 | use AdvancedAds\Constants; |
| 18 | use Advanced_Ads_Ads_Txt_Strategy; |
| 19 | use AdvancedAds\Ads\Ad_Repository; |
| 20 | use AdvancedAds\Utilities\Conditional; |
| 21 | use AdvancedAds\Framework\Utilities\Params; |
| 22 | |
| 23 | defined( 'ABSPATH' ) || exit; |
| 24 | |
| 25 | /** |
| 26 | * Plugin exporter. |
| 27 | * |
| 28 | * phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged |
| 29 | * phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 30 | * phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_print_r |
| 31 | * phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_var_export |
| 32 | */ |
| 33 | class Plugin_Exporter { |
| 34 | /** |
| 35 | * Hold data to make export file |
| 36 | * |
| 37 | * @var array |
| 38 | */ |
| 39 | public $data = []; |
| 40 | |
| 41 | /** |
| 42 | * Types of content to be exported. |
| 43 | * |
| 44 | * @var array |
| 45 | */ |
| 46 | public $options = false; |
| 47 | |
| 48 | /** |
| 49 | * Download export file |
| 50 | * |
| 51 | * @return array|string|WP_Error |
| 52 | */ |
| 53 | public function download_file() { |
| 54 | if ( ! Conditional::user_can( 'advanced_ads_manage_options' ) ) { |
| 55 | return new WP_Error( 'no_permission', __( 'User dont have premission to export.', 'advanced-ads' ) ); |
| 56 | } |
| 57 | |
| 58 | $this->options = Params::post( 'content', false, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); |
| 59 | if ( empty( $this->options ) ) { |
| 60 | return new WP_Error( 'no_option_selected', __( 'No content option selected to export.', 'advanced-ads' ) ); |
| 61 | } |
| 62 | |
| 63 | $this->process(); |
| 64 | |
| 65 | if ( ! empty( $this->data ) ) { |
| 66 | if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) { |
| 67 | error_log( print_r( 'Array to decode', true ) ); |
| 68 | error_log( print_r( $this->data, true ) ); |
| 69 | } |
| 70 | |
| 71 | $filename = $this->get_filename(); |
| 72 | |
| 73 | try { |
| 74 | $encoded = XML_Encoder::get_instance()->encode( |
| 75 | $this->data, |
| 76 | [ 'encoding' => get_option( 'blog_charset' ) ] |
| 77 | ); |
| 78 | |
| 79 | header( 'Content-Description: File Transfer' ); |
| 80 | header( 'Content-Disposition: attachment; filename=' . $filename ); |
| 81 | header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true ); |
| 82 | echo $encoded; // phpcs:ignore |
| 83 | |
| 84 | if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) { |
| 85 | error_log( print_r( $encoded, true ) ); |
| 86 | $decoded = XML_Encoder::get_instance()->decode( $encoded ); |
| 87 | error_log( 'result ' . var_export( $this->data === $decoded, true ) ); |
| 88 | } |
| 89 | |
| 90 | exit(); |
| 91 | |
| 92 | } catch ( Exception $e ) { |
| 93 | return new WP_Error( 'error', $e->getMessage() ); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Generate XML file |
| 102 | * |
| 103 | * @return void |
| 104 | */ |
| 105 | private function process(): void { |
| 106 | @set_time_limit( 0 ); |
| 107 | @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) ); // phpcs:ignore WordPress.PHP.IniSet.memory_limit_Disallowed |
| 108 | |
| 109 | foreach ( $this->options as $option ) { |
| 110 | $method = "process_{$option}"; |
| 111 | if ( method_exists( $this, $method ) ) { |
| 112 | $this->$method(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | do_action_ref_array( 'advanced-ads-export', [ $this->options, &$this->data ] ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Process ads |
| 121 | * |
| 122 | * @return void |
| 123 | */ |
| 124 | private function process_ads(): void { |
| 125 | $ads = []; |
| 126 | $mime_types = $this->get_mime_types(); |
| 127 | $search = '/' . preg_quote( home_url(), '/' ) . '(\S+?)\.(' . implode( '|', array_keys( $mime_types ) ) . ')/i'; |
| 128 | |
| 129 | $posts = $this->get_posts( Constants::POST_TYPE_AD ); |
| 130 | |
| 131 | if ( ! empty( $posts ) ) { |
| 132 | update_meta_cache( 'post', wp_list_pluck( $posts, 'ID' ) ); |
| 133 | } |
| 134 | |
| 135 | foreach ( $posts as $index => $post ) { |
| 136 | if ( ! empty( $post['post_content'] ) ) { |
| 137 | // Wrap images in <advads_import_img></advads_import_img> tags. |
| 138 | $post['post_content'] = preg_replace( $search, '<advads_import_img>\\0</advads_import_img>', $post['post_content'] ); |
| 139 | } |
| 140 | |
| 141 | if ( in_array( 'groups', $this->options, true ) ) { |
| 142 | $group_ids = get_post_meta( $post['ID'], Constants::AD_META_GROUP_IDS, true ); |
| 143 | |
| 144 | if ( is_array( $group_ids ) && ! empty( $group_ids ) ) { |
| 145 | $post['groups'] = []; |
| 146 | foreach ( $group_ids as $group_id ) { |
| 147 | $post['groups'][] = $this->get_group( (int) $group_id ); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | $this->get_post_meta( $post ); |
| 153 | |
| 154 | $ads[] = $post; |
| 155 | } |
| 156 | |
| 157 | if ( $ads ) { |
| 158 | $this->data['ads'] = $ads; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Process placements |
| 164 | * |
| 165 | * @return void |
| 166 | */ |
| 167 | private function process_placements(): void { |
| 168 | $placements = []; |
| 169 | $posts = $this->get_posts( Constants::POST_TYPE_PLACEMENT ); |
| 170 | foreach ( $posts as $index => $post ) { |
| 171 | $this->get_post_meta( $post ); |
| 172 | |
| 173 | $placements[] = $post; |
| 174 | } |
| 175 | |
| 176 | if ( $placements ) { |
| 177 | $this->data['placements'] = $placements; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Process groups |
| 183 | * |
| 184 | * @return void |
| 185 | */ |
| 186 | private function process_groups(): void { |
| 187 | $this->data['groups'] = []; |
| 188 | foreach ( wp_advads_get_groups_dropdown() as $term_id => $name ) { |
| 189 | $this->data['groups'][] = $this->get_group( $term_id ); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Process options |
| 195 | * |
| 196 | * @return void |
| 197 | */ |
| 198 | private function process_options(): void { |
| 199 | /** |
| 200 | * Filters the list of options to be exported. |
| 201 | * |
| 202 | * @param $options An array of options |
| 203 | */ |
| 204 | $this->data['options'] = array_filter( |
| 205 | apply_filters( |
| 206 | 'advanced-ads-export-options', |
| 207 | [ |
| 208 | ADVADS_SLUG => get_option( ADVADS_SLUG ), |
| 209 | GADSENSE_OPT_NAME => get_option( GADSENSE_OPT_NAME ), |
| 210 | Advanced_Ads_Privacy::OPTION_KEY => get_option( Advanced_Ads_Privacy::OPTION_KEY ), |
| 211 | Advanced_Ads_Ads_Txt_Strategy::OPTION => get_option( Advanced_Ads_Ads_Txt_Strategy::OPTION ), |
| 212 | Constants::OPTION_ADBLOCKER_SETTINGS => Options::instance()->get( 'adblocker', [] ), |
| 213 | ] |
| 214 | ) |
| 215 | ); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Get the filename |
| 220 | * |
| 221 | * @return string |
| 222 | */ |
| 223 | private function get_filename(): string { |
| 224 | return sprintf( |
| 225 | '%s-advanced-ads-export-%s.xml', |
| 226 | sanitize_title( |
| 227 | preg_replace( |
| 228 | '#^(?:[^:]+:)?//(?:www\.)?([^/]+)#', |
| 229 | '$1', |
| 230 | get_bloginfo( 'url' ) |
| 231 | ) |
| 232 | ), |
| 233 | gmdate( 'Y-m-d' ) |
| 234 | ); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Get group info |
| 239 | * |
| 240 | * @param int $group_id Group id. |
| 241 | * |
| 242 | * @return array |
| 243 | */ |
| 244 | private function get_group( $group_id ): array { |
| 245 | static $advads_groups; |
| 246 | if ( null === $advads_groups ) { |
| 247 | $advads_groups = []; |
| 248 | } |
| 249 | |
| 250 | if ( ! isset( $advads_groups[ $group_id ] ) ) { |
| 251 | $group = wp_advads_get_group( $group_id ); |
| 252 | |
| 253 | $advads_groups[ $group_id ] = [ |
| 254 | 'term_id' => $group->get_id(), |
| 255 | 'slug' => $group->get_slug(), |
| 256 | 'name' => $group->get_name(), |
| 257 | 'type' => $group->get_type(), |
| 258 | 'ad_count' => $group->get_ad_count(), |
| 259 | 'options' => $group->get_options(), |
| 260 | 'weight' => $group->get_ad_weights(), |
| 261 | ]; |
| 262 | } |
| 263 | |
| 264 | return $advads_groups[ $group_id ]; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Get post IDs to export from cached summaries. |
| 269 | * |
| 270 | * @param string $post_type Post type to fetch. |
| 271 | * |
| 272 | * @return int[] |
| 273 | */ |
| 274 | private function get_export_post_ids( $post_type ): array { |
| 275 | if ( Constants::POST_TYPE_AD === $post_type ) { |
| 276 | return array_map( 'absint', array_keys( wp_advads_get_ad_summaries() ) ); |
| 277 | } |
| 278 | |
| 279 | if ( Constants::POST_TYPE_PLACEMENT === $post_type ) { |
| 280 | return array_map( 'absint', array_keys( wp_advads_get_placement_summaries() ) ); |
| 281 | } |
| 282 | |
| 283 | return []; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Get posts for export |
| 288 | * |
| 289 | * @param string $post_type Post type to fetch. |
| 290 | * |
| 291 | * @return array |
| 292 | */ |
| 293 | private function get_posts( $post_type ): array { |
| 294 | global $wpdb; |
| 295 | |
| 296 | $post_ids = $this->get_export_post_ids( $post_type ); |
| 297 | |
| 298 | if ( empty( $post_ids ) ) { |
| 299 | return []; |
| 300 | } |
| 301 | |
| 302 | $export_fields = implode( |
| 303 | ', ', |
| 304 | [ |
| 305 | 'ID', |
| 306 | 'post_date', |
| 307 | 'post_date_gmt', |
| 308 | 'post_content', |
| 309 | 'post_title', |
| 310 | 'post_password', |
| 311 | 'post_name', |
| 312 | 'post_status', |
| 313 | 'post_modified', |
| 314 | 'post_modified_gmt', |
| 315 | 'guid', |
| 316 | ] |
| 317 | ); |
| 318 | |
| 319 | $placeholders = implode( ', ', array_fill( 0, count( $post_ids ), '%d' ) ); |
| 320 | |
| 321 | // phpcs:disable |
| 322 | return $wpdb->get_results( |
| 323 | $wpdb->prepare( |
| 324 | "SELECT $export_fields FROM {$wpdb->posts} WHERE ID IN ($placeholders) AND post_status NOT IN ('trash', 'auto-draft')", |
| 325 | ...$post_ids |
| 326 | ), |
| 327 | ARRAY_A |
| 328 | ); |
| 329 | // phpcs:enable |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Get mime types |
| 334 | * |
| 335 | * @return array |
| 336 | */ |
| 337 | private function get_mime_types(): array { |
| 338 | static $mime_types; |
| 339 | |
| 340 | if ( null === $mime_types ) { |
| 341 | $mime_types = array_filter( |
| 342 | get_allowed_mime_types(), |
| 343 | function ( $mime_type ) { |
| 344 | return preg_match( '/image\//', $mime_type ); |
| 345 | } |
| 346 | ); |
| 347 | } |
| 348 | |
| 349 | return $mime_types; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Get ads meta |
| 354 | * |
| 355 | * @param array $post Post object array. |
| 356 | * |
| 357 | * @return void |
| 358 | */ |
| 359 | private function get_post_meta( &$post ) { |
| 360 | global $wpdb; |
| 361 | |
| 362 | $postmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->postmeta} WHERE post_id = %d", absint( $post['ID'] ) ) ); // phpcs:ignore |
| 363 | foreach ( $postmeta as $meta ) { |
| 364 | if ( '_edit_lock' === $meta->meta_key ) { |
| 365 | continue; |
| 366 | } |
| 367 | |
| 368 | if ( Ad_Repository::OPTION_METAKEY === $meta->meta_key ) { |
| 369 | $image_id = false; |
| 370 | $ad_options = maybe_unserialize( $meta->meta_value ); |
| 371 | if ( isset( $ad_options['output']['image_id'] ) ) { |
| 372 | $image_id = absint( $ad_options['output']['image_id'] ); |
| 373 | } |
| 374 | if ( isset( $ad_options['image_id'] ) ) { |
| 375 | $image_id = absint( $ad_options['image_id'] ); |
| 376 | } |
| 377 | |
| 378 | if ( $image_id ) { |
| 379 | $atached_img = wp_get_attachment_url( $image_id ); |
| 380 | if ( $atached_img ) { |
| 381 | $post['attached_img_url'] = $atached_img; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | $post['meta_input'][ $meta->meta_key ] = maybe_unserialize( $ad_options ); |
| 386 | } else { |
| 387 | $post['meta_input'][ $meta->meta_key ] = maybe_unserialize( $meta->meta_value ); |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 |