← All changes
|
backend/src/Controllers/Cloud/Libraries/LibraryItemCodeController.php
+82
-82
1.5.5
→
1.5.5.1
View file →
| @@ -1,82 +1,82 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace FL\Assistant\Controllers\Cloud\Libraries; | |
| 4 | - | |
| 5 | -use FL\Assistant\System\Contracts\ControllerAbstract; | |
| 6 | - | |
| 7 | -class LibraryItemCodeController extends ControllerAbstract { | |
| 8 | - | |
| 9 | - public function register_routes() { | |
| 10 | - $this->route( | |
| 11 | - '/library-items/import/code', [ | |
| 12 | - [ | |
| 13 | - 'methods' => \WP_REST_Server::CREATABLE, | |
| 14 | - 'callback' => [ $this, 'import' ], | |
| 15 | - 'permission_callback' => function () { | |
| 16 | - return current_user_can( 'edit_others_posts' ); | |
| 17 | - }, | |
| 18 | - ], | |
| 19 | - ] | |
| 20 | - ); | |
| 21 | - } | |
| 22 | - | |
| 23 | - /** | |
| 24 | - * Imports a code from a library to the site. | |
| 25 | - * | |
| 26 | - * @param object $request | |
| 27 | - * @return array | |
| 28 | - */ | |
| 29 | - public function import( $request ) { | |
| 30 | - $item = $request->get_param( 'item' ); | |
| 31 | - | |
| 32 | - if ( ! $item ) { | |
| 33 | - return rest_ensure_response( | |
| 34 | - [ | |
| 35 | - 'error' => true, | |
| 36 | - ] | |
| 37 | - ); | |
| 38 | - } | |
| 39 | - | |
| 40 | - $post_name = $item['name']; | |
| 41 | - $description = $item['description']; | |
| 42 | - $extension = $item['data']['extension']; | |
| 43 | - $code = $item['data']['content']; | |
| 44 | - | |
| 45 | - if ( 'js' !== $extension && 'css' !== $extension ) { | |
| 46 | - | |
| 47 | - return rest_ensure_response( [ 'error' => __( 'Import is only allowed for CSS and JS extensions.' ) ] ); | |
| 48 | - } | |
| 49 | - | |
| 50 | - if( 'js' === $extension ) { | |
| 51 | - $extension = 'JavaScript'; | |
| 52 | - } | |
| 53 | - | |
| 54 | - if( 'css' === $extension ) { | |
| 55 | - $extension = 'CSS'; | |
| 56 | - } | |
| 57 | - | |
| 58 | - $new_post_id = wp_insert_post( | |
| 59 | - [ | |
| 60 | - 'post_title' => $post_name, | |
| 61 | - 'post_type' => 'fl_code', | |
| 62 | - 'post_author' => wp_get_current_user()->ID, | |
| 63 | - 'post_content' => $description, | |
| 64 | - 'post_status' => 'publish', | |
| 65 | - 'meta_input' => [ | |
| 66 | - '_fl_asst_code_type' => $extension, | |
| 67 | - '_fl_asst_code' => $code, | |
| 68 | - ] | |
| 69 | - ] | |
| 70 | - ); | |
| 71 | - | |
| 72 | - if ( is_wp_error( $new_post_id ) ) { | |
| 73 | - return rest_ensure_response( | |
| 74 | - [ | |
| 75 | - 'error' => true, | |
| 76 | - ] | |
| 77 | - ); | |
| 78 | - } | |
| 79 | - | |
| 80 | - return rest_ensure_response( [ 'success' => true ] ); | |
| 81 | - } | |
| 82 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace FL\Assistant\Controllers\Cloud\Libraries; | |
| 4 | + | |
| 5 | +use FL\Assistant\System\Contracts\ControllerAbstract; | |
| 6 | + | |
| 7 | +class LibraryItemCodeController extends ControllerAbstract { | |
| 8 | + | |
| 9 | + public function register_routes() { | |
| 10 | + $this->route( | |
| 11 | + '/library-items/import/code', [ | |
| 12 | + [ | |
| 13 | + 'methods' => \WP_REST_Server::CREATABLE, | |
| 14 | + 'callback' => [ $this, 'import' ], | |
| 15 | + 'permission_callback' => function () { | |
| 16 | + return current_user_can( 'edit_others_posts' ); | |
| 17 | + }, | |
| 18 | + ], | |
| 19 | + ] | |
| 20 | + ); | |
| 21 | + } | |
| 22 | + | |
| 23 | + /** | |
| 24 | + * Imports a code from a library to the site. | |
| 25 | + * | |
| 26 | + * @param object $request | |
| 27 | + * @return array | |
| 28 | + */ | |
| 29 | + public function import( $request ) { | |
| 30 | + $item = $request->get_param( 'item' ); | |
| 31 | + | |
| 32 | + if ( ! $item ) { | |
| 33 | + return rest_ensure_response( | |
| 34 | + [ | |
| 35 | + 'error' => true, | |
| 36 | + ] | |
| 37 | + ); | |
| 38 | + } | |
| 39 | + | |
| 40 | + $post_name = $item['name']; | |
| 41 | + $description = $item['description']; | |
| 42 | + $extension = $item['data']['extension']; | |
| 43 | + $code = $item['data']['content']; | |
| 44 | + | |
| 45 | + if ( 'js' !== $extension && 'css' !== $extension ) { | |
| 46 | + | |
| 47 | + return rest_ensure_response( [ 'error' => __( 'Import is only allowed for CSS and JS extensions.' ) ] ); | |
| 48 | + } | |
| 49 | + | |
| 50 | + if( 'js' === $extension ) { | |
| 51 | + $extension = 'JavaScript'; | |
| 52 | + } | |
| 53 | + | |
| 54 | + if( 'css' === $extension ) { | |
| 55 | + $extension = 'CSS'; | |
| 56 | + } | |
| 57 | + | |
| 58 | + $new_post_id = wp_insert_post( | |
| 59 | + [ | |
| 60 | + 'post_title' => $post_name, | |
| 61 | + 'post_type' => 'fl_code', | |
| 62 | + 'post_author' => wp_get_current_user()->ID, | |
| 63 | + 'post_content' => $description, | |
| 64 | + 'post_status' => 'publish', | |
| 65 | + 'meta_input' => [ | |
| 66 | + '_fl_asst_code_type' => $extension, | |
| 67 | + '_fl_asst_code' => $code, | |
| 68 | + ] | |
| 69 | + ] | |
| 70 | + ); | |
| 71 | + | |
| 72 | + if ( is_wp_error( $new_post_id ) ) { | |
| 73 | + return rest_ensure_response( | |
| 74 | + [ | |
| 75 | + 'error' => true, | |
| 76 | + ] | |
| 77 | + ); | |
| 78 | + } | |
| 79 | + | |
| 80 | + return rest_ensure_response( [ 'success' => true ] ); | |
| 81 | + } | |
| 82 | +} | |