PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.0.9
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.0.9
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / includes / Builder / Source.php

Source.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.0.9, at includes/Builder/Source.php

278 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Builder;
4
5 use Templately\Utils\Helper;
6 use WP_Post;
7 use WP_Query;
8
9 class Source {
10 const CPT = 'templately_library';
11 const TYPE_META_KEY = '_templately_template_type';
12 const PLATFORM_META_KEY = '_templately_template_platform';
13
14 public $post_type_object;
15
16 /**
17 * @var ThemeBuilder
18 */
19 protected $builder;
20
21 public function __construct( $builder ) {
22 $this->builder = $builder;
23
24 $this->add_actions();
25 $this->register_post_type();
26 }
27
28 private function add_actions() {
29 add_filter( 'views_edit-' . self::CPT, [ $this, 'admin_print_tabs' ], 100 );
30 add_action( 'in_admin_header', [ $this, 'in_admin_header' ] );
31 if ( is_admin() ) {
32 // add_action( 'manage_posts_extra_tablenav', [ $this, 'extra_table_nav' ] );
33 add_action( 'manage_' . self::CPT . '_posts_custom_column', [ $this, 'custom_column' ], 10, 2 );
34 add_filter( 'manage_' . self::CPT . '_posts_columns', [ $this, 'custom_columns' ] );
35 add_filter( 'post_row_actions', [ $this, 'post_row_actions' ], 10, 2 );
36 add_action( 'admin_footer', [ $this, 'app' ] );
37 }
38
39 add_action( 'save_post', [ $this, 'save_post' ], 11, 3 );
40 }
41
42 /**
43 * If needed to add a filter for platform
44 *
45 * @param $which
46 *
47 * @return void
48 * @suppress 50
49 */
50 public function extra_table_nav( $which ) {
51 if( ! $this->is_edit_screen() ) {
52 return;
53 }
54
55 // if( $which === 'top' ) {
56 //
57 // }
58 }
59
60 /**
61 * Print custom column data (Template Type and Platform)
62 *
63 * @param $column_name
64 * @param $post_id
65 *
66 * @return void
67 */
68 public function custom_column( $column_name, $post_id ) {
69 switch ( $column_name ) {
70 case 'template_type':
71 $types = $this->builder::$templates_manager->get_template_types();
72 $type = get_post_meta( $post_id, self::TYPE_META_KEY, true );
73 if(isset($types[ $type ])){
74 echo call_user_func( [ $types[ $type ], 'get_title' ] );
75 }
76 break;
77 case 'template_platform':
78 echo get_post_meta( $post_id, self::PLATFORM_META_KEY, true );
79 break;
80 }
81 }
82
83 /**
84 * Add custom columns. (Template Type and Platform)
85 *
86 * @param $columns
87 *
88 * @return mixed
89 */
90 public function custom_columns( $columns ) {
91 $old_columns = $columns;
92
93 unset( $columns['date'] );
94 unset( $columns['author'] );
95
96 $columns['template_type'] = __( 'Type', 'templately' );
97 $columns['template_platform'] = __( 'Platform', 'templately' );
98
99 $columns['author'] = $old_columns['author'];
100 $columns['date'] = $old_columns['date'];
101
102 return $columns;
103 }
104
105 /**
106 * Add New Template Button App
107 * @return void
108 */
109 public function in_admin_header() {
110 if ( ! $this->is_edit_screen() ) {
111 return;
112 }
113
114 echo '<div id="templately-theme-builder-admin-header"></div>';
115 }
116
117 /**
118 * Edit conditions button app
119 * @return void
120 */
121 public function app() {
122 if( ! $this->is_edit_screen() ) {
123 return;
124 }
125
126 Helper::views( 'builder/edit-conditions' );
127 }
128
129 private function is_edit_screen(): bool {
130 global $current_screen;
131
132 if ( ! $current_screen ) {
133 return false;
134 }
135
136 return 'edit' === $current_screen->base && self::CPT === $current_screen->post_type;
137 }
138
139 public function post_row_actions( $actions, $post ) {
140 if ( $this->is_edit_screen() ) {
141 $actions['edit-conditions'] = sprintf( '<a data-template_id="%1$s" class="templately-edit-conditions" href="#">%2$s</a>', $post->ID, esc_html__( 'Edit Conditions', 'templately' ) );
142 }
143
144 return $actions;
145 }
146
147 public function save_post( int $post_id, WP_Post $post, bool $update ) {
148 if ( $post->post_type !== self::CPT ) {
149 return;
150 }
151
152 if ( wp_is_post_autosave( $post_id ) ) {
153 return;
154 }
155
156 // FIXME: this is something I need to fix in the future.
157 }
158
159 /**
160 * Register post type
161 *
162 * @return void
163 */
164 public function register_post_type() {
165 $name = esc_html_x( 'Templates', 'Template Library', 'templately' );
166
167 $labels = [
168 'name' => $name,
169 'singular_name' => esc_html_x( 'Template', 'Template Library', 'templately' ),
170 'add_new' => esc_html_x( 'Add New Template', 'Template Library', 'templately' ),
171 'add_new_item' => esc_html_x( 'Add New Template', 'Template Library', 'templately' ),
172 'edit_item' => esc_html_x( 'Edit Template', 'Template Library', 'templately' ),
173 'new_item' => esc_html_x( 'New Template', 'Template Library', 'templately' ),
174 'all_items' => esc_html_x( 'All Templates', 'Template Library', 'templately' ),
175 'view_item' => esc_html_x( 'View Template', 'Template Library', 'templately' ),
176 'search_items' => esc_html_x( 'Search Template', 'Template Library', 'templately' ),
177 'not_found' => esc_html_x( 'No Templates found', 'Template Library', 'templately' ),
178 'not_found_in_trash' => esc_html_x( 'No Templates found in Trash', 'Template Library', 'templately' ),
179 'parent_item_colon' => '',
180 'menu_name' => esc_html_x( 'Templates', 'Template Library', 'templately' ),
181 ];
182
183 $args = [
184 'labels' => $labels,
185 'public' => true,
186 'rewrite' => false,
187 'menu_icon' => 'dashicons-admin-page',
188 'show_ui' => true,
189 'show_in_menu' => false,
190 'show_in_nav_menus' => false,
191 'exclude_from_search' => true,
192 'capability_type' => 'post',
193 'hierarchical' => false,
194 'show_in_rest' => true,
195 'supports' => [ 'title', 'thumbnail', 'author', 'editor', 'elementor' ],
196 ];
197
198 /**
199 * Register template library post type args.
200 *
201 * @param array $args Arguments for registering a post type.
202 */
203
204 $this->post_type_object = register_post_type( self::CPT, $args );
205 }
206
207 /**
208 * Tab menu
209 *
210 * @param $tabs
211 *
212 * @return void
213 */
214 public function admin_print_tabs( $tabs ) {
215 $types = templately()->theme_builder::$templates_manager->get_template_types();
216
217 $template_types = [];
218 $status_args = [ 'post_type' => 'templately_library' ];
219
220 $template_types['all'] = [
221 'url' => add_query_arg( $status_args, 'edit.php' ),
222 'label' => __( 'All', 'templately' )
223 ];
224
225 foreach ( $types as $type_name => $type ) {
226 if( $type::get_property( 'builder' ) === false ) {
227 continue;
228 }
229
230 $status_args['type'] = $type_name;
231 $template_types[ $type_name ] = [
232 'url' => add_query_arg( $status_args, 'edit.php' ),
233 'label' => call_user_func( [ $type, 'get_title' ] )
234 ];
235 }
236
237 $this->builder::$views->get( 'builder/tabs', [ 'tabs' => $tabs, 'template_types' => $template_types ] );
238 return [];
239 }
240
241 /**
242 * Retrieves items.
243 *
244 * @param array $args
245 *
246 * @return array
247 */
248 public function get_items( array $args = [] ): array {
249 $template_types = [];
250
251 if ( ! empty( $args['type'] ) ) {
252 $template_types = $args['type'];
253 unset( $args['type'] );
254 }
255
256 $defaults_args = [
257 'post_type' => self::CPT,
258 'post_status' => 'publish',
259 'posts_per_page' => - 1,
260 'orderby' => 'title',
261 'order' => 'ASC',
262 ];
263
264 if ( ! empty( $template_types ) ) {
265 $defaults_args['meta_query'] = [
266 [
267 'key' => self::TYPE_META_KEY,
268 'value' => $template_types,
269 ]
270 ];
271 }
272
273 $args = wp_parse_args( $args, $defaults_args );
274 $items = new WP_Query( $args );
275
276 return $items->posts;
277 }
278 }