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