| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\Cloud; |
| 4 |
|
| 5 |
use Code_Snippets\Data_Item; |
| 6 |
use function Code_Snippets\code_snippets_build_tags_array; |
| 7 |
|
| 8 |
/** |
| 9 |
* A snippet object as retrieved from the cloud API. |
| 10 |
* |
| 11 |
* @since 3.4.0 |
| 12 |
* @package Code_Snippets |
| 13 |
* |
| 14 |
* @property int $id The remote ID. |
| 15 |
* @property string $name The snippet title. |
| 16 |
* @property string $description The formatted description. |
| 17 |
* @property string $code The executable code. |
| 18 |
* @property array<string> $tags An array of the tags. |
| 19 |
* @property string $scope The scope name. |
| 20 |
* @property string $codevault Name of user codevault. |
| 21 |
* @property string $total_votes The total number of votes. |
| 22 |
* @property string $vote_count The number of actual votes. |
| 23 |
* @property string $wp_tested Tested with WP version. |
| 24 |
* @property string $status Snippet Status ID. |
| 25 |
* @property string $created The date and time when the snippet data was first created, in ISO format. |
| 26 |
* @property string $updated When the snippet was last updated, in ISO format. |
| 27 |
* @property integer $revision The update revision number. |
| 28 |
* @property bool $is_owner If user is owner or author of snippet. |
| 29 |
*/ |
| 30 |
class Cloud_Snippet extends Data_Item { |
| 31 |
|
| 32 |
/** |
| 33 |
* Constructor function. |
| 34 |
* |
| 35 |
* @param array<string, mixed>|null $initial_data Initial snippet data. |
| 36 |
*/ |
| 37 |
public function __construct( ?array $initial_data = null ) { |
| 38 |
parent::__construct( |
| 39 |
[ |
| 40 |
'id' => '', |
| 41 |
'cloud_id' => '', |
| 42 |
'name' => '', |
| 43 |
'description' => '', |
| 44 |
'code' => '', |
| 45 |
'tags' => [], |
| 46 |
'scope' => '', |
| 47 |
'status' => '', |
| 48 |
'codevault' => '', |
| 49 |
'total_votes' => '', |
| 50 |
'vote_count' => '', |
| 51 |
'wp_tested' => '', |
| 52 |
'created' => '', |
| 53 |
'updated' => '', |
| 54 |
'revision' => 0, |
| 55 |
'is_owner' => false, |
| 56 |
'shared_network' => false, |
| 57 |
], |
| 58 |
$initial_data |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Prepare a value before it is stored. |
| 64 |
* |
| 65 |
* @param mixed $value Value to prepare. |
| 66 |
* @param string $field Field name. |
| 67 |
* |
| 68 |
* @return mixed Value in the correct format. |
| 69 |
*/ |
| 70 |
protected function prepare_field( $value, string $field ) { |
| 71 |
switch ( $field ) { |
| 72 |
case 'id': |
| 73 |
case 'revision': |
| 74 |
return absint( $value ); |
| 75 |
|
| 76 |
case 'is_owner': |
| 77 |
return (bool) $value; |
| 78 |
case 'description': |
| 79 |
return ( null === $value ) ? '' : $value; |
| 80 |
case 'tags': |
| 81 |
return code_snippets_build_tags_array( $value ); |
| 82 |
|
| 83 |
default: |
| 84 |
return $value; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|