PluginProbe
Code Snippets / 4.0.0-beta.1
Code Snippets v4.0.0-beta.1
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / php / Model / Cloud_Snippet.php

Cloud_Snippet.php in Code Snippets 4.0.0-beta.1, at php/Model/Cloud_Snippet.php

86 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $name The snippet title.
15 * @property string $description The formatted description.
16 * @property string $code The executable code.
17 * @property array<string> $tags An array of the tags.
18 * @property string $scope The scope name.
19 * @property string $codevault Name of user codevault.
20 * @property string $total_votes The total number of votes.
21 * @property string $vote_count The number of actual votes.
22 * @property string $wp_tested Tested with WP version.
23 * @property string $status Snippet Status ID.
24 * @property string $created The date and time when the snippet data was first created, in ISO format.
25 * @property string $updated When the snippet was last updated, in ISO format.
26 * @property int $revision The update revision number.
27 * @property bool $is_owner If user is owner or author of snippet.
28 */
29 class Cloud_Snippet extends Model {
30
31 /**
32 * List of default values provided for fields.
33 *
34 * @var array<string, mixed>
35 */
36 protected static array $default_values = [
37 'id' => '',
38 'cloud_id' => '',
39 'name' => '',
40 'description' => '',
41 'code' => '',
42 'tags' => [],
43 'scope' => '',
44 'language' => '',
45 'status' => '',
46 'codevault' => '',
47 'total_votes' => '',
48 'vote_count' => '',
49 'wp_tested' => '',
50 'created' => '',
51 'updated' => '',
52 'revision' => 0,
53 'is_owner' => false,
54 'shared_network' => false,
55 ];
56
57 /**
58 * Prepare a value before it is stored.
59 *
60 * @param mixed $value Value to prepare.
61 * @param string $field Field name.
62 *
63 * @return mixed Value in the correct format.
64 */
65 protected function prepare_field( $value, string $field ) {
66 switch ( $field ) {
67 case 'id':
68 case 'revision':
69 return absint( $value );
70
71 case 'is_owner':
72 return (bool) $value;
73 case 'description':
74 return ( null === $value ) ? '' : $value;
75 case 'tags':
76 return code_snippets_build_tags_array( $value );
77
78 case 'language':
79 return is_array( $value ) ? ( $value['name'] ?? '' ) : (string) $value;
80
81 default:
82 return $value;
83 }
84 }
85 }
86