| @@ -2,10 +2,8 @@ | ||
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | 4 | * Woody API class |
| 5 | 5 | * |
| 6 | - * @author Webcraftic <wordpress.webraftic@gmail.com> | |
| 7 | - * @copyright (c) 11.12.2018, Webcraftic | |
| 8 | 6 | * @version 1.0 |
| 9 | 7 | */ |
| 10 | 8 | |
| 11 | 9 | // Exit if accessed directly |
| @@ -15,21 +13,10 @@ | ||
| 15 | 13 | |
| 16 | 14 | class WINP_Api extends WINP_Request { |
| 17 | 15 | |
| 18 | 16 | const WINP_API_SNIPPET = 'snippet'; |
| 19 | - const WINP_API_TYPE = 'type'; | |
| 20 | 17 | |
| 21 | 18 | /** |
| 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 | 19 | * Set page parameters |
| 33 | 20 | * |
| 34 | 21 | * @param $json |
| 35 | 22 | * |
| @@ -70,8 +57,12 @@ | ||
| 70 | 57 | * |
| 71 | 58 | * @return bool|mixed |
| 72 | 59 | */ |
| 73 | 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 | + | |
| 74 | 65 | $url = $common ? 'common' : self::WINP_API_SNIPPET; |
| 75 | 66 | $args = $parameters ? '&' . implode( '&', $parameters ) : ''; |
| 76 | 67 | $json = $this->get( $url . '?expand=type' . $args ); |
| 77 | 68 | |
| @@ -76,9 +67,9 @@ | ||
| 76 | 67 | $json = $this->get( $url . '?expand=type' . $args ); |
| 77 | 68 | |
| 78 | 69 | $this->set_page_params( $json ); |
| 79 | 70 | |
| 80 | - return $this->map_objects( $json, 'WINP\JsonMapper\Snippet' ); | |
| 71 | + return $this->map_objects( $json, 'WINP_DTO_Snippet' ); | |
| 81 | 72 | } |
| 82 | 73 | |
| 83 | 74 | /** |
| 84 | 75 | * Get snippet |
| @@ -91,10 +82,12 @@ | ||
| 91 | 82 | public function get_snippet( $id, $common = false ) { |
| 92 | 83 | $url = $common ? 'common' : self::WINP_API_SNIPPET; |
| 93 | 84 | $json = $this->get( $url . '/view?id=' . $id . '&expand=type' ); |
| 94 | 85 | |
| 95 | - $snippet = $this->map_object( $json, 'WINP\JsonMapper\Snippet' ); | |
| 96 | - $snippet->execute_everywhere = $snippet->execute_everywhere ? 'evrywhere' : 'shortcode'; | |
| 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 | + } | |
| 97 | 90 | return $snippet; |
| 98 | 91 | } |
| 99 | 92 | |
| 100 | 93 | /** |
| @@ -118,9 +111,9 @@ | ||
| 118 | 111 | ]; |
| 119 | 112 | |
| 120 | 113 | $json = $this->post( self::WINP_API_SNIPPET . '/create', $args ); |
| 121 | 114 | |
| 122 | - return $this->map_object( $json, 'WINP\JsonMapper\Snippet' ); | |
| 115 | + return $this->map_object( $json, 'WINP_DTO_Snippet' ); | |
| 123 | 116 | } |
| 124 | 117 | |
| 125 | 118 | /** |
| 126 | 119 | * Update snippet |
| @@ -144,9 +137,9 @@ | ||
| 144 | 137 | ]; |
| 145 | 138 | |
| 146 | 139 | $json = $this->put( self::WINP_API_SNIPPET . '/update/?id=' . $id, $args ); |
| 147 | 140 | |
| 148 | - return $this->map_object( $json, 'WINP\JsonMapper\Snippet' ); | |
| 141 | + return $this->map_object( $json, 'WINP_DTO_Snippet' ); | |
| 149 | 142 | } |
| 150 | 143 | |
| 151 | 144 | /** |
| 152 | 145 | * Delete snippet |
| @@ -167,27 +160,60 @@ | ||
| 167 | 160 | |
| 168 | 161 | /** |
| 169 | 162 | * Get all types |
| 170 | 163 | * |
| 171 | - * @return object|boolean | |
| 164 | + * @return array<object> | |
| 172 | 165 | */ |
| 173 | 166 | public function get_all_types() { |
| 174 | - $json = $this->get( self::WINP_API_TYPE ); | |
| 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 | + ]; | |
| 175 | 204 | |
| 176 | - return $this->map_objects( $json, 'WINP\JsonMapper\Type' ); | |
| 177 | - } | |
| 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 | + } | |
| 178 | 214 | |
| 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' ); | |
| 215 | + return $types; | |
| 190 | 216 | } |
| 191 | 217 | |
| 192 | 218 | /** |
| 193 | 219 | * Check if snippet changed |
| @@ -196,9 +222,9 @@ | ||
| 196 | 222 | * |
| 197 | 223 | * @return bool |
| 198 | 224 | */ |
| 199 | 225 | public function is_changed( $post_id ) { |
| 200 | - $data = get_post_meta( $post_id, WINP_Plugin::app()->getPrefix() . 'snippet_check_data', true ); | |
| 226 | + $data = get_post_meta( $post_id, 'wbcr_inp_snippet_check_data', true ); | |
| 201 | 227 | if ( ! empty( $data ) && isset( $data['content'] ) ) { |
| 202 | 228 | $post = get_post( $post_id ); |
| 203 | 229 | |
| 204 | 230 | return $data['content'] != $post->post_content || WINP_Helper::getMetaOption( $post->ID, 'snippet_description' ) != $data['description']; |
| @@ -216,13 +242,12 @@ | ||
| 216 | 242 | */ |
| 217 | 243 | private function get_type_id_by_type( $type_title ) { |
| 218 | 244 | if ( $type_title ) { |
| 219 | 245 | $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 | - } | |
| 246 | + | |
| 247 | + foreach ( $types as $type ) { | |
| 248 | + if ( property_exists( $type, 'slug' ) && property_exists( $type, 'id' ) && $type_title === $type->slug ) { | |
| 249 | + return $type->id; | |
| 225 | 250 | } |
| 226 | 251 | } |
| 227 | 252 | } |
| 228 | 253 | |
| @@ -265,12 +290,12 @@ | ||
| 265 | 290 | |
| 266 | 291 | return true; |
| 267 | 292 | } |
| 268 | 293 | |
| 269 | - return __( 'Synchronization snippet error', 'insert-php' ); | |
| 294 | + return __( 'Snippet synchronization error', 'insert-php' ); | |
| 270 | 295 | } |
| 271 | 296 | |
| 272 | - return __( 'Unknown sippet type', 'insert-php' ); | |
| 297 | + return __( 'Unknown snippet type', 'insert-php' ); | |
| 273 | 298 | } |
| 274 | 299 | |
| 275 | 300 | return false; |
| 276 | 301 | } |
| @@ -317,6 +342,5 @@ | ||
| 317 | 342 | } |
| 318 | 343 | |
| 319 | 344 | return false; |
| 320 | 345 | } |
| 321 | - | |
| 322 | 346 | } |