PluginProbe
HT Builder – WordPress Theme Builder for Elementor / 1.2.4
HT Builder – WordPress Theme Builder for Elementor v1.2.4
1.3.5 1.3.4 trunk 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.8 1.0.9 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3
ht-builder / includes / admin / template-library.php

template-library.php in HT Builder – WordPress Theme Builder for Elementor 1.2.4, at includes/admin/template-library.php

227 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly.
4
5 class HTBuilder_Template_Library{
6
7 const TRANSIENT_KEY = 'htbuilder_template_info';
8
9 public static $endpoint = HTBUILDER_PL_URL.'includes/admin/json/layoutinfofree.json';
10 public static $templateapi = 'https://api.hasthemes.com/api/htbuilder/layouts-free/%s.json';
11
12 public static $api_args = [];
13
14 private static $_instance = null;
15 public static function instance(){
16 if( is_null( self::$_instance ) ){
17 self::$_instance = new self();
18 }
19 return self::$_instance;
20 }
21
22 function __construct(){
23
24 if ( is_admin() ) {
25 add_action( 'admin_menu', [ $this, 'admin_menu' ], 225 );
26 add_action( 'wp_ajax_htbuilder_ajax_request', [ $this, 'templates_ajax_request' ] );
27 add_action( 'wp_ajax_nopriv_htbuilder_ajax_request', [ $this, 'templates_ajax_request' ] );
28 }
29 add_action( 'admin_enqueue_scripts', [ $this, 'scripts' ] );
30
31 self::$api_args = [
32 'plugin_version' => HTBUILDER_VERSION,
33 'url' => home_url(),
34 ];
35
36 }
37
38 // Setter Endpoint
39 function set_api_endpoint( $endpoint ){
40 self::$endpoint = $endpoint;
41 }
42
43 // Setter Template API
44 function set_api_templateapi( $templateapi ){
45 self::$templateapi = $templateapi;
46 }
47
48 // Plugins Library Register
49 function admin_menu() {
50 add_submenu_page(
51 'htbuilder',
52 __( 'Templates Library', 'ht-builder' ),
53 __( 'Templates Library', 'ht-builder' ),
54 'manage_options',
55 'htbuilder_templates_library',
56 array ( $this, 'library_render_html' )
57 );
58 }
59
60 function library_render_html(){
61 require_once HTBUILDER_PL_PATH . 'includes/admin/templates_list.php';
62 }
63
64 public static function request_remote_templates_info( $force_update ) {
65 global $wp_version;
66 $body_args = apply_filters( 'htbuildertemplates/api/get_templates/body_args', self::$api_args );
67 $request = wp_remote_get(
68 self::$endpoint,
69 [
70 'timeout' => $force_update ? 25 : 10,
71 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url(),
72 'body' => $body_args,
73 'sslverify' => false,
74 ]
75 );
76 if ( is_wp_error( $request ) || 200 !== (int) wp_remote_retrieve_response_code( $request ) ) {
77 return [];
78 }
79
80 $response = json_decode( wp_remote_retrieve_body( $request ), true );
81 return $response;
82 }
83
84 /**
85 * Retrieve template library and save as a transient.
86 */
87 public static function set_templates_info( $force_update = false ) {
88 $transient = get_transient( self::TRANSIENT_KEY );
89
90 if ( ! $transient || $force_update ) {
91 $info = self::request_remote_templates_info( $force_update );
92 set_transient( self::TRANSIENT_KEY, $info, DAY_IN_SECONDS );
93 }
94 }
95
96 /**
97 * Get template info.
98 */
99 public function get_templates_info( $force_update = false ) {
100 if ( ! get_transient( self::TRANSIENT_KEY ) || $force_update ) {
101 self::set_templates_info( true );
102 }
103 return get_transient( self::TRANSIENT_KEY );
104 }
105
106 /**
107 * Admin Scripts.
108 */
109 public function scripts( $hook ) {
110
111 // wp core styles
112 wp_enqueue_style( 'wp-jquery-ui-dialog' );
113 // wp core scripts
114 wp_enqueue_script( 'jquery-ui-dialog' );
115
116 if( $hook == 'ht-builder_page_htbuilder_templates_library' ){
117
118 // CSS
119 wp_enqueue_style( 'htbuilder-stapel', HTBUILDER_PL_URL . 'includes/admin/assets/lib/css/stapel.css', false, HTBUILDER_VERSION );
120
121 wp_enqueue_script(
122 'htbuilder-modernizr',
123 HTBUILDER_PL_URL . 'includes/admin/assets/lib/js/modernizr.custom.63321.js',
124 [
125 'jquery',
126 ],
127 false
128 );
129
130 wp_enqueue_script(
131 'htbuilder-stapel',
132 HTBUILDER_PL_URL . 'includes/admin/assets/lib/js/jquery.stapel.js',
133 [
134 'jquery',
135 ],
136 true
137 );
138
139 // JS
140 wp_enqueue_script(
141 'htbuilder-templates',
142 HTBUILDER_PL_URL . 'includes/admin/assets/js/admin-ajax.js',
143 [
144 'jquery',
145 ],
146 true
147 );
148
149 }
150 $current_user = wp_get_current_user();
151
152 wp_localize_script(
153 'htbuilder-templates',
154 'HTTM',
155 [
156 'ajaxurl' => admin_url( 'admin-ajax.php' ),
157 'adminURL' => admin_url(),
158 'elementorURL' => admin_url( 'edit.php?post_type=elementor_library' ),
159 'version' => HTBUILDER_VERSION,
160 'pluginURL' => plugin_dir_url( __FILE__ ),
161 'packagedesc' => __( 'Templates in this package', 'ht-builder' ),
162 'user' => [
163 'email' => $current_user->user_email,
164 ]
165 ]
166 );
167
168 }
169
170 /**
171 * Ajax request.
172 */
173 function templates_ajax_request(){
174
175 if ( isset( $_REQUEST ) ) {
176
177 $template_id = $_REQUEST['httemplateid'];
178 $page_title = $_REQUEST['pagetitle'];
179
180 $templateurl = sprintf( self::$templateapi, $template_id );
181
182 $response_data = $this->templates_get_content_remote_request( $templateurl );
183
184 $defaulttitle = !empty( $response_data['title'] ) ? $response_data['title'] : __( 'New Template', 'ht-builder' );
185
186 $args = [
187 'post_type' => !empty( $page_title ) ? 'page' : 'elementor_library',
188 'post_status' => !empty( $page_title ) ? 'draft' : 'publish',
189 'post_title' => !empty( $page_title ) ? $page_title : $defaulttitle,
190 'post_content' => '',
191 ];
192 $new_post_id = wp_insert_post( $args );
193
194 update_post_meta( $new_post_id, '_elementor_data', $response_data['content'] );
195 update_post_meta( $new_post_id, '_elementor_page_settings', $response_data['page_settings'] );
196 update_post_meta( $new_post_id, '_elementor_template_type', $response_data['type'] );
197 update_post_meta( $new_post_id, '_elementor_edit_mode', 'builder' );
198
199 if ( $new_post_id && ! is_wp_error( $new_post_id ) ) {
200 update_post_meta( $new_post_id, '_wp_page_template', ! empty( $response_data['page_template'] ) ? $response_data['page_template'] : 'elementor_header_footer' );
201 }
202
203 echo json_encode(
204 array(
205 'id' => $new_post_id,
206 'edittxt' => esc_html__( 'Edit Template', 'ht-builder' )
207 )
208 );
209 }
210
211 die();
212 }
213
214 function templates_get_content_remote_request( $templateurl ){
215 $url = $templateurl;
216 $response = wp_remote_get( $url, array(
217 'timeout' => 60,
218 'sslverify' => false
219 ) );
220 $result = json_decode( wp_remote_retrieve_body( $response ), true );
221 return $result;
222 }
223
224
225 }
226
227 HTBuilder_Template_Library::instance();