PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.0.4
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.0.4
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.4, at includes/Builder/Source.php

275 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' ] );
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 echo call_user_func( [ $types[ $type ], 'get_title' ] );
74 break;
75 case 'template_platform':
76 echo get_post_meta( $post_id, self::PLATFORM_META_KEY, true );
77 break;
78 }
79 }
80
81 /**
82 * Add custom columns. (Template Type and Platform)
83 *
84 * @param $columns
85 *
86 * @return mixed
87 */
88 public function custom_columns( $columns ) {
89 $old_columns = $columns;
90
91 unset( $columns['date'] );
92 unset( $columns['author'] );
93
94 $columns['template_type'] = __( 'Type', 'templately' );
95 $columns['template_platform'] = __( 'Platform', 'templately' );
96
97 $columns['author'] = $old_columns['author'];
98 $columns['date'] = $old_columns['date'];
99
100 return $columns;
101 }
102
103 /**
104 * Add New Template Button App
105 * @return void
106 */
107 public function in_admin_header() {
108 if ( ! $this->is_edit_screen() ) {
109 return;
110 }
111
112 echo '<div id="templately-theme-builder-admin-header"></div>';
113 }
114
115 /**
116 * Edit conditions button app
117 * @return void
118 */
119 public function app() {
120 if( ! $this->is_edit_screen() ) {
121 return;
122 }
123
124 Helper::views( 'builder/edit-conditions' );
125 }
126
127 private function is_edit_screen(): bool {
128 global $current_screen;
129
130 if ( ! $current_screen ) {
131 return false;
132 }
133
134 return 'edit' === $current_screen->base && self::CPT === $current_screen->post_type;
135 }
136
137 public function post_row_actions( $actions, $post ) {
138 if ( $this->is_edit_screen() ) {
139 $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' ) );
140 }
141
142 return $actions;
143 }
144
145 public function save_post( int $post_id, WP_Post $post, bool $update ) {
146 if ( $post->post_type !== self::CPT ) {
147 return;
148 }
149
150 if ( wp_is_post_autosave( $post_id ) ) {
151 return;
152 }
153
154 // FIXME: this is something I need to fix in the future.
155 }
156
157 /**
158 * Register post type
159 *
160 * @return void
161 */
162 public function register_post_type() {
163 $name = esc_html_x( 'Templates', 'Template Library', 'templately' );
164
165 $labels = [
166 'name' => $name,
167 'singular_name' => esc_html_x( 'Template', 'Template Library', 'templately' ),
168 'add_new' => esc_html_x( 'Add New Template', 'Template Library', 'templately' ),
169 'add_new_item' => esc_html_x( 'Add New Template', 'Template Library', 'templately' ),
170 'edit_item' => esc_html_x( 'Edit Template', 'Template Library', 'templately' ),
171 'new_item' => esc_html_x( 'New Template', 'Template Library', 'templately' ),
172 'all_items' => esc_html_x( 'All Templates', 'Template Library', 'templately' ),
173 'view_item' => esc_html_x( 'View Template', 'Template Library', 'templately' ),
174 'search_items' => esc_html_x( 'Search Template', 'Template Library', 'templately' ),
175 'not_found' => esc_html_x( 'No Templates found', 'Template Library', 'templately' ),
176 'not_found_in_trash' => esc_html_x( 'No Templates found in Trash', 'Template Library', 'templately' ),
177 'parent_item_colon' => '',
178 'menu_name' => esc_html_x( 'Templates', 'Template Library', 'templately' ),
179 ];
180
181 $args = [
182 'labels' => $labels,
183 'public' => true,
184 'rewrite' => false,
185 'menu_icon' => 'dashicons-admin-page',
186 'show_ui' => true,
187 'show_in_menu' => false,
188 'show_in_nav_menus' => false,
189 'exclude_from_search' => true,
190 'capability_type' => 'post',
191 'hierarchical' => false,
192 'show_in_rest' => true,
193 'supports' => [ 'title', 'thumbnail', 'author', 'editor', 'elementor' ],
194 ];
195
196 /**
197 * Register template library post type args.
198 *
199 * @param array $args Arguments for registering a post type.
200 */
201
202 $this->post_type_object = register_post_type( self::CPT, $args );
203 }
204
205 /**
206 * Tab menu
207 *
208 * @param $tabs
209 *
210 * @return void
211 */
212 public function admin_print_tabs( $tabs ) {
213 $types = templately()->theme_builder::$templates_manager->get_template_types();
214
215 $template_types = [];
216 $status_args = [ 'post_type' => 'templately_library' ];
217
218 $template_types['all'] = [
219 'url' => add_query_arg( $status_args, 'edit.php' ),
220 'label' => __( 'All', 'templately' )
221 ];
222
223 foreach ( $types as $type_name => $type ) {
224 if( $type::get_property( 'builder' ) === false ) {
225 continue;
226 }
227
228 $status_args['type'] = $type_name;
229 $template_types[ $type_name ] = [
230 'url' => add_query_arg( $status_args, 'edit.php' ),
231 'label' => call_user_func( [ $type, 'get_title' ] )
232 ];
233 }
234
235 $this->builder::$views->get( 'builder/tabs', [ 'tabs' => $tabs, 'template_types' => $template_types ] );
236 }
237
238 /**
239 * Retrieves items.
240 *
241 * @param array $args
242 *
243 * @return array
244 */
245 public function get_items( array $args = [] ): array {
246 $template_types = [];
247
248 if ( ! empty( $args['type'] ) ) {
249 $template_types = $args['type'];
250 unset( $args['type'] );
251 }
252
253 $defaults_args = [
254 'post_type' => self::CPT,
255 'post_status' => 'publish',
256 'posts_per_page' => - 1,
257 'orderby' => 'title',
258 'order' => 'ASC',
259 ];
260
261 if ( ! empty( $template_types ) ) {
262 $defaults_args['meta_query'] = [
263 [
264 'key' => self::TYPE_META_KEY,
265 'value' => $template_types,
266 ]
267 ];
268 }
269
270 $args = wp_parse_args( $args, $defaults_args );
271 $items = new WP_Query( $args );
272
273 return $items->posts;
274 }
275 }