PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.4.7
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.4.7
2.7.6 2.7.5 2.7.4 trunk 1.3 2.0.4 2.0.6 2.1.91 2.2.4 2.2.7 2.2.9 2.3.1 2.3.10 2.4.10 2.4.2 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.6.0 2.6.1 2.7.0 2.7.1 All 27 releases
insert-php / admin / includes / class.api.php

class.api.php in Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts 2.4.7, at admin/includes/class.api.php

323 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Woody API class
5 *
6 * @author Webcraftic <wordpress.webraftic@gmail.com>
7 * @copyright (c) 11.12.2018, Webcraftic
8 * @version 1.0
9 */
10
11 // Exit if accessed directly
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 class WINP_Api extends WINP_Request {
17
18 const WINP_API_SNIPPET = 'snippet';
19 const WINP_API_TYPE = 'type';
20
21 /**
22 * WINP_Api constructor.
23 */
24 public function __construct() {
25 parent::__construct();
26
27 require_once WINP_PLUGIN_DIR . '/includes/jsonmapper/class/snippet.php';
28 require_once WINP_PLUGIN_DIR . '/includes/jsonmapper/class/type.php';
29 }
30
31 /**
32 * Set page parameters
33 *
34 * @param $json
35 *
36 * @return bool
37 */
38 private function set_page_params( $json ) {
39 if ( ! $this->check_response( $json ) ) {
40 return false;
41 }
42
43 if ( empty( $json['headers'] ) ) {
44 return false;
45 }
46
47 global $winp_api_total_items;
48
49 $winp_api_total_items = isset( $json['headers']['x-pagination-total-count'] ) ? $json['headers']['x-pagination-total-count'] : 0;
50
51 return true;
52 }
53
54 /**
55 * Get total items for last query
56 *
57 * @return int
58 */
59 public function get_total_items() {
60 global $winp_api_total_items;
61
62 return $winp_api_total_items;
63 }
64
65 /**
66 * Get all snippets
67 *
68 * @param boolean $common - если true, то выводить общие сниппеты без привязки к пользователю
69 * @param array $parameters
70 *
71 * @return bool|mixed
72 */
73 public function get_all_snippets( $common = false, $parameters = [] ) {
74 $url = $common ? 'common' : self::WINP_API_SNIPPET;
75 $args = $parameters ? '&' . implode( '&', $parameters ) : '';
76 $json = $this->get( $url . '?expand=type' . $args );
77
78 $this->set_page_params( $json );
79
80 return $this->map_objects( $json, 'WINP\JsonMapper\Snippet' );
81 }
82
83 /**
84 * Get snippet
85 *
86 * @param integer $id
87 * @param boolean $common - если true, то запрос на общий сниппет
88 *
89 * @return bool|mixed
90 */
91 public function get_snippet( $id, $common = false ) {
92 $url = $common ? 'common' : self::WINP_API_SNIPPET;
93 $json = $this->get( $url . '/view?id=' . $id . '&expand=type' );
94
95 $snippet = $this->map_object( $json, 'WINP\JsonMapper\Snippet' );
96 $snippet->execute_everywhere = $snippet->execute_everywhere ? 'evrywhere' : 'shortcode';
97 return $snippet;
98 }
99
100 /**
101 * Create snippet
102 *
103 * @param string $title
104 * @param string $content
105 * @param string $description
106 * @param integer $type_id
107 *
108 * @return bool|mixed
109 */
110 public function create_snippet( $title, $content, $description, $type_id ) {
111 $args = [
112 'body' => [
113 'title' => $title,
114 'content' => $content,
115 'description' => $description,
116 'type_id' => $type_id, // Тип снипета
117 ],
118 ];
119
120 $json = $this->post( self::WINP_API_SNIPPET . '/create', $args );
121
122 return $this->map_object( $json, 'WINP\JsonMapper\Snippet' );
123 }
124
125 /**
126 * Update snippet
127 *
128 * @param integer $id
129 * @param string $title
130 * @param string $content
131 * @param string $description
132 * @param integer $type_id
133 *
134 * @return bool|mixed
135 */
136 public function update_snippet( $id, $title, $content, $description, $type_id ) {
137 $args = [
138 'body' => [
139 'title' => $title,
140 'content' => $content,
141 'description' => $description,
142 'type_id' => $type_id, // Тип снипета
143 ],
144 ];
145
146 $json = $this->put( self::WINP_API_SNIPPET . '/update/?id=' . $id, $args );
147
148 return $this->map_object( $json, 'WINP\JsonMapper\Snippet' );
149 }
150
151 /**
152 * Delete snippet
153 *
154 * @param integer $id
155 *
156 * @return boolean
157 */
158 public function delete_snippet( $id ) {
159 $json = $this->post( self::WINP_API_SNIPPET . '/delete/?id=' . $id );
160
161 if ( 200 == $json['response']['code'] || 204 == $json['response']['code'] ) {
162 return true;
163 }
164
165 return false;
166 }
167
168 /**
169 * Get all types
170 *
171 * @return object|boolean
172 */
173 public function get_all_types() {
174 $json = $this->get( self::WINP_API_TYPE );
175
176 return $this->map_objects( $json, 'WINP\JsonMapper\Type' );
177 }
178
179 /**
180 * Get type
181 *
182 * @param $id
183 *
184 * @return object|boolean
185 */
186 public function get_type( $id ) {
187 $json = $this->get( self::WINP_API_TYPE . '/view/?id=' . $id );
188
189 return $this->map_object( $json, 'WINP\JsonMapper\Type' );
190 }
191
192 /**
193 * Check if snippet changed
194 *
195 * @param $post_id
196 *
197 * @return bool
198 */
199 public function is_changed( $post_id ) {
200 $data = get_post_meta( $post_id, WINP_Plugin::app()->getPrefix() . 'snippet_check_data', true );
201 if ( ! empty( $data ) && isset( $data['content'] ) ) {
202 $post = get_post( $post_id );
203
204 return $data['content'] != $post->post_content || WINP_Helper::getMetaOption( $post->ID, 'snippet_description' ) != $data['description'];
205 } else {
206 return true;
207 }
208 }
209
210 /**
211 * Get tipy id by type title
212 *
213 * @param $type_title
214 *
215 * @return int
216 */
217 private function get_type_id_by_type( $type_title ) {
218 if ( $type_title ) {
219 $types = $this->get_all_types();
220 if ( ! empty( $types ) && is_array( $types ) ) {
221 foreach ( $types as $type ) {
222 if ( $type_title == $type->slug ) {
223 return $type->id;
224 }
225 }
226 }
227 }
228
229 return 0;
230 }
231
232 /**
233 * Snippet synchronization
234 *
235 * @param $id
236 * @param $name
237 *
238 * @return bool|string
239 */
240 public function synchronization( $id, $name ) {
241 $post = get_post( $id );
242
243 if ( $post ) {
244 $type_id = WINP_Helper::getMetaOption( $post->ID, 'snippet_api_type', 0 );
245
246 if ( ! $type_id ) {
247 $type = WINP_Helper::get_snippet_type( $post->ID );
248 $type_id = $this->get_type_id_by_type( $type );
249 }
250
251 if ( $type_id ) {
252 $title = ! empty( $name ) ? $name : $post->post_title;
253 $description = WINP_Helper::getMetaOption( $post->ID, 'snippet_description', '' );
254 $snippet_code = WINP_Helper::get_snippet_code( $post );
255 $snippet = $this->create_snippet( $title, $snippet_code, $description, $type_id );
256
257 if ( $snippet ) {
258 $data = [
259 'content' => $snippet_code,
260 'description' => $description,
261 ];
262 WINP_Helper::updateMetaOption( $post->ID, 'snippet_check_data', $data );
263 WINP_Helper::updateMetaOption( $post->ID, 'snippet_api_snippet', $snippet->id );
264 WINP_Helper::updateMetaOption( $post->ID, 'snippet_api_type', $type_id );
265
266 return true;
267 }
268
269 return __( 'Synchronization snippet error', 'insert-php' );
270 }
271
272 return __( 'Unknown sippet type', 'insert-php' );
273 }
274
275 return false;
276 }
277
278 /**
279 * Create snippet from library
280 *
281 * @param integer $snippet_id
282 * @param integer $post_id
283 * @param boolean $common
284 *
285 * @return bool|integer
286 */
287 public function create_from_library( $snippet_id, $post_id, $common ) {
288 if ( $snippet_id ) {
289 $snippet = $this->get_snippet( $snippet_id, $common );
290 if ( $snippet ) {
291 if ( ! $post_id ) {
292 $post = [
293 'post_title' => $snippet->title,
294 'post_content' => $snippet->content,
295 'post_type' => WINP_SNIPPETS_POST_TYPE,
296 'post_status' => 'publish',
297 ];
298 $post_id = wp_insert_post( $post );
299 WINP_Helper::updateMetaOption( $post_id, 'snippet_activate', 0 );
300 } else {
301 $post = [
302 'ID' => $post_id,
303 'post_title' => $snippet->title,
304 'post_content' => $snippet->content,
305 ];
306 wp_update_post( $post );
307 }
308
309 WINP_Helper::updateMetaOption( $post_id, 'snippet_api_snippet', $snippet_id );
310 WINP_Helper::updateMetaOption( $post_id, 'snippet_type', $snippet->type->slug );
311 WINP_Helper::updateMetaOption( $post_id, 'snippet_api_type', $snippet->type_id );
312 WINP_Helper::updateMetaOption( $post_id, 'snippet_description', $snippet->description );
313 WINP_Helper::updateMetaOption( $post_id, 'snippet_scope', $snippet->execute_everywhere );
314
315 return $post_id;
316 }
317 }
318
319 return false;
320 }
321
322 }
323