cpt-api.php
117 lines
| 1 | <?php |
| 2 | namespace ElementsKit_Lite; |
| 3 | |
| 4 | defined( 'ABSPATH' ) || exit; |
| 5 | |
| 6 | class ElementsKit_Cpt_Api extends Core\Handler_Api { |
| 7 | |
| 8 | public function config() { |
| 9 | $this->prefix = 'dynamic-content'; |
| 10 | $this->param = '/(?P<type>\w+)/(?P<key>\w+(|[-]\w+))/'; |
| 11 | } |
| 12 | |
| 13 | public function get_content_editor() { |
| 14 | |
| 15 | if ( ! is_user_logged_in() ) { |
| 16 | wp_die( |
| 17 | esc_html__( 'You must be logged in.', 'elementskit-lite' ), |
| 18 | 401 |
| 19 | ); |
| 20 | } |
| 21 | |
| 22 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 23 | wp_die( |
| 24 | esc_html__( 'You are not allowed to access this page.', 'elementskit-lite' ), |
| 25 | 403 |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | $content_key = sanitize_key( $this->request['key'] ); |
| 30 | $content_type = sanitize_key( $this->request['type'] ); |
| 31 | $builder_post_title = 'dynamic-content-' . $content_type . '-' . $content_key; |
| 32 | |
| 33 | $builder_post_slug = Utils::get_page_by_title( $builder_post_title, 'elementskit_content' ); |
| 34 | |
| 35 | if ( is_null( $builder_post_slug ) ) { |
| 36 | |
| 37 | // Contributors -> pending |
| 38 | // Authors/Editors/Admins -> publish |
| 39 | $post_status = current_user_can( 'publish_posts' ) |
| 40 | ? 'publish' |
| 41 | : 'pending'; |
| 42 | |
| 43 | $builder_post_id = wp_insert_post( |
| 44 | array( |
| 45 | 'post_content' => '', |
| 46 | 'post_title' => $builder_post_title, |
| 47 | 'post_name' => sanitize_title( $builder_post_title ), |
| 48 | 'post_status' => $post_status, |
| 49 | 'post_type' => 'elementskit_content', |
| 50 | 'post_author' => get_current_user_id(), |
| 51 | ) |
| 52 | ); |
| 53 | |
| 54 | if ( ! is_wp_error( $builder_post_id ) ) { |
| 55 | update_post_meta( |
| 56 | $builder_post_id, |
| 57 | '_wp_page_template', |
| 58 | 'elementor_canvas' |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | } else { |
| 63 | |
| 64 | $builder_post_id = $builder_post_slug->ID; |
| 65 | |
| 66 | // Prevent users editing other users' content unless they have permission. |
| 67 | if ( |
| 68 | ! current_user_can( 'edit_others_posts' ) && |
| 69 | (int) get_post_field( 'post_author', $builder_post_id ) !== get_current_user_id() |
| 70 | ) { |
| 71 | wp_die( |
| 72 | esc_html__( 'You are not allowed to edit this content.', 'elementskit-lite' ), |
| 73 | 403 |
| 74 | ); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // WPML support. |
| 79 | if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { |
| 80 | $builder_post_id = $this->set_wpml_data( $builder_post_id ); |
| 81 | } |
| 82 | |
| 83 | $url = admin_url( |
| 84 | 'post.php?post=' . absint( $builder_post_id ) . '&action=elementor' |
| 85 | ); |
| 86 | |
| 87 | wp_safe_redirect( $url ); |
| 88 | exit; |
| 89 | } |
| 90 | |
| 91 | public function set_wpml_data($builder_post_id) { |
| 92 | global $sitepress; |
| 93 | $default_language = $sitepress->get_default_language(); |
| 94 | $wpml_element_type = apply_filters( 'wpml_element_type', 'elementskit_content' ); |
| 95 | $trid = $sitepress->get_element_trid( $builder_post_id, $wpml_element_type ); |
| 96 | if( ! $trid ) { |
| 97 | $sitepress->set_element_language_details( $builder_post_id, $wpml_element_type, false, $default_language, null, false ); |
| 98 | } |
| 99 | |
| 100 | // get wpml post by language code |
| 101 | $referer = wp_get_referer(); |
| 102 | $referer = wp_parse_url($referer); |
| 103 | $referer = !empty($referer['query']) ? $referer['query'] : ''; |
| 104 | $referer = parse_str($referer, $referer_args); |
| 105 | |
| 106 | if( !empty($referer_args['post']) ) { |
| 107 | $language_details = apply_filters( 'wpml_post_language_details', NULL, $referer_args['post'] ); |
| 108 | if( !is_wp_error($language_details) ) { |
| 109 | $builder_post_id = apply_filters( 'wpml_object_id', $builder_post_id, 'elementskit_content', true, $language_details['language_code'] ); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return $builder_post_id; |
| 114 | } |
| 115 | } |
| 116 | new ElementsKit_Cpt_Api(); |
| 117 |