| 1 |
<?php |
| 2 |
/** |
| 3 |
* A WP_CLI set of subcommands to list and update WP-Members plugin settings. |
| 4 |
* |
| 5 |
* @since 3.3.5 |
| 6 |
*/ |
| 7 |
class WP_Members_CLI_Settings { |
| 8 |
|
| 9 |
/** |
| 10 |
* Initialize any required elements. |
| 11 |
* |
| 12 |
* @since 3.3.5 |
| 13 |
* |
| 14 |
* @global object $wpmem |
| 15 |
*/ |
| 16 |
public function __construct() { |
| 17 |
// WP-Members admin needs to be loaded manually. |
| 18 |
global $wpmem; |
| 19 |
if ( ! isset( $wpmem->admin ) ) { |
| 20 |
$wpmem->load_admin(); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* List the WP-Members content settings. |
| 26 |
* |
| 27 |
* @since 3.3.5 |
| 28 |
*/ |
| 29 |
public function content() { |
| 30 |
$this->list_settings( array( 'content' ), array() ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* List the WP-Members option settings. |
| 35 |
* |
| 36 |
* @since 3.3.5 |
| 37 |
*/ |
| 38 |
public function options() { |
| 39 |
$this->list_settings( array( 'options' ), array() ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Lists WP-Members settings. |
| 44 |
* |
| 45 |
* @since 3.3.5 |
| 46 |
* |
| 47 |
* @param array $args |
| 48 |
* @param array $assoc_args |
| 49 |
*/ |
| 50 |
private function list_settings( $args, $assoc_args ) { |
| 51 |
|
| 52 |
global $wpmem; |
| 53 |
|
| 54 |
if ( 'content' == $args[0] ) { |
| 55 |
$settings = $this->settings( 'content' ); |
| 56 |
} else { |
| 57 |
$settings = $this->settings( 'options' ); |
| 58 |
} |
| 59 |
if ( 'content' == $args[0] ) { |
| 60 |
|
| 61 |
// @todo Add custom post types, and look for admin where all possible post types are assembled. |
| 62 |
$post_types = array_merge( array( 'post', 'page' ), $wpmem->admin->post_types() ); |
| 63 |
|
| 64 |
foreach( $post_types as $post_type ) { |
| 65 |
foreach ( $settings as $setting => $description ) { |
| 66 |
if ( 'autoex' != $setting ) { |
| 67 |
$list[] = array( |
| 68 |
'Setting' => $setting . ' ' . $post_type, |
| 69 |
'Description' => $description . ' ' . $post_type, |
| 70 |
'Value' => $wpmem->{$setting}[ $post_type ], |
| 71 |
'Option' => ( 0 == $wpmem->{$setting}[ $post_type ] ) ? 'Disabled' : 'Enabled', |
| 72 |
); |
| 73 |
} else { |
| 74 |
$list[] = array( |
| 75 |
'Setting' => $setting . ' ' . $post_type, |
| 76 |
'Description' => $description . ' ' . $post_type, |
| 77 |
'Value' => $wpmem->{$setting}[ $post_type ]['enabled'], |
| 78 |
'Option' => ( 0 == $wpmem->{$setting}[ $post_type ]['enabled'] ) ? 'Disabled' : 'Enabled', |
| 79 |
); |
| 80 |
$list[] = array( |
| 81 |
'Setting' => '', |
| 82 |
'Description' => $post_type . ' excerpt word length:', |
| 83 |
'Value' => $wpmem->{$setting}[ $post_type ]['length'], |
| 84 |
'Option' => '', |
| 85 |
); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
$list[] = array( 'Setting' => '', 'Description' => '', 'Value' => '', 'Option' => '' ); |
| 90 |
} |
| 91 |
|
| 92 |
} else { |
| 93 |
foreach ( $settings as $setting => $description ) { |
| 94 |
if ( 'captcha' == $setting ) { |
| 95 |
$option = WP_Members_Captcha::type( $wpmem->{$setting} ); |
| 96 |
} else { |
| 97 |
$option = ( 0 == $wpmem->{$setting} ) ? 'Disabled' : 'Enabled'; |
| 98 |
} |
| 99 |
$list[] = array( |
| 100 |
'Setting' => $setting, |
| 101 |
'Description' => $description, |
| 102 |
'Value' => $wpmem->{$setting}, |
| 103 |
'Option' => $option, |
| 104 |
); |
| 105 |
|
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'Description', 'Setting', 'Value', 'Option' ) ); |
| 110 |
$formatter->display_items( $list ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* List custom post types for WP-Members management. |
| 115 |
* |
| 116 |
* @since 3.3.5 |
| 117 |
* |
| 118 |
* @subcommand post-types |
| 119 |
*/ |
| 120 |
public function post_types() { |
| 121 |
global $wpmem; |
| 122 |
$post_types = $wpmem->admin->post_types(); |
| 123 |
if ( empty( $post_types ) ) { |
| 124 |
WP_CLI::line( 'No custom post types are available for WP-Members settings' ); |
| 125 |
} else { |
| 126 |
foreach ( $post_types as $post_type ) { |
| 127 |
$enabled = ( array_key_exists( $post_type, $wpmem->post_types ) ) ? "Enabled" : "Disabled"; |
| 128 |
$list[] = array( |
| 129 |
'Post Type' => $post_type, |
| 130 |
'Value' => $enabled, |
| 131 |
); |
| 132 |
} |
| 133 |
WP_CLI::line( 'Custom post type settings for WP-Members:' ); |
| 134 |
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'Post Type', 'Value' ) ); |
| 135 |
$formatter->display_items( $list ); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Manage post type. |
| 141 |
* |
| 142 |
* ## OPTIONS |
| 143 |
* |
| 144 |
* [--enable=<post_type>] |
| 145 |
* : enable the specified post type. |
| 146 |
* |
| 147 |
* [--disable=<post_type>] |
| 148 |
* : disable the specified post type. |
| 149 |
* |
| 150 |
* @since 3.3.5 |
| 151 |
* |
| 152 |
* @subcommand post-type |
| 153 |
*/ |
| 154 |
public function post_type( $args, $assoc_args ) { |
| 155 |
global $wpmem; |
| 156 |
if ( isset( $assoc_args['enable'] ) || isset( $assoc_args['disable'] ) ) { |
| 157 |
$post_types = $wpmem->admin->post_types(); |
| 158 |
if ( ( isset( $assoc_args['enable'] ) && ! array_key_exists( $assoc_args['enable'], $post_types ) ) || ( isset( $assoc_args['disable'] ) && ! array_key_exists( $assoc_args['disable'], $post_types ) ) ) { |
| 159 |
WP_CLI::error( 'Not an available post type. Try [wp mem settings post_types]' ); |
| 160 |
} |
| 161 |
// Handle disable. |
| 162 |
if ( isset( $assoc_args['disable'] ) ) { |
| 163 |
unset( $wpmem->post_types[ $assoc_args['disable'] ] ); |
| 164 |
wpmem_update_option( 'wpmembers_settings', 'post_types', $wpmem->post_types, true ); |
| 165 |
WP_CLI::success( sprintf( 'Disabled %s post type.', $assoc_args['disable'] ) ); |
| 166 |
} |
| 167 |
if ( isset( $assoc_args['enable'] ) ) { |
| 168 |
$cpt_obj = get_post_type_object( $assoc_args['enable'] ); |
| 169 |
$new_post_types = array_merge($wpmem->post_types, array( $cpt_obj->name => $cpt_obj->labels->name ) ); |
| 170 |
wpmem_update_option( 'wpmembers_settings', 'post_types', $new_post_types, true ); |
| 171 |
WP_CLI::success( sprintf( 'Enabled %s post type.', $assoc_args['enable'] ) ); |
| 172 |
} |
| 173 |
} else { |
| 174 |
WP_CLI::error( 'Must specify an option: --enable=<post_type> or --disable=<post_type>' ); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Enable a WP-Members setting. |
| 180 |
* |
| 181 |
* ## OPTIONS |
| 182 |
* |
| 183 |
* <option> |
| 184 |
* : The WP-Members option setting to enable. |
| 185 |
* --- |
| 186 |
* options: |
| 187 |
* - notify |
| 188 |
* - mod_reg |
| 189 |
* - act_link |
| 190 |
* - optin |
| 191 |
* - captcha |
| 192 |
* - warnings |
| 193 |
* - dropins |
| 194 |
* - enable_products |
| 195 |
* - clone_menus |
| 196 |
* --- |
| 197 |
* |
| 198 |
* [<captcha_type>] |
| 199 |
* : If enabling captcha, include the captcha type |
| 200 |
* --- |
| 201 |
* default: recaptcha_v3 |
| 202 |
* options: |
| 203 |
* - recaptcha_v3 |
| 204 |
* - recaptcha_v2 |
| 205 |
* - rs_captcha |
| 206 |
* - hcaptcha |
| 207 |
* |
| 208 |
* ## EXAMPLES |
| 209 |
* |
| 210 |
* wp mem settings enable mod_reg |
| 211 |
* |
| 212 |
* @todo Add options to enable content (post type) settings. |
| 213 |
*/ |
| 214 |
public function enable( $args, $assoc_args ) { |
| 215 |
$settings = $this->settings( 'options' ); |
| 216 |
|
| 217 |
if ( 'captcha' == $args[0] ) { |
| 218 |
|
| 219 |
if ( ! isset( $args[1] ) ) { |
| 220 |
WP_CLI::error( 'You must specify captcha type: rs_captcha|recaptcha_v2|recaptcha_v3|hcaptcha' ); |
| 221 |
} |
| 222 |
|
| 223 |
switch( $args[1] ) { |
| 224 |
case 'rs_captcha': |
| 225 |
$which = 2; |
| 226 |
break; |
| 227 |
case 'hcaptcha': |
| 228 |
$which = 5; |
| 229 |
break; |
| 230 |
case 'recaptcha_v2': |
| 231 |
$which = 3; |
| 232 |
break; |
| 233 |
case 'recaptcha_v3': |
| 234 |
default: |
| 235 |
$which = 4; |
| 236 |
break; |
| 237 |
} |
| 238 |
wpmem_update_option( 'wpmembers_settings', $args[0], $which, true ); |
| 239 |
WP_CLI::success( sprintf( '%s %s enabled', $settings[ $args[0] ], $args[1] ) ); |
| 240 |
|
| 241 |
} else { |
| 242 |
wpmem_update_option( 'wpmembers_settings', $args[0], 1, true ); |
| 243 |
WP_CLI::success( sprintf( '%s enabled', $settings[ $args[0] ] ) ); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Disables a WP-Members setting. |
| 249 |
* |
| 250 |
* ## OPTIONS |
| 251 |
* |
| 252 |
* <option> |
| 253 |
* : The WP-Members option setting to disable. |
| 254 |
* --- |
| 255 |
* options: |
| 256 |
* - notify |
| 257 |
* - mod_reg |
| 258 |
* - act_link |
| 259 |
* - optin |
| 260 |
* - captcha |
| 261 |
* - warnings |
| 262 |
* - dropins |
| 263 |
* - enable_products |
| 264 |
* - clone_menus |
| 265 |
* --- |
| 266 |
* |
| 267 |
* ## EXAMPLES |
| 268 |
* |
| 269 |
* wp mem settings disable mod_reg |
| 270 |
* |
| 271 |
* @todo Test for captcha |
| 272 |
* @todo Add options to disable content (post type) settings. |
| 273 |
*/ |
| 274 |
public function disable( $args ) { |
| 275 |
$settings = $this->settings( 'options' ); |
| 276 |
wpmem_update_option( 'wpmembers_settings', $args[0], 0, true ); |
| 277 |
WP_CLI::success( sprintf( '%s disabled', $settings[ $args[0] ] ) ); |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Set, clear, or list the WP-Members page settings. |
| 282 |
* |
| 283 |
* ## OPTIONS |
| 284 |
* |
| 285 |
* <option> |
| 286 |
* : List user pages, clear user pages, or set a page ID for a user page. |
| 287 |
* --- |
| 288 |
* default: list |
| 289 |
* options |
| 290 |
* - list |
| 291 |
* - clear |
| 292 |
* - set |
| 293 |
* --- |
| 294 |
* |
| 295 |
* [--all] |
| 296 |
* : used with <clear> option, clears all pages. |
| 297 |
* |
| 298 |
* [--login[=<ID>]] |
| 299 |
* : Leave empty (--login) to clear, or set a page ID for the login page. |
| 300 |
* |
| 301 |
* [--register[=<ID>]] |
| 302 |
* : Leave empty (--register) to clear, or set a page ID for the registration page. |
| 303 |
* |
| 304 |
* [--profile[=<ID>]] |
| 305 |
* : Leave empty (--profile) to clear, or set a page ID for the profile page. |
| 306 |
* |
| 307 |
* ## EXAMPLES |
| 308 |
* |
| 309 |
* wp mem settings pages clear --all |
| 310 |
* wp mem settings pages clear --register |
| 311 |
* wp mem settings pages set --login=123 |
| 312 |
* wp mem settings pages list |
| 313 |
*/ |
| 314 |
public function pages( $args, $assoc_args ) { |
| 315 |
|
| 316 |
if ( 'clear' == $args[0] ) { |
| 317 |
if ( empty( $assoc_args ) ) { |
| 318 |
WP_CLI::error( 'You must specify --all or --login|register|profile', true ); |
| 319 |
} |
| 320 |
if ( isset( $assoc_args['all'] ) ) { |
| 321 |
unset( $assoc_args['all'] ); |
| 322 |
$assoc_args = array( 'login'=>'', 'register'=>'', 'profile'=>'' ); |
| 323 |
} |
| 324 |
foreach ( $assoc_args as $page => $value ) { |
| 325 |
if ( isset( $assoc_args[ $page ] ) ) { |
| 326 |
wpmem_update_option( 'wpmembers_settings', 'user_pages/' . $page, '', true ); |
| 327 |
WP_CLI::success( sprintf( '%s page cleared', ucfirst( $page ) ) ); |
| 328 |
} |
| 329 |
} |
| 330 |
return; |
| 331 |
} |
| 332 |
if ( 'set' == $args[0] ) { |
| 333 |
if ( empty( $assoc_args ) ) { |
| 334 |
WP_CLI::error( 'You must specify which page(s) to set: --login=<ID>, --register=<ID>, --profile=<ID>', true ); |
| 335 |
} |
| 336 |
foreach ( $assoc_args as $page => $value ) { |
| 337 |
if ( isset( $assoc_args[ $page ] ) ) { |
| 338 |
wpmem_update_option( 'wpmembers_settings', 'user_pages/' . $page, $assoc_args[ $page ], true ); |
| 339 |
WP_CLI::success( sprintf( '%s page set to ID %s', ucfirst( $page ), $assoc_args[ $page ] ) ); |
| 340 |
} |
| 341 |
} |
| 342 |
return; |
| 343 |
} |
| 344 |
if ( 'list' == $args[0] ) { |
| 345 |
|
| 346 |
$raw_settings = get_option( 'wpmembers_settings' ); |
| 347 |
$user_pages = wpmem_user_pages(); |
| 348 |
|
| 349 |
foreach ( $user_pages as $key => $page ) { |
| 350 |
$list[] = array( |
| 351 |
'Page' => ucfirst( $key ), |
| 352 |
'ID' => $raw_settings['user_pages'][ $key ], |
| 353 |
'URL' => $page, |
| 354 |
); |
| 355 |
} |
| 356 |
|
| 357 |
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'Page', 'ID', 'URL' ) ); |
| 358 |
$formatter->display_items( $list ); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
private function settings( $which ) { |
| 363 |
switch ( $which ) { |
| 364 |
case 'content': |
| 365 |
return array( |
| 366 |
'block' => 'Content Restriction', |
| 367 |
'show_excerpt' => 'Show Excerpts', |
| 368 |
'show_login' => 'Show Login Form', |
| 369 |
'show_reg' => 'Show Registration Form', |
| 370 |
'autoex' => 'Auto Excerpt' ); |
| 371 |
break; |
| 372 |
case 'options': |
| 373 |
return array( |
| 374 |
'enable_products' => 'Enable membership products', |
| 375 |
'notify' => 'Notify admin', |
| 376 |
'mod_reg' => 'Moderate registration', |
| 377 |
'act_link' => 'Confirmation link', |
| 378 |
'optin' => 'Opt in to security and updates information', |
| 379 |
'warnings' => 'Ignore warning messages', |
| 380 |
'dropins' => 'Enable dropins', |
| 381 |
'captcha' => 'Enable registration CAPTCHA', |
| 382 |
'clone_menus' => 'Clone menus (deprecated)', |
| 383 |
); |
| 384 |
break; |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
WP_CLI::add_command( 'mem settings', 'WP_Members_CLI_Settings' ); |