| 1 |
<?php |
| 2 |
/** |
| 3 |
* Content Control Migrations |
| 4 |
* |
| 5 |
* @package ContentControl\Plugin |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace ContentControl\Upgrades; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
use function __; |
| 13 |
use function add_post_meta; |
| 14 |
use function wp_parse_args; |
| 15 |
use function wp_insert_post; |
| 16 |
use function ContentControl\remap_conditions_to_query; |
| 17 |
use function ContentControl\get_default_restriction_settings; |
| 18 |
|
| 19 |
/** |
| 20 |
* Restrictions v2 migration. |
| 21 |
*/ |
| 22 |
class Restrictions_2 extends \ContentControl\Base\Upgrade { |
| 23 |
|
| 24 |
const TYPE = 'restrictions'; |
| 25 |
const VERSION = 2; |
| 26 |
|
| 27 |
/** |
| 28 |
* Get the label for the upgrade. |
| 29 |
* |
| 30 |
* @return string |
| 31 |
*/ |
| 32 |
public function label() { |
| 33 |
return __( 'Update global restrictions', 'content-control' ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Get the dependencies for this upgrade. |
| 38 |
* |
| 39 |
* @return string[] |
| 40 |
*/ |
| 41 |
public function get_dependencies() { |
| 42 |
return [ |
| 43 |
'backup-2', |
| 44 |
]; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Run the migration. |
| 49 |
* |
| 50 |
* @return void|\WP_Error|bool |
| 51 |
*/ |
| 52 |
public function run() { |
| 53 |
// Gets a stream or mock stream for sending events. |
| 54 |
$stream = $this->stream(); |
| 55 |
|
| 56 |
// Get settings (where restrictions were store before). |
| 57 |
$settings = \get_option( 'jp_cc_settings', [] ); |
| 58 |
|
| 59 |
if ( ! is_array( $settings ) || |
| 60 |
empty( $settings ) || |
| 61 |
! isset( $settings['restrictions'] ) || |
| 62 |
empty( $settings['restrictions'] ) |
| 63 |
) { |
| 64 |
$stream->complete_task( __( 'No restrictions to migrate.', 'content-control' ) ); |
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
$restrictions = $settings['restrictions']; |
| 69 |
$count = count( $restrictions ); |
| 70 |
|
| 71 |
if ( $count ) { |
| 72 |
$stream->start_task( |
| 73 |
// translators: %d: number of restrictions to migrate. |
| 74 |
sprintf( __( 'Migrating %d restrictions', 'content-control' ), $count ), |
| 75 |
$count |
| 76 |
); |
| 77 |
|
| 78 |
$progress = 0; |
| 79 |
|
| 80 |
foreach ( $restrictions as $key => $restriction ) { |
| 81 |
if ( $this->migrate_restriction( $restriction ) ) { |
| 82 |
++$progress; |
| 83 |
$stream->update_task_progress( $progress ); |
| 84 |
|
| 85 |
// Unset any restrictions that were migrated. |
| 86 |
unset( $restrictions[ $key ] ); |
| 87 |
} else { |
| 88 |
$stream->send_event( |
| 89 |
'task:error', |
| 90 |
[ |
| 91 |
// translators: %s: restriction title. |
| 92 |
'message' => sprintf( __( 'Failed to migrate restriction "%s".', 'content-control' ), $restriction['title'] ), |
| 93 |
'restriction' => $restriction, |
| 94 |
] |
| 95 |
); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
if ( empty( $restrictions ) ) { |
| 101 |
unset( $settings['restrictions'] ); |
| 102 |
} else { |
| 103 |
$settings['restrictions'] = $restrictions; |
| 104 |
} |
| 105 |
|
| 106 |
// Update the settings with any migrated restrictions removed so they can later be migrated. |
| 107 |
\update_option( 'jp_cc_settings', $settings ); |
| 108 |
|
| 109 |
if ( ! $count || $progress === $count ) { |
| 110 |
// translators: %d: number of restrictions migrated. |
| 111 |
$stream->complete_task( sprintf( __( '%d restrictions migrated', 'content-control' ), $count ) ); |
| 112 |
} else { |
| 113 |
$stream->send_event( |
| 114 |
'task:error', |
| 115 |
[ |
| 116 |
// translators: %d: number of restrictions that failed to migrate. |
| 117 |
'message' => sprintf( __( 'Failed to migrate %d restrictions.', 'content-control' ), count( $settings['restrictions'] ) ), |
| 118 |
] |
| 119 |
); |
| 120 |
|
| 121 |
// Prevent marking this upgrade as complete. |
| 122 |
return false; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Migrate a given restriction to the new post type. |
| 128 |
* |
| 129 |
* @param array<string,mixed> $restriction Restriction data. |
| 130 |
* |
| 131 |
* @return bool True if successful, false otherwise. |
| 132 |
*/ |
| 133 |
public function migrate_restriction( $restriction ) { |
| 134 |
static $priority = 0; |
| 135 |
|
| 136 |
$restriction = wp_parse_args( $restriction, [ |
| 137 |
'title' => '', |
| 138 |
'who' => '', |
| 139 |
'roles' => [], |
| 140 |
'protection_method' => 'redirect', |
| 141 |
'show_excerpts' => false, |
| 142 |
'override_default_message' => false, |
| 143 |
'custom_message' => '', |
| 144 |
'redirect_type' => 'login', |
| 145 |
'redirect_url' => '', |
| 146 |
'conditions' => '', |
| 147 |
]); |
| 148 |
|
| 149 |
$new_restriction_id = wp_insert_post( |
| 150 |
[ |
| 151 |
'post_type' => 'cc_restriction', |
| 152 |
'post_title' => $restriction['title'], |
| 153 |
'post_content' => $restriction['custom_message'], |
| 154 |
'post_status' => 'publish', |
| 155 |
'menu_order' => $priority++, // Maintain order. |
| 156 |
] |
| 157 |
); |
| 158 |
|
| 159 |
// Convert from associative to indexed array. |
| 160 |
$user_roles = is_array( $restriction['roles'] ) ? array_values( $restriction['roles'] ) : []; |
| 161 |
|
| 162 |
$settings = wp_parse_args( [ |
| 163 |
'userStatus' => $restriction['who'], |
| 164 |
'roleMatch' => count( $user_roles ) > 0 ? 'match' : 'any', |
| 165 |
'userRoles' => $user_roles, |
| 166 |
'protectionMethod' => 'custom_message' === $restriction['protection_method'] ? 'replace' : 'redirect', |
| 167 |
'showExcerpts' => $restriction['show_excerpts'], |
| 168 |
'overrideMessage' => $restriction['override_default_message'], |
| 169 |
'customMessage' => $restriction['custom_message'], |
| 170 |
'redirectType' => $restriction['redirect_type'], |
| 171 |
'redirectUrl' => esc_url_raw( $restriction['redirect_url'] ), |
| 172 |
'conditions' => remap_conditions_to_query( $restriction['conditions'] ), |
| 173 |
], get_default_restriction_settings() ); |
| 174 |
|
| 175 |
$added_meta = add_post_meta( $new_restriction_id, 'restriction_settings', $settings, true ); |
| 176 |
|
| 177 |
return $new_restriction_id > 0 && $added_meta; |
| 178 |
} |
| 179 |
} |
| 180 |
|