settings; $rows = array( array( 'key' => 'version', 'value' => DANGO_COF_VERSION, ), array( 'key' => 'pro', 'value' => dango_cof()->is_pro() ? 'active' : 'no', ), array( 'key' => 'accepting orders', 'value' => $s->get( 'general', 'enabled', true ) ? 'yes' : 'no (paused)', ), array( 'key' => 'test mode', 'value' => $s->get( 'general', 'test_mode', false ) ? 'on' : 'off', ), array( 'key' => 'forms', 'value' => count( Dango_COF_Forms::all() ), ), array( 'key' => 'orders (30 days)', 'value' => (int) Dango_COF_Stats::total( 'order', 30 ), ), array( 'key' => 'store countries', 'value' => implode( ', ', Dango_COF_Markets::store_countries() ), ), array( 'key' => 'debug log', 'value' => Dango_COF_Log::debug_on() ? 'on' : 'off', ), ); WP_CLI\Utils\format_items( isset( $assoc['format'] ) ? $assoc['format'] : 'table', $rows, array( 'key', 'value' ) ); } /** * List the order forms. * * ## OPTIONS * * [--format=] * : table, csv, json or yaml. Default table. * * @param array $args Positional. * @param array $assoc Named. */ public function forms( $args, $assoc ) { $default = Dango_COF_Forms::default_id(); $rows = array(); foreach ( Dango_COF_Forms::all() as $f ) { $rows[] = array( 'id' => $f['id'], 'name' => $f['name'], 'default' => (int) $f['id'] === (int) $default ? 'yes' : '', 'assign' => isset( $f['assign']['mode'] ) ? $f['assign']['mode'] : '', ); } WP_CLI\Utils\format_items( isset( $assoc['format'] ) ? $assoc['format'] : 'table', $rows, array( 'id', 'name', 'default', 'assign' ) ); } /** * Export, import or reset the settings. Saved API keys are never exported. * * ## OPTIONS * * * : export, import or reset. * * [] * : JSON file (import). Export prints to the screen unless --file is given. * * [--file=] * : Write the export to this file. * * [--yes] * : Do not ask before resetting. * * ## EXAMPLES * * wp dango-cod settings export --file=cod-settings.json * wp dango-cod settings import cod-settings.json * wp dango-cod settings reset --yes * * @param array $args Positional. * @param array $assoc Named. */ public function settings( $args, $assoc ) { $action = isset( $args[0] ) ? $args[0] : ''; $settings = dango_cof()->settings; if ( 'export' === $action ) { $json = wp_json_encode( $settings->export(), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ); if ( ! empty( $assoc['file'] ) ) { file_put_contents( $assoc['file'], $json ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents -- CLI writes the file the user named. WP_CLI::success( 'Settings written to ' . $assoc['file'] ); return; } WP_CLI::line( $json ); return; } if ( 'import' === $action ) { $file = isset( $args[1] ) ? $args[1] : ''; if ( '' === $file || ! is_readable( $file ) ) { WP_CLI::error( 'Give the backup file to import.' ); } $data = json_decode( (string) file_get_contents( $file ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- local file named by the user. if ( ! is_array( $data ) || 'dango-cod-order-form-settings' !== ( isset( $data['type'] ) ? $data['type'] : '' ) || empty( $data['settings'] ) ) { WP_CLI::error( 'This is not a settings backup from Dango COD Order Form.' ); } $settings->update( $data['settings'] ); WP_CLI::success( 'Settings imported.' ); return; } if ( 'reset' === $action ) { WP_CLI::confirm( 'Reset all settings to their defaults (saved API keys are kept)?', $assoc ); $settings->reset(); WP_CLI::success( 'Settings reset.' ); return; } WP_CLI::error( 'Use: export, import or reset.' ); } /** * Switch the debug log on or off (WooCommerce → Status → Logs). * * ## OPTIONS * * * : on or off. * * @param array $args Positional. * @param array $assoc Named. */ public function debug( $args, $assoc ) { $state = isset( $args[0] ) ? $args[0] : ''; if ( ! in_array( $state, array( 'on', 'off' ), true ) ) { WP_CLI::error( 'Use: on or off.' ); } dango_cof()->settings->update( array( 'privacy' => array( 'debug_log' => 'on' === $state ) ) ); WP_CLI::success( 'Debug log ' . $state . '.' ); } /** * Manage the block list. * * ## OPTIONS * * * : list, add or remove. * * [] * : phone, email, ip or name. * * [] * : The value to add or remove. * * [--reason=] * : Note saved with a new entry. * * [--search=] * : Filter the list. * * ## EXAMPLES * * wp dango-cod blocklist add phone 03001234567 --reason="Fake orders" * wp dango-cod blocklist remove phone 03001234567 * * @param array $args Positional. * @param array $assoc Named. */ public function blocklist( $args, $assoc ) { $action = isset( $args[0] ) ? $args[0] : 'list'; if ( 'list' === $action ) { $res = Dango_COF_Blocklist::query( isset( $assoc['search'] ) ? (string) $assoc['search'] : '', 1, 500 ); $rows = isset( $res['rows'] ) ? $res['rows'] : array(); WP_CLI\Utils\format_items( 'table', $rows, $rows ? array_keys( (array) $rows[0] ) : array( 'id' ) ); return; } $type = isset( $args[1] ) ? $args[1] : ''; $value = isset( $args[2] ) ? $args[2] : ''; if ( '' === $type || '' === $value ) { WP_CLI::error( 'Give a type (phone, email, ip, name) and a value.' ); } if ( 'add' === $action ) { $ok = Dango_COF_Blocklist::add( $type, $value, 'block', isset( $assoc['reason'] ) ? (string) $assoc['reason'] : '', 'cli' ); is_wp_error( $ok ) || ! $ok ? WP_CLI::error( is_wp_error( $ok ) ? $ok->get_error_message() : 'Could not add.' ) : WP_CLI::success( 'Blocked.' ); return; } if ( 'remove' === $action ) { Dango_COF_Blocklist::remove_value( $type, $value ); WP_CLI::success( 'Removed.' ); return; } WP_CLI::error( 'Use: list, add or remove.' ); } /** * Show the city list for a country (with TrackKaro's saved cities when present). * * ## OPTIONS * * * : Two-letter country code. * * [--format=] * : table, csv, json or count. * * @param array $args Positional. * @param array $assoc Named. */ public function cities( $args, $assoc ) { $list = Dango_COF_Markets::cities( isset( $args[0] ) ? $args[0] : 'PK' ); $rows = array(); foreach ( $list as $c ) { $rows[] = array( 'name' => $c['n'], 'native' => $c['u'], 'state' => $c['s'], ); } WP_CLI\Utils\format_items( isset( $assoc['format'] ) ? $assoc['format'] : 'table', $rows, array( 'name', 'native', 'state' ) ); } }