cli.php
236 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Implements example command. |
| 5 | */ |
| 6 | class Disable_Comment_Command |
| 7 | { |
| 8 | public $dc_instance; |
| 9 | |
| 10 | public function __construct($dc_instance) |
| 11 | { |
| 12 | $this->dc_instance = $dc_instance; |
| 13 | |
| 14 | $post_types = array_keys($this->dc_instance->get_all_post_types()); |
| 15 | $comment_types = array_keys($this->dc_instance->get_all_comment_types()); |
| 16 | $post_types[] = $comment_types[] = 'all'; |
| 17 | |
| 18 | $disable_synopsis = array( |
| 19 | array( |
| 20 | 'type' => 'assoc', |
| 21 | 'name' => 'types', |
| 22 | 'description' => 'Disable comments from the selected post type(s) only.', |
| 23 | 'optional' => true, |
| 24 | 'options' => $post_types, |
| 25 | ), |
| 26 | array( |
| 27 | 'type' => 'flag', |
| 28 | 'name' => 'xmlrpc', |
| 29 | 'description' => 'Disable Comments via XML-RPC.', |
| 30 | 'optional' => true, |
| 31 | ), |
| 32 | array( |
| 33 | 'type' => 'flag', |
| 34 | 'name' => 'rest-api', |
| 35 | 'description' => 'Disable Comments via REST API.', |
| 36 | 'optional' => true, |
| 37 | ), |
| 38 | array( |
| 39 | 'type' => 'flag', |
| 40 | 'name' => 'add', |
| 41 | 'description' => 'Check specified checkbox in On Specific Post Types.', // check specified checkbox in `On Specific Post Types:` |
| 42 | 'optional' => true, |
| 43 | ), |
| 44 | array( |
| 45 | 'type' => 'flag', |
| 46 | 'name' => 'remove', |
| 47 | 'description' => 'Uncheck specified checkbox in `On Specific Post Types.', // uncheck specified checkbox in `On Specific Post Types:` |
| 48 | 'optional' => true, |
| 49 | ), |
| 50 | array( |
| 51 | 'type' => 'flag', |
| 52 | 'name' => 'disable-avatar', |
| 53 | 'description' => 'This will change Avatar state from your entire site.', // uncheck specified checkbox in `On Specific Post Types:` |
| 54 | 'optional' => true, |
| 55 | ), |
| 56 | ); |
| 57 | if ($this->dc_instance->networkactive){ |
| 58 | $disable_synopsis[] = array( |
| 59 | 'type' => 'assoc', |
| 60 | 'name' => 'extra-post-types', |
| 61 | 'description' => 'If you want to disable comments on other custom post types on the entire network, you can supply a comma-separated list of post types below (use the slug that identifies the post type.', |
| 62 | 'optional' => true, |
| 63 | ); |
| 64 | } |
| 65 | WP_CLI::add_command('disable-comments settings', [$this, 'disable'], [ |
| 66 | 'synopsis' => $disable_synopsis, |
| 67 | 'when' => 'after_wp_load', |
| 68 | 'longdesc' => "## EXAMPLES |
| 69 | wp disable-comments settings --types=post |
| 70 | wp disable-comments settings --types=page --add |
| 71 | wp disable-comments settings --types=attachment --remove |
| 72 | wp disable-comments settings --xmlrpc --rest-api |
| 73 | wp disable-comments settings --xmlrpc=false --rest-api=false ", |
| 74 | ]); |
| 75 | |
| 76 | $delete_synopsis = array( |
| 77 | array( |
| 78 | 'type' => 'assoc', |
| 79 | 'name' => 'types', |
| 80 | 'description' => 'Remove existing comments entries for the selected post type(s) in the database and cannot be reverted without a database backups.', |
| 81 | 'optional' => true, |
| 82 | 'options' => $post_types, |
| 83 | ), |
| 84 | array( |
| 85 | 'type' => 'assoc', |
| 86 | 'name' => 'comment-types', |
| 87 | 'description' => 'Remove existing comment entries for the selected comment type(s) in the database and cannot be reverted without a database backups.', |
| 88 | 'optional' => true, |
| 89 | 'options' => $comment_types, |
| 90 | ), |
| 91 | array( |
| 92 | 'type' => 'flag', |
| 93 | 'name' => 'spam', |
| 94 | 'description' => 'Permanently delete all spam comments on your WordPress website.', |
| 95 | 'optional' => true, |
| 96 | ), |
| 97 | ); |
| 98 | if (!$this->dc_instance->networkactive){ |
| 99 | $delete_synopsis[] = array( |
| 100 | 'type' => 'assoc', |
| 101 | 'name' => 'extra-post-types', |
| 102 | 'description' => 'If you want to disable comments on other custom post types on the entire network, you can supply a comma-separated list of post types below (use the slug that identifies the post type.', |
| 103 | 'optional' => true, |
| 104 | ); |
| 105 | } |
| 106 | WP_CLI::add_command('disable-comments delete', [$this, 'delete'], [ |
| 107 | 'synopsis' => $delete_synopsis, |
| 108 | 'when' => 'after_wp_load', |
| 109 | 'longdesc' => "## EXAMPLES |
| 110 | wp disable-comments delete --types=post,page |
| 111 | wp disable-comments delete --types=post,page --extra-post-types=contact |
| 112 | wp disable-comments delete --comment-types=comment " |
| 113 | ]); |
| 114 | |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Disable Comments on your website. |
| 119 | * |
| 120 | * @when after_wp_load |
| 121 | */ |
| 122 | function disable($args, $assoc_args) |
| 123 | { |
| 124 | $msg = ""; |
| 125 | $disable_comments_settings = array(); |
| 126 | $types = WP_CLI\Utils\get_flag_value($assoc_args, 'types'); |
| 127 | $add = WP_CLI\Utils\get_flag_value($assoc_args, 'add'); |
| 128 | $remove = WP_CLI\Utils\get_flag_value($assoc_args, 'remove'); |
| 129 | $extra_post_types = WP_CLI\Utils\get_flag_value($assoc_args, 'extra-post-types'); |
| 130 | $remove_xmlrpc_comments = WP_CLI\Utils\get_flag_value($assoc_args, 'xmlrpc'); |
| 131 | $remove_rest_API_comments = WP_CLI\Utils\get_flag_value($assoc_args, 'rest-api'); |
| 132 | $disable_avatar = WP_CLI\Utils\get_flag_value($assoc_args, 'disable-avatar'); |
| 133 | |
| 134 | if ($types === 'all') { |
| 135 | $disable_comments_settings['mode'] = 'remove_everywhere'; |
| 136 | $msg .= __( 'Comments is disabled everywhere. ', 'disable-comments' ); |
| 137 | } elseif(!empty($types) ) { |
| 138 | $disable_comments_settings['mode'] = 'selected_types'; |
| 139 | $_types = array_map('trim', explode(',', $types)); |
| 140 | $disabled_post_types = $this->dc_instance->get_disabled_post_types(); |
| 141 | // translators: %s: post types to be disabled |
| 142 | $new_msg = sprintf( __( 'Comments disabled for %s. ', 'disable-comments' ), $types ); |
| 143 | if(!empty($add)){ |
| 144 | $_types = array_unique(array_merge($disabled_post_types, $_types)); |
| 145 | // translators: %s: post types to be disabled |
| 146 | $new_msg = sprintf( __( 'Comments disabled for %s. ', 'disable-comments' ), $types ); |
| 147 | } |
| 148 | if(!empty($remove)){ |
| 149 | $_types = array_diff($disabled_post_types, $_types); |
| 150 | // translators: %s: post types to be enabled |
| 151 | $new_msg = sprintf( __( 'Comments enabled for %s. ', 'disable-comments' ), $types ); |
| 152 | } |
| 153 | |
| 154 | $msg = $new_msg; |
| 155 | $disable_comments_settings['disabled_types'] = $_types; |
| 156 | } |
| 157 | |
| 158 | // for network. |
| 159 | if(!empty($extra_post_types)){ |
| 160 | $disable_comments_settings['extra_post_types'] = $extra_post_types; |
| 161 | // translators: %s: post types to be disabled in network |
| 162 | $msg .= sprintf( __( 'Custom post types: %s. ', 'disable-comments' ), $extra_post_types ); |
| 163 | } |
| 164 | |
| 165 | if(isset($remove_xmlrpc_comments)){ |
| 166 | $disable_comments_settings['remove_xmlrpc_comments'] = $remove_xmlrpc_comments; |
| 167 | if($remove_xmlrpc_comments && $remove_xmlrpc_comments !== 'false'){ |
| 168 | $msg .= __( 'Disable Comments via XML-RPC. ', 'disable-comments' ); |
| 169 | } |
| 170 | else{ |
| 171 | $msg .= __( 'Enabled Comments via XML-RPC. ', 'disable-comments' ); |
| 172 | } |
| 173 | } |
| 174 | if(isset($remove_rest_API_comments)){ |
| 175 | $disable_comments_settings['remove_rest_API_comments'] = $remove_rest_API_comments; |
| 176 | if($remove_rest_API_comments && $remove_rest_API_comments !== 'false'){ |
| 177 | $msg .= __( 'Disable Comments via REST API. ', 'disable-comments' ); |
| 178 | } |
| 179 | else{ |
| 180 | $msg .= __( 'Enabled Comments via REST API. ', 'disable-comments' ); |
| 181 | } |
| 182 | } |
| 183 | if($disable_avatar != null){ |
| 184 | $disable_comments_settings['disable_avatar'] = $disable_avatar; |
| 185 | if($disable_avatar && $disable_avatar !== 'false'){ |
| 186 | $msg .= __( 'Disabled Avatar on your entire site. ', 'disable-comments' ); |
| 187 | } |
| 188 | else{ |
| 189 | $msg .= __( 'Enabled Avatar on your entire site. ', 'disable-comments' ); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | $this->dc_instance->disable_comments_settings($disable_comments_settings); |
| 194 | |
| 195 | WP_CLI::success($msg); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Deletes Comments on your website. |
| 200 | * |
| 201 | * @when after_wp_load |
| 202 | */ |
| 203 | function delete($args, $assoc_args) |
| 204 | { |
| 205 | $msg = ""; |
| 206 | $delete_comments_settings = array('delete' => true); |
| 207 | $selected_delete_types = WP_CLI\Utils\get_flag_value($assoc_args, 'types'); |
| 208 | $delete_extra_post_types = WP_CLI\Utils\get_flag_value($assoc_args, 'extra-post-types'); |
| 209 | $delete_comment_types = WP_CLI\Utils\get_flag_value($assoc_args, 'comment-types'); |
| 210 | $delete_spam_types = WP_CLI\Utils\get_flag_value($assoc_args, 'spam'); |
| 211 | |
| 212 | |
| 213 | if ( $delete_comment_types === 'all' || $selected_delete_types === 'all' ) { |
| 214 | $delete_comments_settings['delete_mode'] = 'delete_everywhere'; |
| 215 | } elseif( !empty($selected_delete_types)) { |
| 216 | $delete_comments_settings['delete_mode'] = 'selected_delete_types'; |
| 217 | $delete_comments_settings['delete_types'] = array_map('trim', explode(',', $selected_delete_types)); |
| 218 | } elseif(!empty($delete_comment_types)) { |
| 219 | $delete_comments_settings['delete_mode'] = 'selected_delete_comment_types'; |
| 220 | $delete_comments_settings['delete_comment_types'] = array_map('trim', explode(',', $delete_comment_types)); |
| 221 | } elseif(!empty($delete_spam_types)) { |
| 222 | $delete_comments_settings['delete_mode'] = 'delete_spam'; |
| 223 | } else{ |
| 224 | WP_CLI::error("Please provide valid parameters. \nSee 'wp help dc delete' for more information."); |
| 225 | } |
| 226 | |
| 227 | // for network. |
| 228 | if(!empty($delete_extra_post_types)){ |
| 229 | $delete_comments_settings['delete_extra_post_types'] = $delete_extra_post_types; |
| 230 | } |
| 231 | |
| 232 | $logged_msg = $this->dc_instance->delete_comments_settings($delete_comments_settings); |
| 233 | WP_CLI::success( is_array($logged_msg) ? implode( "\n", $logged_msg ) : $logged_msg ); |
| 234 | } |
| 235 | } |
| 236 |