Edit File by line
/home/zeestwma/ajeebong.../wp-conte.../plugins/code-sni.../php/cloud
File: class-cloud-snippets.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Code_Snippets\Cloud;
[2] Fix | Delete
[3] Fix | Delete
use Code_Snippets\Data_Item;
[4] Fix | Delete
[5] Fix | Delete
/**
[6] Fix | Delete
* A list of snippets as retrieved from the cloud API.
[7] Fix | Delete
*
[8] Fix | Delete
* @since 3.4.0
[9] Fix | Delete
* @package Code_Snippets
[10] Fix | Delete
*
[11] Fix | Delete
* @property Cloud_Snippet[] $snippets List of snippet items for the current page.
[12] Fix | Delete
* @property integer $page Page of data that this data belongs to.
[13] Fix | Delete
* @property integer $total_pages Total number of available pages of items.
[14] Fix | Delete
* @property integer $total_snippets Total number of available snippet items.
[15] Fix | Delete
* @property array $cloud_id_rev An array of all cloud snippet IDs and their revision numbers.
[16] Fix | Delete
* @property bool $success If the request has any results.
[17] Fix | Delete
*/
[18] Fix | Delete
class Cloud_Snippets extends Data_Item {
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Class constructor.
[22] Fix | Delete
*
[23] Fix | Delete
* @param array<string, Cloud_Snippet[]|integer> $initial_data Initial data.
[24] Fix | Delete
*/
[25] Fix | Delete
public function __construct( $initial_data = null ) {
[26] Fix | Delete
$initial_data = $this->normalize_cloud_api( $initial_data );
[27] Fix | Delete
parent::__construct(
[28] Fix | Delete
[
[29] Fix | Delete
'snippets' => [],
[30] Fix | Delete
'total_snippets' => 0,
[31] Fix | Delete
'total_pages' => 0,
[32] Fix | Delete
'page' => 0,
[33] Fix | Delete
'cloud_id_rev' => [],
[34] Fix | Delete
],
[35] Fix | Delete
$initial_data,
[36] Fix | Delete
[
[37] Fix | Delete
'items' => 'snippets',
[38] Fix | Delete
'total_items' => 'total_snippets',
[39] Fix | Delete
'page' => 'page',
[40] Fix | Delete
'cloud_id_rev' => 'cloud_id_rev',
[41] Fix | Delete
]
[42] Fix | Delete
);
[43] Fix | Delete
}
[44] Fix | Delete
[45] Fix | Delete
/**
[46] Fix | Delete
* Prepare a value before it is stored.
[47] Fix | Delete
*
[48] Fix | Delete
* @param mixed $value Value to prepare.
[49] Fix | Delete
* @param string $field Field name.
[50] Fix | Delete
*
[51] Fix | Delete
* @return mixed Value in the correct format.
[52] Fix | Delete
*/
[53] Fix | Delete
protected function prepare_field( $value, string $field ) {
[54] Fix | Delete
switch ( $field ) {
[55] Fix | Delete
case 'page':
[56] Fix | Delete
case 'total_pages':
[57] Fix | Delete
case 'total_snippets':
[58] Fix | Delete
return absint( $value );
[59] Fix | Delete
[60] Fix | Delete
default:
[61] Fix | Delete
return $value;
[62] Fix | Delete
}
[63] Fix | Delete
}
[64] Fix | Delete
[65] Fix | Delete
/**
[66] Fix | Delete
* Prepare the `snippets` field by ensuring it is a list of Cloud_Snippets objects.
[67] Fix | Delete
*
[68] Fix | Delete
* @param mixed $snippets The field as provided.
[69] Fix | Delete
*
[70] Fix | Delete
* @return Cloud_Snippets[] The field in the correct format.
[71] Fix | Delete
*/
[72] Fix | Delete
protected function prepare_snippets( $snippets ): array {
[73] Fix | Delete
$result = [];
[74] Fix | Delete
$snippets = is_array( $snippets ) ? $snippets : [ $snippets ];
[75] Fix | Delete
[76] Fix | Delete
foreach ( $snippets as $snippet ) {
[77] Fix | Delete
$result[] = $snippet instanceof Cloud_Snippet ? $snippet : new Cloud_Snippet( $snippet );
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
return $result;
[81] Fix | Delete
}
[82] Fix | Delete
[83] Fix | Delete
/**
[84] Fix | Delete
* Normalize payloads returned by the cloud API into the shape expected by this class.
[85] Fix | Delete
*
[86] Fix | Delete
* @param mixed $initial_data Raw data passed into the constructor.
[87] Fix | Delete
*
[88] Fix | Delete
* @return mixed Normalized data array or original value when no normalization is required.
[89] Fix | Delete
*/
[90] Fix | Delete
private function normalize_cloud_api( $initial_data ) {
[91] Fix | Delete
// pagination metadata is nested under a 'meta' key.
[92] Fix | Delete
if ( is_array( $initial_data ) && isset( $initial_data['meta'] ) ) {
[93] Fix | Delete
$meta = $initial_data['meta'];
[94] Fix | Delete
$normalized = [];
[95] Fix | Delete
$normalized['snippets'] = $initial_data['snippets'] ?? $initial_data['data'] ?? [];
[96] Fix | Delete
$normalized['total_snippets'] = isset( $meta['total'] ) ? (int) $meta['total'] : 0;
[97] Fix | Delete
$normalized['total_pages'] = isset( $meta['total_pages'] ) ? (int) $meta['total_pages'] : 0;
[98] Fix | Delete
$normalized['page'] = isset( $meta['page'] ) ? max( 0, (int) $meta['page'] - 1 ) : 0;
[99] Fix | Delete
$normalized['cloud_id_rev'] = $initial_data['cloud_id_rev'] ?? [];
[100] Fix | Delete
$initial_data = $normalized;
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
return $initial_data;
[104] Fix | Delete
}
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function