common.php
126 lines
| 1 | <?php |
| 2 | namespace ElementsKit_Lite\Modules\Widget_Builder\Api; |
| 3 | |
| 4 | defined( 'ABSPATH' ) || exit; |
| 5 | |
| 6 | class Common extends \ElementsKit_Lite\Core\Handler_Api { |
| 7 | |
| 8 | public function config() { |
| 9 | $this->prefix = 'widget-builder'; |
| 10 | $this->param = '/(?P<id>\w+(|[-]\w+))/'; |
| 11 | } |
| 12 | |
| 13 | private function fix_title( $title ) { |
| 14 | return ( $title == '' ) ? ( 'ElementsKit_Lite Custom Widget #' . time() ) : $title; |
| 15 | } |
| 16 | |
| 17 | private function current_user_can_save_widget() { |
| 18 | return is_user_logged_in() && current_user_can( 'manage_options' ) && ( ! is_multisite() || is_super_admin() ); |
| 19 | } |
| 20 | |
| 21 | public function post_push() { |
| 22 | |
| 23 | if ( ! $this->current_user_can_save_widget() ) { |
| 24 | |
| 25 | return array( |
| 26 | 'success' => false, |
| 27 | 'message' => array( |
| 28 | esc_html__( 'Not enough permission.', 'elementskit-lite' ), |
| 29 | ), |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | $id = $this->request['id']; |
| 34 | $data = json_decode( $this->request['data'] ); |
| 35 | |
| 36 | if ( ! property_exists( $data, 'title' ) || ! property_exists( $data, 'tabs' ) || ! ( $data->tabs instanceof \stdClass ) ) { |
| 37 | return array( |
| 38 | 'success' => false, |
| 39 | 'message' => array( |
| 40 | esc_html__( 'Invalid data.', 'elementskit-lite' ), |
| 41 | ), |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | $title = $this->fix_title( $data->title ); |
| 46 | |
| 47 | $widget_data = array( |
| 48 | 'post_title' => $title, |
| 49 | 'post_status' => 'publish', |
| 50 | 'post_type' => 'elementskit_widget', |
| 51 | ); |
| 52 | |
| 53 | $widget = get_post( $id ); |
| 54 | |
| 55 | if ( $widget == null ) { |
| 56 | $id = wp_insert_post( $widget_data ); |
| 57 | } else { |
| 58 | $widget_data['ID'] = $id; |
| 59 | wp_update_post( $widget_data ); |
| 60 | } |
| 61 | update_post_meta( $id, '_elementor_edit_mode', 'builder' ); |
| 62 | update_post_meta( $id, '_wp_page_template', 'elementor_canvas' ); |
| 63 | |
| 64 | $data->push_id = $id; |
| 65 | |
| 66 | //update_post_meta( $id, '_wp_page_template', 'elementor_canvas' ); |
| 67 | update_post_meta( $id, 'elementskit_custom_widget_data', $data ); |
| 68 | \ElementsKit_Lite\Modules\Widget_Builder\Widget_File::instance()->create( $data, $id ); |
| 69 | |
| 70 | return array( |
| 71 | 'success' => true, |
| 72 | 'message' => array( |
| 73 | esc_html__( 'Widget data saved!', 'elementskit-lite' ), |
| 74 | ), |
| 75 | 'push_id' => $id, |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | public function post_pull() { |
| 80 | |
| 81 | if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) { |
| 82 | |
| 83 | return array( |
| 84 | 'success' => false, |
| 85 | 'message' => array( |
| 86 | esc_html__( 'Not enough permission.', 'elementskit-lite' ), |
| 87 | ), |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | $id = $this->request['id']; |
| 92 | $default = array( |
| 93 | 'title' => 'New Widget', |
| 94 | 'icon' => 'eicon-cog', |
| 95 | 'categories' => array( 'basic' ), |
| 96 | 'push_id' => $id, |
| 97 | 'markup' => '', |
| 98 | 'css' => '', |
| 99 | 'js' => '', |
| 100 | 'css_includes' => '', |
| 101 | 'js_includes' => '', |
| 102 | 'tabs' => array( |
| 103 | 'content' => array(), |
| 104 | 'style' => array(), |
| 105 | 'advanced' => array(), |
| 106 | ), |
| 107 | ); |
| 108 | |
| 109 | $widget_data = get_post_meta( $id, 'elementskit_custom_widget_data', true ); |
| 110 | |
| 111 | return is_object( $widget_data ) ? $widget_data : $default; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | public function post_import() { |
| 116 | |
| 117 | //$id = $this->request['id']; |
| 118 | |
| 119 | return array( |
| 120 | 'success' => true, |
| 121 | 'message' => 'hi', |
| 122 | //'file' => $this->request |
| 123 | ); |
| 124 | } |
| 125 | } |
| 126 |