Edit File by line
/home/barbar84/www/wp-conte.../plugins/wordpres.../src/commands
File: index-command.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Commands;
[2] Fix | Delete
[3] Fix | Delete
use WP_CLI;
[4] Fix | Delete
use WP_CLI\Utils;
[5] Fix | Delete
use Yoast\WP\Lib\Model;
[6] Fix | Delete
use Yoast\WP\SEO\Actions\Indexing\Indexable_General_Indexation_Action;
[7] Fix | Delete
use Yoast\WP\SEO\Actions\Indexing\Indexable_Indexing_Complete_Action;
[8] Fix | Delete
use Yoast\WP\SEO\Actions\Indexing\Indexable_Post_Indexation_Action;
[9] Fix | Delete
use Yoast\WP\SEO\Actions\Indexing\Indexable_Post_Type_Archive_Indexation_Action;
[10] Fix | Delete
use Yoast\WP\SEO\Actions\Indexing\Indexable_Term_Indexation_Action;
[11] Fix | Delete
use Yoast\WP\SEO\Actions\Indexing\Indexation_Action_Interface;
[12] Fix | Delete
use Yoast\WP\SEO\Actions\Indexing\Indexing_Prepare_Action;
[13] Fix | Delete
use Yoast\WP\SEO\Actions\Indexing\Post_Link_Indexing_Action;
[14] Fix | Delete
use Yoast\WP\SEO\Actions\Indexing\Term_Link_Indexing_Action;
[15] Fix | Delete
use Yoast\WP\SEO\Main;
[16] Fix | Delete
[17] Fix | Delete
/**
[18] Fix | Delete
* Command to generate indexables for all posts and terms.
[19] Fix | Delete
*/
[20] Fix | Delete
class Index_Command implements Command_Interface {
[21] Fix | Delete
[22] Fix | Delete
/**
[23] Fix | Delete
* The post indexation action.
[24] Fix | Delete
*
[25] Fix | Delete
* @var Indexable_Post_Indexation_Action
[26] Fix | Delete
*/
[27] Fix | Delete
private $post_indexation_action;
[28] Fix | Delete
[29] Fix | Delete
/**
[30] Fix | Delete
* The term indexation action.
[31] Fix | Delete
*
[32] Fix | Delete
* @var Indexable_Term_Indexation_Action
[33] Fix | Delete
*/
[34] Fix | Delete
private $term_indexation_action;
[35] Fix | Delete
[36] Fix | Delete
/**
[37] Fix | Delete
* The post type archive indexation action.
[38] Fix | Delete
*
[39] Fix | Delete
* @var Indexable_Post_Type_Archive_Indexation_Action
[40] Fix | Delete
*/
[41] Fix | Delete
private $post_type_archive_indexation_action;
[42] Fix | Delete
[43] Fix | Delete
/**
[44] Fix | Delete
* The general indexation action.
[45] Fix | Delete
*
[46] Fix | Delete
* @var Indexable_General_Indexation_Action
[47] Fix | Delete
*/
[48] Fix | Delete
private $general_indexation_action;
[49] Fix | Delete
[50] Fix | Delete
/**
[51] Fix | Delete
* The term link indexing action.
[52] Fix | Delete
*
[53] Fix | Delete
* @var Term_Link_Indexing_Action
[54] Fix | Delete
*/
[55] Fix | Delete
private $term_link_indexing_action;
[56] Fix | Delete
[57] Fix | Delete
/**
[58] Fix | Delete
* The post link indexing action.
[59] Fix | Delete
*
[60] Fix | Delete
* @var Post_Link_Indexing_Action
[61] Fix | Delete
*/
[62] Fix | Delete
private $post_link_indexing_action;
[63] Fix | Delete
[64] Fix | Delete
/**
[65] Fix | Delete
* The complete indexation action.
[66] Fix | Delete
*
[67] Fix | Delete
* @var Indexable_Indexing_Complete_Action
[68] Fix | Delete
*/
[69] Fix | Delete
private $complete_indexation_action;
[70] Fix | Delete
[71] Fix | Delete
/**
[72] Fix | Delete
* The indexing prepare action.
[73] Fix | Delete
*
[74] Fix | Delete
* @var Indexing_Prepare_Action
[75] Fix | Delete
*/
[76] Fix | Delete
private $prepare_indexing_action;
[77] Fix | Delete
[78] Fix | Delete
/**
[79] Fix | Delete
* Generate_Indexables_Command constructor.
[80] Fix | Delete
*
[81] Fix | Delete
* @param Indexable_Post_Indexation_Action $post_indexation_action The post indexation
[82] Fix | Delete
* action.
[83] Fix | Delete
* @param Indexable_Term_Indexation_Action $term_indexation_action The term indexation
[84] Fix | Delete
* action.
[85] Fix | Delete
* @param Indexable_Post_Type_Archive_Indexation_Action $post_type_archive_indexation_action The post type archive
[86] Fix | Delete
* indexation action.
[87] Fix | Delete
* @param Indexable_General_Indexation_Action $general_indexation_action The general indexation
[88] Fix | Delete
* action.
[89] Fix | Delete
* @param Indexable_Indexing_Complete_Action $complete_indexation_action The complete indexation
[90] Fix | Delete
* action.
[91] Fix | Delete
* @param Indexing_Prepare_Action $prepare_indexing_action The prepare indexing
[92] Fix | Delete
* action.
[93] Fix | Delete
* @param Post_Link_Indexing_Action $post_link_indexing_action The post link indexation
[94] Fix | Delete
* action.
[95] Fix | Delete
* @param Term_Link_Indexing_Action $term_link_indexing_action The term link indexation
[96] Fix | Delete
* action.
[97] Fix | Delete
*/
[98] Fix | Delete
public function __construct(
[99] Fix | Delete
Indexable_Post_Indexation_Action $post_indexation_action,
[100] Fix | Delete
Indexable_Term_Indexation_Action $term_indexation_action,
[101] Fix | Delete
Indexable_Post_Type_Archive_Indexation_Action $post_type_archive_indexation_action,
[102] Fix | Delete
Indexable_General_Indexation_Action $general_indexation_action,
[103] Fix | Delete
Indexable_Indexing_Complete_Action $complete_indexation_action,
[104] Fix | Delete
Indexing_Prepare_Action $prepare_indexing_action,
[105] Fix | Delete
Post_Link_Indexing_Action $post_link_indexing_action,
[106] Fix | Delete
Term_Link_Indexing_Action $term_link_indexing_action
[107] Fix | Delete
) {
[108] Fix | Delete
$this->post_indexation_action = $post_indexation_action;
[109] Fix | Delete
$this->term_indexation_action = $term_indexation_action;
[110] Fix | Delete
$this->post_type_archive_indexation_action = $post_type_archive_indexation_action;
[111] Fix | Delete
$this->general_indexation_action = $general_indexation_action;
[112] Fix | Delete
$this->complete_indexation_action = $complete_indexation_action;
[113] Fix | Delete
$this->prepare_indexing_action = $prepare_indexing_action;
[114] Fix | Delete
$this->post_link_indexing_action = $post_link_indexing_action;
[115] Fix | Delete
$this->term_link_indexing_action = $term_link_indexing_action;
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
/**
[119] Fix | Delete
* Gets the namespace.
[120] Fix | Delete
*
[121] Fix | Delete
* @return string
[122] Fix | Delete
*/
[123] Fix | Delete
public static function get_namespace() {
[124] Fix | Delete
return Main::WP_CLI_NAMESPACE;
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
/**
[128] Fix | Delete
* Indexes all your content to ensure the best performance.
[129] Fix | Delete
*
[130] Fix | Delete
* ## OPTIONS
[131] Fix | Delete
*
[132] Fix | Delete
* [--network]
[133] Fix | Delete
* : Performs the indexation on all sites within the network.
[134] Fix | Delete
*
[135] Fix | Delete
* [--reindex]
[136] Fix | Delete
* : Removes all existing indexables and then reindexes them.
[137] Fix | Delete
*
[138] Fix | Delete
* [--skip-confirmation]
[139] Fix | Delete
* : Skips the confirmations (for automated systems).
[140] Fix | Delete
*
[141] Fix | Delete
* ## EXAMPLES
[142] Fix | Delete
*
[143] Fix | Delete
* wp yoast index
[144] Fix | Delete
*
[145] Fix | Delete
* @when after_wp_load
[146] Fix | Delete
*
[147] Fix | Delete
* @param array|null $args The arguments.
[148] Fix | Delete
* @param array|null $assoc_args The associative arguments.
[149] Fix | Delete
*
[150] Fix | Delete
* @return void
[151] Fix | Delete
*/
[152] Fix | Delete
public function index( $args = null, $assoc_args = null ) {
[153] Fix | Delete
if ( ! isset( $assoc_args['network'] ) ) {
[154] Fix | Delete
$this->run_indexation_actions( $assoc_args );
[155] Fix | Delete
[156] Fix | Delete
return;
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
$criteria = [
[160] Fix | Delete
'fields' => 'ids',
[161] Fix | Delete
'spam' => 0,
[162] Fix | Delete
'deleted' => 0,
[163] Fix | Delete
'archived' => 0,
[164] Fix | Delete
];
[165] Fix | Delete
$blog_ids = \get_sites( $criteria );
[166] Fix | Delete
[167] Fix | Delete
foreach ( $blog_ids as $blog_id ) {
[168] Fix | Delete
\switch_to_blog( $blog_id );
[169] Fix | Delete
\do_action( '_yoast_run_migrations' );
[170] Fix | Delete
$this->run_indexation_actions( $assoc_args );
[171] Fix | Delete
\restore_current_blog();
[172] Fix | Delete
}
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
/**
[176] Fix | Delete
* Runs all indexation actions.
[177] Fix | Delete
*
[178] Fix | Delete
* @param array $assoc_args The associative arguments.
[179] Fix | Delete
*
[180] Fix | Delete
* @return void
[181] Fix | Delete
*/
[182] Fix | Delete
protected function run_indexation_actions( $assoc_args ) {
[183] Fix | Delete
// See if we need to clear all indexables before repopulating.
[184] Fix | Delete
if ( isset( $assoc_args['reindex'] ) ) {
[185] Fix | Delete
[186] Fix | Delete
// Argument --skip-confirmation to prevent confirmation (for automated systems).
[187] Fix | Delete
if ( ! isset( $assoc_args['skip-confirmation'] ) ) {
[188] Fix | Delete
WP_CLI::confirm( 'This will clear all previously indexed objects. Are you certain you wish to proceed?' );
[189] Fix | Delete
}
[190] Fix | Delete
[191] Fix | Delete
// Truncate the tables.
[192] Fix | Delete
$this->clear();
[193] Fix | Delete
[194] Fix | Delete
// Delete the transients to make sure re-indexing runs every time.
[195] Fix | Delete
\delete_transient( Indexable_Post_Indexation_Action::UNINDEXED_COUNT_TRANSIENT );
[196] Fix | Delete
\delete_transient( Indexable_Post_Type_Archive_Indexation_Action::UNINDEXED_COUNT_TRANSIENT );
[197] Fix | Delete
\delete_transient( Indexable_Term_Indexation_Action::UNINDEXED_COUNT_TRANSIENT );
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
$indexation_actions = [
[201] Fix | Delete
'posts' => $this->post_indexation_action,
[202] Fix | Delete
'terms' => $this->term_indexation_action,
[203] Fix | Delete
'post type archives' => $this->post_type_archive_indexation_action,
[204] Fix | Delete
'general objects' => $this->general_indexation_action,
[205] Fix | Delete
'post links' => $this->post_link_indexing_action,
[206] Fix | Delete
'term links' => $this->term_link_indexing_action,
[207] Fix | Delete
];
[208] Fix | Delete
[209] Fix | Delete
$this->prepare_indexing_action->prepare();
[210] Fix | Delete
[211] Fix | Delete
foreach ( $indexation_actions as $name => $indexation_action ) {
[212] Fix | Delete
$this->run_indexation_action( $name, $indexation_action );
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
$this->complete_indexation_action->complete();
[216] Fix | Delete
}
[217] Fix | Delete
[218] Fix | Delete
/**
[219] Fix | Delete
* Runs an indexation action.
[220] Fix | Delete
*
[221] Fix | Delete
* @param string $name The name of the object to be indexed.
[222] Fix | Delete
* @param Indexation_Action_Interface $indexation_action The indexation action.
[223] Fix | Delete
*
[224] Fix | Delete
* @return void
[225] Fix | Delete
*/
[226] Fix | Delete
protected function run_indexation_action( $name, Indexation_Action_Interface $indexation_action ) {
[227] Fix | Delete
$total = $indexation_action->get_total_unindexed();
[228] Fix | Delete
if ( $total > 0 ) {
[229] Fix | Delete
$limit = $indexation_action->get_limit();
[230] Fix | Delete
$progress = Utils\make_progress_bar( 'Indexing ' . $name, $total );
[231] Fix | Delete
do {
[232] Fix | Delete
$indexables = $indexation_action->index();
[233] Fix | Delete
$count = \count( $indexables );
[234] Fix | Delete
$progress->tick( $count );
[235] Fix | Delete
} while ( $count >= $limit );
[236] Fix | Delete
$progress->finish();
[237] Fix | Delete
}
[238] Fix | Delete
}
[239] Fix | Delete
[240] Fix | Delete
/**
[241] Fix | Delete
* Clears the database related to the indexables.
[242] Fix | Delete
*/
[243] Fix | Delete
protected function clear() {
[244] Fix | Delete
global $wpdb;
[245] Fix | Delete
[246] Fix | Delete
// For the PreparedSQLPlaceholders issue, see: https://github.com/WordPress/WordPress-Coding-Standards/issues/1903.
[247] Fix | Delete
// For the DirectDBQuery issue, see: https://github.com/WordPress/WordPress-Coding-Standards/issues/1947.
[248] Fix | Delete
// phpcs:disable WordPress.DB -- Table names should not be quoted and truncate queries can not be cached.
[249] Fix | Delete
$wpdb->query(
[250] Fix | Delete
$wpdb->prepare(
[251] Fix | Delete
'TRUNCATE TABLE %1$s',
[252] Fix | Delete
Model::get_table_name( 'Indexable' )
[253] Fix | Delete
)
[254] Fix | Delete
);
[255] Fix | Delete
$wpdb->query(
[256] Fix | Delete
$wpdb->prepare(
[257] Fix | Delete
'TRUNCATE TABLE %1$s',
[258] Fix | Delete
Model::get_table_name( 'Indexable_Hierarchy' )
[259] Fix | Delete
)
[260] Fix | Delete
);
[261] Fix | Delete
// phpcs:enable
[262] Fix | Delete
}
[263] Fix | Delete
}
[264] Fix | Delete
[265] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function