Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/popup-ma.../trunk/classes/Abstract/Upgrade
File: Posts.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Abstract for posts upgrade
[2] Fix | Delete
*
[3] Fix | Delete
* @package PUM
[4] Fix | Delete
* @copyright Copyright (c) 2023, Code Atlantic LLC
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[8] Fix | Delete
exit;
[9] Fix | Delete
}
[10] Fix | Delete
[11] Fix | Delete
/**
[12] Fix | Delete
* Implements a batch processor for migrating existing posts to new data structure.
[13] Fix | Delete
*
[14] Fix | Delete
* @since 1.7.0
[15] Fix | Delete
*
[16] Fix | Delete
* @see PUM_Abstract_Upgrade
[17] Fix | Delete
* @see PUM_Interface_Batch_PrefetchProcess
[18] Fix | Delete
* @see PUM_Interface_Upgrade_Posts
[19] Fix | Delete
*/
[20] Fix | Delete
abstract class PUM_Abstract_Upgrade_Posts extends PUM_Abstract_Upgrade implements PUM_Interface_Upgrade_Posts {
[21] Fix | Delete
[22] Fix | Delete
/**
[23] Fix | Delete
* Batch process ID.
[24] Fix | Delete
*
[25] Fix | Delete
* @var string
[26] Fix | Delete
*/
[27] Fix | Delete
public $batch_id;
[28] Fix | Delete
[29] Fix | Delete
/**
[30] Fix | Delete
* Post type.
[31] Fix | Delete
*
[32] Fix | Delete
* @var string
[33] Fix | Delete
*/
[34] Fix | Delete
public $post_type = 'post';
[35] Fix | Delete
[36] Fix | Delete
/**
[37] Fix | Delete
* Post status to update.
[38] Fix | Delete
*
[39] Fix | Delete
* @var array
[40] Fix | Delete
*/
[41] Fix | Delete
public $post_status = [ 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash' ];
[42] Fix | Delete
[43] Fix | Delete
/**
[44] Fix | Delete
* Number of posts to migrate per step.
[45] Fix | Delete
*
[46] Fix | Delete
* @var int
[47] Fix | Delete
*/
[48] Fix | Delete
public $per_step = 1;
[49] Fix | Delete
[50] Fix | Delete
/**
[51] Fix | Delete
* @var array
[52] Fix | Delete
*/
[53] Fix | Delete
public $post_ids;
[54] Fix | Delete
[55] Fix | Delete
/**
[56] Fix | Delete
* @var array
[57] Fix | Delete
*/
[58] Fix | Delete
public $completed_post_ids;
[59] Fix | Delete
[60] Fix | Delete
/**
[61] Fix | Delete
* Allows disabling of the post_id array query prefetch for stepping.
[62] Fix | Delete
*
[63] Fix | Delete
* When true will prefetch all post_ids from the query and cache them, stepping through that array. WP_Query is only called once.
[64] Fix | Delete
*
[65] Fix | Delete
* When false the stepping will occur via a new WP_Query with pagination.
[66] Fix | Delete
*
[67] Fix | Delete
* True is useful if you are querying on data that will be changed during processing.
[68] Fix | Delete
*
[69] Fix | Delete
* False is useful if there may be a massive amount of post data to migrate.
[70] Fix | Delete
* False is not useful when the query args are targeting data that will be changed.
[71] Fix | Delete
* Ex: Query all posts with old_meta, then during each step moving old_meta to new_meta.
[72] Fix | Delete
* In this example, the second query will not include posts updated in the first step, but then also sets an offset skipping posts that need update still.
[73] Fix | Delete
*
[74] Fix | Delete
* @var bool
[75] Fix | Delete
*/
[76] Fix | Delete
public $prefetch_ids = true;
[77] Fix | Delete
[78] Fix | Delete
public function init( $data = null ) {
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
public function pre_fetch() {
[82] Fix | Delete
$total_to_migrate = $this->get_total_count();
[83] Fix | Delete
[84] Fix | Delete
if ( ! $total_to_migrate ) {
[85] Fix | Delete
$posts = $this->get_posts(
[86] Fix | Delete
[
[87] Fix | Delete
'fields' => 'ids',
[88] Fix | Delete
'posts_per_page' => - 1,
[89] Fix | Delete
]
[90] Fix | Delete
);
[91] Fix | Delete
[92] Fix | Delete
$posts = wp_parse_id_list( $posts );
[93] Fix | Delete
[94] Fix | Delete
$total_to_migrate = count( $posts );
[95] Fix | Delete
[96] Fix | Delete
if ( $this->prefetch_ids ) {
[97] Fix | Delete
$this->set_post_ids( $posts );
[98] Fix | Delete
}
[99] Fix | Delete
[100] Fix | Delete
$this->set_total_count( $total_to_migrate );
[101] Fix | Delete
}
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
/**
[105] Fix | Delete
* Gets the results of a custom post query.
[106] Fix | Delete
*
[107] Fix | Delete
* @param array $args
[108] Fix | Delete
*
[109] Fix | Delete
* @return array
[110] Fix | Delete
*/
[111] Fix | Delete
public function get_posts( $args = [] ) {
[112] Fix | Delete
return get_posts( $this->query_args( $args ) );
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
/**
[116] Fix | Delete
* Generates an array of query args for this upgrade.
[117] Fix | Delete
*
[118] Fix | Delete
* @uses self::custom_query_args();
[119] Fix | Delete
*
[120] Fix | Delete
* @param array $args
[121] Fix | Delete
*
[122] Fix | Delete
* @return array
[123] Fix | Delete
*/
[124] Fix | Delete
public function query_args( $args = [] ) {
[125] Fix | Delete
[126] Fix | Delete
$defaults = wp_parse_args(
[127] Fix | Delete
$this->custom_query_args(),
[128] Fix | Delete
[
[129] Fix | Delete
'post_status' => $this->post_status,
[130] Fix | Delete
'post_type' => $this->post_type,
[131] Fix | Delete
]
[132] Fix | Delete
);
[133] Fix | Delete
[134] Fix | Delete
return wp_parse_args( $args, $defaults );
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
[138] Fix | Delete
/**
[139] Fix | Delete
* @return array
[140] Fix | Delete
*/
[141] Fix | Delete
public function custom_query_args() {
[142] Fix | Delete
return [];
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
/**
[146] Fix | Delete
* Executes a single step in the batch process.
[147] Fix | Delete
*
[148] Fix | Delete
* @return int|string|WP_Error Next step number, 'done', or a WP_Error object.
[149] Fix | Delete
*/
[150] Fix | Delete
public function process_step() {
[151] Fix | Delete
$completed_post_ids = $this->get_completed_post_ids();
[152] Fix | Delete
[153] Fix | Delete
if ( $this->prefetch_ids ) {
[154] Fix | Delete
$all_posts = $this->get_post_ids();
[155] Fix | Delete
$remaining_post_ids = array_diff( $all_posts, $completed_post_ids );
[156] Fix | Delete
$posts = array_slice( $remaining_post_ids, 0, $this->per_step );
[157] Fix | Delete
} else {
[158] Fix | Delete
$posts = $this->get_posts(
[159] Fix | Delete
[
[160] Fix | Delete
'fields' => 'ids',
[161] Fix | Delete
'posts_per_page' => $this->per_step,
[162] Fix | Delete
'offset' => $this->get_offset(),
[163] Fix | Delete
'orderby' => 'ID',
[164] Fix | Delete
'order' => 'ASC',
[165] Fix | Delete
]
[166] Fix | Delete
);
[167] Fix | Delete
}
[168] Fix | Delete
[169] Fix | Delete
if ( empty( $posts ) ) {
[170] Fix | Delete
return 'done';
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
foreach ( $posts as $post_id ) {
[174] Fix | Delete
$this->process_post( $post_id );
[175] Fix | Delete
$completed_post_ids[] = $post_id;
[176] Fix | Delete
}
[177] Fix | Delete
[178] Fix | Delete
// Deduplicate.
[179] Fix | Delete
$completed_post_ids = wp_parse_id_list( $completed_post_ids );
[180] Fix | Delete
$this->set_completed_post_ids( $completed_post_ids );
[181] Fix | Delete
[182] Fix | Delete
$this->set_current_count( count( $completed_post_ids ) );
[183] Fix | Delete
[184] Fix | Delete
return ++ $this->step;
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
/**
[188] Fix | Delete
* Retrieves a message for the given code.
[189] Fix | Delete
*
[190] Fix | Delete
* @param string $code Message code.
[191] Fix | Delete
*
[192] Fix | Delete
* @return string Message.
[193] Fix | Delete
*/
[194] Fix | Delete
public function get_message( $code ) {
[195] Fix | Delete
$post_type = get_post_type_object( $this->post_type );
[196] Fix | Delete
$labels = get_post_type_labels( $post_type );
[197] Fix | Delete
$singular = strtolower( $labels->singular_name );
[198] Fix | Delete
$plural = strtolower( $labels->name );
[199] Fix | Delete
[200] Fix | Delete
switch ( $code ) {
[201] Fix | Delete
[202] Fix | Delete
case 'start':
[203] Fix | Delete
$total_count = $this->get_total_count();
[204] Fix | Delete
[205] Fix | Delete
$message = sprintf( _n( 'Updating %d %2$s.', 'Updating %d %3$s.', $total_count, 'popup-maker' ), number_format_i18n( $total_count ), $singular, $plural );
[206] Fix | Delete
break;
[207] Fix | Delete
[208] Fix | Delete
case 'done':
[209] Fix | Delete
$final_count = $this->get_current_count();
[210] Fix | Delete
[211] Fix | Delete
$message = sprintf( _n( '%s %2$s was updated successfully.', '%s %3$s were updated successfully.', $final_count, 'popup-maker' ), number_format_i18n( $final_count ), $singular, $plural );
[212] Fix | Delete
break;
[213] Fix | Delete
[214] Fix | Delete
default:
[215] Fix | Delete
$message = '';
[216] Fix | Delete
break;
[217] Fix | Delete
}
[218] Fix | Delete
[219] Fix | Delete
return $message;
[220] Fix | Delete
}
[221] Fix | Delete
[222] Fix | Delete
/**
[223] Fix | Delete
* Process needed upgrades on each post.
[224] Fix | Delete
*
[225] Fix | Delete
* @param int $post_id
[226] Fix | Delete
*/
[227] Fix | Delete
abstract public function process_post( $post_id = 0 );
[228] Fix | Delete
[229] Fix | Delete
/**
[230] Fix | Delete
* Full list of post_ids to be processed.
[231] Fix | Delete
*
[232] Fix | Delete
* @return array|bool Default false.
[233] Fix | Delete
*/
[234] Fix | Delete
protected function get_post_ids() {
[235] Fix | Delete
if ( ! isset( $this->post_ids ) || ! $this->post_ids ) {
[236] Fix | Delete
$this->post_ids = PUM_DataStorage::get( "{$this->batch_id}_post_ids", false );
[237] Fix | Delete
[238] Fix | Delete
if ( is_array( $this->post_ids ) ) {
[239] Fix | Delete
$this->post_ids = wp_parse_id_list( $this->post_ids );
[240] Fix | Delete
}
[241] Fix | Delete
}
[242] Fix | Delete
[243] Fix | Delete
return $this->post_ids;
[244] Fix | Delete
}
[245] Fix | Delete
[246] Fix | Delete
/**
[247] Fix | Delete
* Sets list of post_ids to be processed.
[248] Fix | Delete
*
[249] Fix | Delete
* @param array $post_ids Full list of post_ids to be processed.
[250] Fix | Delete
*/
[251] Fix | Delete
protected function set_post_ids( $post_ids = [] ) {
[252] Fix | Delete
$this->post_ids = $post_ids;
[253] Fix | Delete
[254] Fix | Delete
PUM_DataStorage::write( "{$this->batch_id}_post_ids", $post_ids );
[255] Fix | Delete
}
[256] Fix | Delete
[257] Fix | Delete
/**
[258] Fix | Delete
* Deletes the stored data for this process.
[259] Fix | Delete
*/
[260] Fix | Delete
protected function delete_post_ids() {
[261] Fix | Delete
$this->post_ids = false;
[262] Fix | Delete
PUM_DataStorage::delete( "{$this->batch_id}_post_ids" );
[263] Fix | Delete
}
[264] Fix | Delete
[265] Fix | Delete
[266] Fix | Delete
/**
[267] Fix | Delete
* Full list of completed_post_ids to be processed.
[268] Fix | Delete
*
[269] Fix | Delete
* @return array|bool Default false.
[270] Fix | Delete
*/
[271] Fix | Delete
protected function get_completed_post_ids() {
[272] Fix | Delete
if ( ! isset( $this->completed_post_ids ) || ! $this->completed_post_ids ) {
[273] Fix | Delete
$completed_post_ids = PUM_DataStorage::get( "{$this->batch_id}_completed_post_ids", [] );
[274] Fix | Delete
$this->completed_post_ids = wp_parse_id_list( $completed_post_ids );
[275] Fix | Delete
}
[276] Fix | Delete
[277] Fix | Delete
return $this->completed_post_ids;
[278] Fix | Delete
}
[279] Fix | Delete
[280] Fix | Delete
/**
[281] Fix | Delete
* Sets list of completed_post_ids to be processed.
[282] Fix | Delete
*
[283] Fix | Delete
* @param array $completed_post_ids Full list of post_ids to be processed.
[284] Fix | Delete
*/
[285] Fix | Delete
protected function set_completed_post_ids( $completed_post_ids = [] ) {
[286] Fix | Delete
$this->completed_post_ids = wp_parse_id_list( $completed_post_ids );
[287] Fix | Delete
[288] Fix | Delete
PUM_DataStorage::write( "{$this->batch_id}_completed_post_ids", $completed_post_ids );
[289] Fix | Delete
}
[290] Fix | Delete
[291] Fix | Delete
/**
[292] Fix | Delete
* Deletes the stored data for this process.
[293] Fix | Delete
*/
[294] Fix | Delete
protected function delete_completed_post_ids() {
[295] Fix | Delete
$this->completed_post_ids = false;
[296] Fix | Delete
PUM_DataStorage::delete( "{$this->batch_id}_completed_post_ids" );
[297] Fix | Delete
}
[298] Fix | Delete
}
[299] Fix | Delete
[300] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function