location = true === Fusion_App()->is_builder || ( isset( $_POST ) && isset( $_POST['fusion_front_end'] ) && $_POST['fusion_front_end'] ) ? 'front' : 'back'; // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput } /** * Creates or returns an instance of this class. * * @static * @access public * @since 3.5 */ public static function get_instance() { // If an instance hasn't been created and set to $instance create an instance and set it to $instance. if ( null === self::$instance ) { self::$instance = new AWB_Demo_Import(); } return self::$instance; } /** * Get the data for ajax website requests. * * @access public * @since 3.5 * @return void */ public function get_ajax_website_data() { check_ajax_referer( 'fusion_load_nonce', 'fusion_load_nonce' ); echo wp_json_encode( $this->get_website_data() ); wp_die(); } /** * Return the demo URL. * * @access public * @since 3.5 * @return string */ public function get_demo_url() { return $this->demo_url; } /** * Get the demo data from REST endpoint. * * @access public * @since 3.5 * @return array */ public function get_website_data() { if ( null !== $this->demo_data ) { return $this->demo_data; } if ( false !== get_transient( 'awb_library_demo' ) ) { $this->demo_data = get_transient( 'awb_library_demo' ); return $this->demo_data; } $response = wp_remote_get( $this->get_demo_url() . '/wp-json/demo/full', [ 'timeout' => 60 ] ); // Exit if error. if ( is_wp_error( $response ) ) { return; } // Get the body. $resources = json_decode( wp_remote_retrieve_body( $response ), true ); if ( is_array( $resources ) && ! isset( $resources['tags'] ) ) { return; } set_transient( 'awb_library_demo', $resources, DAY_IN_SECONDS ); return $resources; } /** * Load website page. * * @access public * @since 3.5 * @return void */ public function load_website_page() { check_ajax_referer( 'fusion_load_nonce', 'fusion_load_nonce' ); if ( ! isset( $_POST['post_id'] ) && '' === $_POST['post_id'] ) { die( -1 ); } if ( ! isset( $_POST['demo_name'] ) && '' === $_POST['demo_name'] ) { die( -1 ); } if ( ! isset( $_POST['page_id'] ) && '' === $_POST['page_id'] ) { die( -1 ); } $demo_name = sanitize_text_field( wp_unslash( $_POST['demo_name'] ) ); $demo_name = str_replace( '_', '-', $demo_name ); $post_id = (int) $_POST['post_id']; $page_id = (int) $_POST['page_id']; $data = [ 'success' => true, 'post_id' => $post_id, ]; $page_ep = $this->page_url . '/' . $demo_name . '/wp-json/wp/v2/pages/' . $page_id . '/'; $response = wp_remote_get( $page_ep, [ 'timeout' => 60 ] ); // Exit if error. if ( is_wp_error( $response ) ) { $data['success'] = false; echo wp_json_encode( $data ); die(); } // Get the body. $resources = json_decode( wp_remote_retrieve_body( $response ), true ); // Set post_content. if ( isset( $resources['content'] ) && isset( $resources['content']['raw'] ) ) { $data['post_content'] = $resources['content']['raw']; } else { $data['success'] = false; // required to have post_content otherwise flag it as error. } // Set page template. if ( isset( $resources['template'] ) && ! empty( $resources['template'] ) ) { $data['page_template'] = $resources['template']; } // TODO: data meta. if ( isset( $resources['avada_media'] ) && is_array( $resources['avada_media'] ) ) { $data['avada_media'] = $resources['avada_media']; } $json_data = wp_json_encode( $data ); die( $json_data ); // phpcs:ignore WordPress.Security.EscapeOutput } /** * Template used for website layouts. * * @access public * @since 3.5 * @return void */ public function library_template() { ?>