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