| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\Model; |
| 4 |
|
| 5 |
use function Code_Snippets\code_snippets_build_tags_array; |
| 6 |
|
| 7 |
/** |
| 8 |
* A snippet object as retrieved from the cloud API. |
| 9 |
* |
| 10 |
* @since 3.4.0 |
| 11 |
* @package Code_Snippets |
| 12 |
* |
| 13 |
* @property int $id The remote ID. |
| 14 |
* @property string $slug The snippet slug. |
| 15 |
* @property string $name The snippet title. |
| 16 |
* @property string $description The formatted description. |
| 17 |
* @property string $code The executable code. |
| 18 |
* @property 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 int $revision The update revision number. |
| 28 |
* @property bool $is_owner If user is owner or author of snippet. |
| 29 |
* @property int|null $local_id The local snippet ID when this cloud snippet has been downloaded. |
| 30 |
* @property bool|null $update_available If synchronised, whether there is an update available on the cloud platform. |
| 31 |
*/ |
| 32 |
class Cloud_Snippet extends Model { |
| 33 |
|
| 34 |
/** |
| 35 |
* List of default values provided for fields. |
| 36 |
* |
| 37 |
* @var array<string, mixed> |
| 38 |
*/ |
| 39 |
protected static array $default_values = [ |
| 40 |
'id' => '', |
| 41 |
'cloud_id' => '', |
| 42 |
'name' => '', |
| 43 |
'description' => '', |
| 44 |
'code' => '', |
| 45 |
'tags' => [], |
| 46 |
'scope' => '', |
| 47 |
'language' => '', |
| 48 |
'status' => '', |
| 49 |
'codevault' => '', |
| 50 |
'total_votes' => '', |
| 51 |
'vote_count' => '', |
| 52 |
'wp_tested' => '', |
| 53 |
'created' => '', |
| 54 |
'updated' => '', |
| 55 |
'revision' => 0, |
| 56 |
'is_owner' => false, |
| 57 |
'local_id' => null, |
| 58 |
'update_available' => null, |
| 59 |
]; |
| 60 |
|
| 61 |
/** |
| 62 |
* Prepare a value before it is stored. |
| 63 |
* |
| 64 |
* @param mixed $value Value to prepare. |
| 65 |
* @param string $field Field name. |
| 66 |
* |
| 67 |
* @return mixed Value in the correct format. |
| 68 |
*/ |
| 69 |
protected function prepare_field( $value, string $field ) { |
| 70 |
switch ( $field ) { |
| 71 |
case 'id': |
| 72 |
case 'revision': |
| 73 |
return absint( $value ); |
| 74 |
|
| 75 |
case 'local_id': |
| 76 |
return is_null( $value ) ? null : absint( $value ); |
| 77 |
|
| 78 |
case 'is_owner': |
| 79 |
return (bool) $value; |
| 80 |
|
| 81 |
case 'description': |
| 82 |
return is_scalar( $value ) ? wp_kses_post( (string) $value ) : ''; |
| 83 |
|
| 84 |
case 'tags': |
| 85 |
return code_snippets_build_tags_array( $value ); |
| 86 |
|
| 87 |
case 'language': |
| 88 |
return is_array( $value ) ? ( $value['name'] ?? '' ) : (string) $value; |
| 89 |
|
| 90 |
case 'name': |
| 91 |
case 'code': |
| 92 |
case 'scope': |
| 93 |
case 'codevault': |
| 94 |
case 'wp_tested': |
| 95 |
case 'created': |
| 96 |
case 'updated': |
| 97 |
return is_scalar( $value ) ? (string) $value : ''; |
| 98 |
|
| 99 |
default: |
| 100 |
return $value; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|