WC_REST_System_Status_Tools_V2_Controller::execute_tool()publicWC 1.0

Actually executes a tool.

Метод класса: WC_REST_System_Status_Tools_V2_Controller{}

Хуки из метода

Возвращает

Массив.

Использование

$WC_REST_System_Status_Tools_V2_Controller = new WC_REST_System_Status_Tools_V2_Controller();
$WC_REST_System_Status_Tools_V2_Controller->execute_tool( $tool );
$tool(строка) (обязательный)
Tool.

Код WC_REST_System_Status_Tools_V2_Controller::execute_tool() WC 8.7.0

public function execute_tool( $tool ) {
	global $wpdb;
	$ran = true;
	switch ( $tool ) {
		case 'clear_transients':
			wc_delete_product_transients();
			wc_delete_shop_order_transients();
			delete_transient( 'wc_count_comments' );
			delete_transient( 'as_comment_count' );

			$attribute_taxonomies = wc_get_attribute_taxonomies();

			if ( $attribute_taxonomies ) {
				foreach ( $attribute_taxonomies as $attribute ) {
					delete_transient( 'wc_layered_nav_counts_pa_' . $attribute->attribute_name );
				}
			}

			WC_Cache_Helper::get_transient_version( 'shipping', true );
			$message = __( 'Product transients cleared', 'woocommerce' );
			break;

		case 'clear_expired_transients':
			/* translators: %d: amount of expired transients */
			$message = sprintf( __( '%d transients rows cleared', 'woocommerce' ), wc_delete_expired_transients() );
			break;

		case 'delete_orphaned_variations':
			// Delete orphans.
			$result = absint(
				$wpdb->query(
					"DELETE products
				FROM {$wpdb->posts} products
				LEFT JOIN {$wpdb->posts} wp ON wp.ID = products.post_parent
				WHERE wp.ID IS NULL AND products.post_type = 'product_variation';"
				)
			);
			/* translators: %d: amount of orphaned variations */
			$message = sprintf( __( '%d orphaned variations deleted', 'woocommerce' ), $result );
			break;

		case 'clear_expired_download_permissions':
			// Delete related records in wc_download_log (aka ON DELETE CASCADE).
			$wpdb->query(
				$wpdb->prepare(
					"DELETE FROM {$wpdb->prefix}wc_download_log
					WHERE permission_id IN (
							    SELECT permission_id FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
								WHERE ( downloads_remaining != '' AND downloads_remaining = 0 ) OR ( access_expires IS NOT NULL AND access_expires < %s )
							    )",
					current_time( 'Y-m-d' )
				)
			);
			// Delete expired download permissions and ones with 0 downloads remaining.
			$result = absint(
				$wpdb->query(
					$wpdb->prepare(
						"DELETE FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
						WHERE ( downloads_remaining != '' AND downloads_remaining = 0 ) OR ( access_expires IS NOT NULL AND access_expires < %s )",
						current_time( 'Y-m-d' )
					)
				)
			);
			/* translators: %d: amount of permissions */
			$message = sprintf( __( '%d permissions deleted', 'woocommerce' ), $result );
			break;

		case 'regenerate_product_lookup_tables':
			if ( ! wc_update_product_lookup_tables_is_running() ) {
				wc_update_product_lookup_tables();
			}
			$message = __( 'Lookup tables are regenerating', 'woocommerce' );
			break;
		case 'reset_roles':
			// Remove then re-add caps and roles.
			WC_Install::remove_roles();
			WC_Install::create_roles();
			$message = __( 'Roles successfully reset', 'woocommerce' );
			break;

		case 'recount_terms':
			wc_recount_all_terms();
			$message = __( 'Terms successfully recounted', 'woocommerce' );
			break;

		case 'clear_sessions':
			$wpdb->query( "TRUNCATE {$wpdb->prefix}woocommerce_sessions" );
			$result = absint( $wpdb->query( "DELETE FROM {$wpdb->usermeta} WHERE meta_key='_woocommerce_persistent_cart_" . get_current_blog_id() . "';" ) ); // WPCS: unprepared SQL ok.
			wp_cache_flush();
			/* translators: %d: amount of sessions */
			$message = sprintf( __( 'Deleted all active sessions, and %d saved carts.', 'woocommerce' ), absint( $result ) );
			break;

		case 'install_pages':
			WC_Install::create_pages();
			$message = __( 'All missing WooCommerce pages successfully installed', 'woocommerce' );
			break;

		case 'delete_taxes':
			$wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}woocommerce_tax_rates;" );
			$wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}woocommerce_tax_rate_locations;" );

			if ( method_exists( 'WC_Cache_Helper', 'invalidate_cache_group' ) ) {
				WC_Cache_Helper::invalidate_cache_group( 'taxes' );
			} else {
				WC_Cache_Helper::incr_cache_prefix( 'taxes' );
			}
			$message = __( 'Tax rates successfully deleted', 'woocommerce' );
			break;

		case 'regenerate_thumbnails':
			WC_Regenerate_Images::queue_image_regeneration();
			$message = __( 'Thumbnail regeneration has been scheduled to run in the background.', 'woocommerce' );
			break;

		case 'db_update_routine':
			$blog_id = get_current_blog_id();
			// Used to fire an action added in WP_Background_Process::_construct() that calls WP_Background_Process::handle_cron_healthcheck().
			// This method will make sure the database updates are executed even if cron is disabled. Nothing will happen if the updates are already running.
			do_action( 'wp_' . $blog_id . '_wc_updater_cron' );
			$message = __( 'Database upgrade routine has been scheduled to run in the background.', 'woocommerce' );
			break;

		case 'clear_template_cache':
			if ( function_exists( 'wc_clear_template_cache' ) ) {
				wc_clear_template_cache();
				$message = __( 'Template cache cleared.', 'woocommerce' );
			} else {
				$message = __( 'The active version of WooCommerce does not support template cache clearing.', 'woocommerce' );
				$ran     = false;
			}
			break;

		case 'verify_db_tables':
			if ( ! method_exists( 'WC_Install', 'verify_base_tables' ) ) {
				$message = __( 'You need WooCommerce 4.2 or newer to run this tool.', 'woocommerce' );
				$ran     = false;
				break;
			}
			// Try to manually create table again.
			$missing_tables = WC_Install::verify_base_tables( true, true );
			if ( 0 === count( $missing_tables ) ) {
				$message = __( 'Database verified successfully.', 'woocommerce' );
			} else {
				$message  = __( 'Verifying database... One or more tables are still missing: ', 'woocommerce' );
				$message .= implode( ', ', $missing_tables );
				$ran      = false;
			}
			break;

		default:
			$tools = $this->get_tools();
			if ( isset( $tools[ $tool ]['callback'] ) ) {
				$callback = $tools[ $tool ]['callback'];
				try {
					$return = call_user_func( $callback );
				} catch ( Exception $exception ) {
					$return = $exception;
				}
				if ( is_a( $return, Exception::class ) ) {
					$callback_string = $this->get_printable_callback_name( $callback, $tool );
					$ran             = false;
					/* translators: %1$s: callback string, %2$s: error message */
					$message = sprintf( __( 'There was an error calling %1$s: %2$s', 'woocommerce' ), $callback_string, $return->getMessage() );

					$logger = wc_get_logger();
					$logger->error(
						sprintf(
							'Error running debug tool %s: %s',
							$tool,
							$return->getMessage()
						),
						array(
							'source'   => 'run-debug-tool',
							'tool'     => $tool,
							'callback' => $callback,
							'error'    => $return,
						)
					);
				} elseif ( is_string( $return ) ) {
					$message = $return;
				} elseif ( false === $return ) {
					$callback_string = $this->get_printable_callback_name( $callback, $tool );
					$ran             = false;
					/* translators: %s: callback string */
					$message = sprintf( __( 'There was an error calling %s', 'woocommerce' ), $callback_string );
				} else {
					$message = __( 'Tool ran.', 'woocommerce' );
				}
			} else {
				$ran     = false;
				$message = __( 'There was an error calling this tool. There is no callback present.', 'woocommerce' );
			}
			break;
	}

	return array(
		'success' => $ran,
		'message' => $message,
	);
}