index.php
1 year ago
upgrade-1.48.4.php
1 year ago
upgrade-1.48.5.php
1 year ago
upgrade-1.52.1.php
5 days ago
upgrade-2.0.0.php
1 year ago
upgrade-2.0.8.php
1 week ago
upgrade-2.0.9.php
5 days ago
upgrade-2.1.0.php
2 days ago
upgrade-2.0.0.php
135 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Update routine |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 2.0.0 |
| 8 | */ |
| 9 | |
| 10 | use AdvancedAds\Constants; |
| 11 | |
| 12 | /** |
| 13 | * Check for existing placements in posts. |
| 14 | * |
| 15 | * @param array $slugs Slugs to check. |
| 16 | * |
| 17 | * @return array |
| 18 | */ |
| 19 | function advads_get_existing_placement_posts( $slugs ): array { |
| 20 | global $wpdb; |
| 21 | |
| 22 | foreach ( $slugs as &$slug ) { |
| 23 | $slug = sanitize_title_for_query( $slug ); |
| 24 | $slug = str_replace( '_', '-', $slug ); |
| 25 | $slug = esc_sql( $slug ); |
| 26 | } |
| 27 | |
| 28 | $in_string = "'" . implode( "','", $slugs ) . "'"; |
| 29 | |
| 30 | // phpcs:disable |
| 31 | $posts = $wpdb->get_results( |
| 32 | $wpdb->prepare( |
| 33 | " |
| 34 | SELECT ID, post_name |
| 35 | FROM $wpdb->posts |
| 36 | WHERE post_name IN ($in_string) |
| 37 | AND post_type = %s |
| 38 | ", |
| 39 | Constants::POST_TYPE_PLACEMENT |
| 40 | ) |
| 41 | ); |
| 42 | // phpcs:enable |
| 43 | |
| 44 | return empty( $posts ) ? [] : wp_list_pluck( $posts, 'ID', 'post_name' ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Save a placement as post. |
| 49 | * |
| 50 | * @param array $data Placement data array. |
| 51 | * |
| 52 | * @return int|WP_Error |
| 53 | */ |
| 54 | function advads_save_new_placement( $data ) { |
| 55 | // Normalize options. |
| 56 | $options = $data['options'] ?? []; |
| 57 | $display_conditions = $options['placement_conditions']['display'] ?? []; |
| 58 | $visitor_conditions = $options['placement_conditions']['visitors'] ?? []; |
| 59 | unset( $options['placement_conditions'] ); |
| 60 | |
| 61 | // Find placement type. |
| 62 | $placement_type = empty( $data['type'] ) ? 'default' : $data['type']; |
| 63 | if ( ! wp_advads_has_placement_type( $placement_type ) ) { |
| 64 | wp_advads_create_placement_type( $placement_type ); |
| 65 | } |
| 66 | |
| 67 | $placement = wp_advads_create_new_placement( $placement_type ); |
| 68 | $placement->set_type( $data['type'] ); |
| 69 | $placement->set_item( $data['item'] ); |
| 70 | $placement->set_slug( $data['slug'] ); |
| 71 | $placement->set_title( $data['name'] ); |
| 72 | $placement->set_status( 'publish' ); |
| 73 | $placement->set_display_conditions( $display_conditions ); |
| 74 | $placement->set_visitor_conditions( $visitor_conditions ); |
| 75 | $placement->set_props( $options ); |
| 76 | |
| 77 | $placement->save(); |
| 78 | |
| 79 | return $placement->get_id(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Backup old Placements |
| 84 | * |
| 85 | * @param array $placements Placements. |
| 86 | * |
| 87 | * @return void |
| 88 | */ |
| 89 | function advads_upgrade_2_0_0_make_backup( $placements ): void { |
| 90 | $backup_key = 'advads-ads-placements_backup'; |
| 91 | |
| 92 | if ( false !== get_option( $backup_key ) ) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | update_option( $backup_key, $placements ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Migrate placements from options to custom post type. |
| 101 | * |
| 102 | * @since 2.0.0 |
| 103 | * |
| 104 | * @return void |
| 105 | */ |
| 106 | function advads_upgrade_2_0_0_placement_migration(): void { |
| 107 | $option_key = 'advads-ads-placements'; |
| 108 | $placements = get_option( $option_key, [] ); |
| 109 | |
| 110 | // Early bail!! |
| 111 | if ( ! is_array( $placements ) || empty( $placements ) ) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | advads_upgrade_2_0_0_make_backup( $placements ); |
| 116 | $existing_posts = advads_get_existing_placement_posts( array_keys( $placements ) ); |
| 117 | |
| 118 | foreach ( $placements as $slug => $placement ) { |
| 119 | if ( ! is_array( $placement ) || array_key_exists( $slug, $existing_posts ) ) { |
| 120 | continue; |
| 121 | } |
| 122 | |
| 123 | $post_id = advads_save_new_placement( array_merge( $placement, [ 'slug' => $slug ] ) ); |
| 124 | if ( 0 === $post_id ) { |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | unset( $placements[ $slug ] ); |
| 129 | } |
| 130 | |
| 131 | update_option( $option_key, $placements ); |
| 132 | } |
| 133 | |
| 134 | advads_upgrade_2_0_0_placement_migration(); |
| 135 |