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