| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Modules\ThemeBuilder; |
| 4 |
|
| 5 |
use Templately\Utils\Helper; |
| 6 |
use WP_Post; |
| 7 |
use Templately\Admin\Roles; |
| 8 |
use WP_Query; |
| 9 |
|
| 10 |
use Templately\Modules\ThemeBuilder\Managers\TemplateManager; |
| 11 |
|
| 12 |
class Source { |
| 13 |
const CPT = 'templately_library'; |
| 14 |
const TYPE_META_KEY = '_templately_template_type'; |
| 15 |
const PLATFORM_META_KEY = '_templately_template_platform'; |
| 16 |
/** |
| 17 |
* Marks the template that wins a TIE for its type. |
| 18 |
* |
| 19 |
* A tie-break, never a selector: display conditions still decide which templates are |
| 20 |
* eligible and which is most specific (a shop-archive header beats a site-wide one on |
| 21 |
* shop pages, flag or no flag). This only settles the case conditions cannot — two |
| 22 |
* templates matching the same location with the SAME specificity — which used to fall |
| 23 |
* to "lowest post ID", i.e. the OLDEST import won and a freshly imported header was |
| 24 |
* never seen. One template per type carries it; the list marks it "Active". |
| 25 |
*/ |
| 26 |
const ACTIVE_META_KEY = '_templately_is_active_template'; |
| 27 |
|
| 28 |
public $post_type_object; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var ThemeBuilder |
| 32 |
*/ |
| 33 |
protected $builder; |
| 34 |
|
| 35 |
public function __construct( $builder ) { |
| 36 |
$this->builder = $builder; |
| 37 |
|
| 38 |
$this->add_actions(); |
| 39 |
$this->register_post_type(); |
| 40 |
} |
| 41 |
|
| 42 |
private function add_actions() { |
| 43 |
add_filter( 'views_edit-' . self::CPT, [ $this, 'admin_print_tabs' ], 100 ); |
| 44 |
add_filter( 'post_row_actions', [ $this, 'row_actions' ], 10, 2 ); |
| 45 |
add_action( 'admin_post_templately_set_active_template', [ $this, 'handle_set_active' ] ); |
| 46 |
add_action( 'in_admin_header', [ $this, 'in_admin_header' ] ); |
| 47 |
if ( is_admin() ) { |
| 48 |
// add_action( 'manage_posts_extra_tablenav', [ $this, 'extra_table_nav' ] ); |
| 49 |
add_action( 'manage_' . self::CPT . '_posts_custom_column', [ $this, 'custom_column' ], 10, 2 ); |
| 50 |
add_filter( 'manage_' . self::CPT . '_posts_columns', [ $this, 'custom_columns' ] ); |
| 51 |
add_filter( 'post_row_actions', [ $this, 'post_row_actions' ], 10, 2 ); |
| 52 |
add_action( 'admin_footer', [ $this, 'app' ] ); |
| 53 |
} |
| 54 |
|
| 55 |
// add_action( 'save_post', [ $this, 'save_post' ], 11, 3 ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* If needed to add a filter for platform |
| 60 |
* |
| 61 |
* @param $which |
| 62 |
* |
| 63 |
* @return void |
| 64 |
* @suppress 50 |
| 65 |
*/ |
| 66 |
public function extra_table_nav( $which ) { |
| 67 |
if( ! $this->is_edit_screen() ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
// if( $which === 'top' ) { |
| 72 |
// |
| 73 |
// } |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Offer "Make active" on every template that is not already the active one. |
| 78 |
* |
| 79 |
* A read-only badge would leave a wrong choice fixable only by re-importing, so the |
| 80 |
* flag the import sets is the same one the user can set. |
| 81 |
* |
| 82 |
* @param array $actions |
| 83 |
* @param \WP_Post $post |
| 84 |
* @return array |
| 85 |
*/ |
| 86 |
public function row_actions( $actions, $post ) { |
| 87 |
if ( ! $post || self::CPT !== $post->post_type || ! current_user_can( 'edit_post', $post->ID ) ) { |
| 88 |
return $actions; |
| 89 |
} |
| 90 |
|
| 91 |
$type = (string) get_post_meta( $post->ID, self::TYPE_META_KEY, true ); |
| 92 |
|
| 93 |
// Already the one being served — nothing to make active. |
| 94 |
if ( '' === $type || TemplateManager::get_active_id( $type ) === (int) $post->ID ) { |
| 95 |
return $actions; |
| 96 |
} |
| 97 |
|
| 98 |
$url = wp_nonce_url( |
| 99 |
admin_url( 'admin-post.php?action=templately_set_active_template&post=' . $post->ID ), |
| 100 |
'templately_set_active_' . $post->ID |
| 101 |
); |
| 102 |
|
| 103 |
$actions['templately_set_active'] = sprintf( |
| 104 |
'<a href="%s">%s</a>', |
| 105 |
esc_url( $url ), |
| 106 |
esc_html__( 'Make active', 'templately' ) |
| 107 |
); |
| 108 |
|
| 109 |
return $actions; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Apply "Make active" and return to the list. |
| 114 |
* |
| 115 |
* Capability AND nonce: this changes which template every visitor is served, so it is |
| 116 |
* not a link anything may follow on the user's behalf. |
| 117 |
* |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
public function handle_set_active() { |
| 121 |
$post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0; |
| 122 |
|
| 123 |
if ( ! $post_id || self::CPT !== get_post_type( $post_id ) ) { |
| 124 |
wp_die( esc_html__( 'Invalid template.', 'templately' ) ); |
| 125 |
} |
| 126 |
|
| 127 |
check_admin_referer( 'templately_set_active_' . $post_id ); |
| 128 |
|
| 129 |
if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 130 |
wp_die( esc_html__( 'Sorry, you are not allowed to do that.', 'templately' ) ); |
| 131 |
} |
| 132 |
|
| 133 |
$this->builder::$templates_manager->set_active( $post_id ); |
| 134 |
|
| 135 |
wp_safe_redirect( wp_get_referer() ?: admin_url( 'edit.php?post_type=' . self::CPT ) ); |
| 136 |
exit; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Print custom column data (Template Type and Platform) |
| 141 |
* |
| 142 |
* @param $column_name |
| 143 |
* @param $post_id |
| 144 |
* |
| 145 |
* @return void |
| 146 |
*/ |
| 147 |
public function custom_column( $column_name, $post_id ) { |
| 148 |
switch ( $column_name ) { |
| 149 |
case 'template_type': |
| 150 |
$types = $this->builder::$templates_manager->get_template_types(); |
| 151 |
$type = get_post_meta( $post_id, self::TYPE_META_KEY, true ); |
| 152 |
if(isset($types[ $type ])){ |
| 153 |
echo esc_html( call_user_func( [ $types[ $type ], 'get_title' ] ) ); |
| 154 |
} |
| 155 |
// With several templates of one type there was nothing on screen saying which |
| 156 |
// one a visitor actually gets. Badge the EFFECTIVE winner, not merely the |
| 157 |
// flagged one: nothing is flagged on any site that predates the flag, so a |
| 158 |
// badge keyed on it would appear nowhere while two headers sit here and one |
| 159 |
// of them quietly wins. One badge, whether the template was chosen or is |
| 160 |
// simply the newest — the reader wants to know which one is live, and a |
| 161 |
// second variant for "how it got there" asks them to learn a distinction |
| 162 |
// that changes nothing they would do about it. |
| 163 |
if ( '' !== $type && TemplateManager::get_active_id( $type ) === (int) $post_id ) { |
| 164 |
printf( |
| 165 |
' <span class="templately-active-badge" style="display:inline-block;margin-left:6px;padding:1px 8px;border-radius:9px;background:#e7f7ee;color:#0a6b3d;font-size:11px;font-weight:600;line-height:1.8;vertical-align:middle;" title="%s">%s</span>', |
| 166 |
esc_attr__( 'The template used for this type. Display conditions can still override it on specific pages.', 'templately' ), |
| 167 |
esc_html__( 'Active', 'templately' ) |
| 168 |
); |
| 169 |
} |
| 170 |
break; |
| 171 |
case 'template_platform': |
| 172 |
echo esc_html( get_post_meta( $post_id, self::PLATFORM_META_KEY, true ) ); |
| 173 |
break; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Add custom columns. (Template Type and Platform) |
| 179 |
* |
| 180 |
* @param $columns |
| 181 |
* |
| 182 |
* @return mixed |
| 183 |
*/ |
| 184 |
public function custom_columns( $columns ) { |
| 185 |
$old_columns = $columns; |
| 186 |
|
| 187 |
unset( $columns['date'] ); |
| 188 |
unset( $columns['author'] ); |
| 189 |
|
| 190 |
$columns['template_type'] = __( 'Type', 'templately' ); |
| 191 |
$columns['template_platform'] = __( 'Platform', 'templately' ); |
| 192 |
|
| 193 |
$columns['author'] = $old_columns['author']; |
| 194 |
$columns['date'] = $old_columns['date']; |
| 195 |
|
| 196 |
return $columns; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Add New Template Button App |
| 201 |
* @return void |
| 202 |
*/ |
| 203 |
public function in_admin_header() { |
| 204 |
if ( ! $this->is_edit_screen() ) { |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
echo '<div id="templately-theme-builder-admin-header"></div>'; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Edit conditions button app |
| 213 |
* @return void |
| 214 |
*/ |
| 215 |
public function app() { |
| 216 |
if( ! $this->is_edit_screen() ) { |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
Helper::views( 'builder/edit-conditions' ); |
| 221 |
} |
| 222 |
|
| 223 |
private function is_edit_screen(): bool { |
| 224 |
global $current_screen; |
| 225 |
|
| 226 |
if ( ! $current_screen ) { |
| 227 |
return false; |
| 228 |
} |
| 229 |
|
| 230 |
return 'edit' === $current_screen->base && self::CPT === $current_screen->post_type; |
| 231 |
} |
| 232 |
|
| 233 |
public function post_row_actions( $actions, $post ) { |
| 234 |
if ( $this->is_edit_screen() ) { |
| 235 |
$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' ) ); |
| 236 |
} |
| 237 |
|
| 238 |
return $actions; |
| 239 |
} |
| 240 |
|
| 241 |
public function save_post( int $post_id, WP_Post $post, bool $update ) { |
| 242 |
if ( $post->post_type !== self::CPT ) { |
| 243 |
return; |
| 244 |
} |
| 245 |
|
| 246 |
if ( wp_is_post_autosave( $post_id ) ) { |
| 247 |
return; |
| 248 |
} |
| 249 |
|
| 250 |
// FIXME: this is something I need to fix in the future. |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Register post type |
| 255 |
* |
| 256 |
* @return void |
| 257 |
*/ |
| 258 |
public function register_post_type() { |
| 259 |
$name = esc_html_x( 'Templates', 'Template Library', 'templately' ); |
| 260 |
|
| 261 |
$labels = [ |
| 262 |
'name' => $name, |
| 263 |
'singular_name' => esc_html_x( 'Template', 'Template Library', 'templately' ), |
| 264 |
'add_new' => esc_html_x( 'Add New Template', 'Template Library', 'templately' ), |
| 265 |
'add_new_item' => esc_html_x( 'Add New Template', 'Template Library', 'templately' ), |
| 266 |
'edit_item' => esc_html_x( 'Edit Template', 'Template Library', 'templately' ), |
| 267 |
'new_item' => esc_html_x( 'New Template', 'Template Library', 'templately' ), |
| 268 |
'all_items' => esc_html_x( 'All Templates', 'Template Library', 'templately' ), |
| 269 |
'view_item' => esc_html_x( 'View Template', 'Template Library', 'templately' ), |
| 270 |
'search_items' => esc_html_x( 'Search Template', 'Template Library', 'templately' ), |
| 271 |
'not_found' => esc_html_x( 'No Templates found', 'Template Library', 'templately' ), |
| 272 |
'not_found_in_trash' => esc_html_x( 'No Templates found in Trash', 'Template Library', 'templately' ), |
| 273 |
'parent_item_colon' => '', |
| 274 |
'menu_name' => esc_html_x( 'Templates', 'Template Library', 'templately' ), |
| 275 |
]; |
| 276 |
|
| 277 |
$args = [ |
| 278 |
'labels' => $labels, |
| 279 |
'public' => true, |
| 280 |
'rewrite' => false, |
| 281 |
'menu_icon' => 'dashicons-admin-page', |
| 282 |
'show_ui' => true, |
| 283 |
'show_in_menu' => false, |
| 284 |
'show_in_nav_menus' => false, |
| 285 |
'exclude_from_search' => true, |
| 286 |
// Site-structure control, not post authoring: every write/list primitive |
| 287 |
// resolves to `edit_templately_builder` (administrator-only by default). |
| 288 |
// See Templately\Admin\Roles. |
| 289 |
'capability_type' => 'post', |
| 290 |
'capabilities' => Roles::builder_post_type_capabilities(), |
| 291 |
'map_meta_cap' => true, |
| 292 |
'hierarchical' => false, |
| 293 |
'show_in_rest' => true, |
| 294 |
'supports' => [ 'title', 'thumbnail', 'author', 'editor', 'elementor' ], |
| 295 |
]; |
| 296 |
|
| 297 |
/** |
| 298 |
* Register template library post type args. |
| 299 |
* |
| 300 |
* @param array $args Arguments for registering a post type. |
| 301 |
*/ |
| 302 |
|
| 303 |
$this->post_type_object = register_post_type( self::CPT, $args ); |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Tab menu |
| 308 |
* |
| 309 |
* @param $tabs |
| 310 |
* |
| 311 |
* @return void |
| 312 |
*/ |
| 313 |
public function admin_print_tabs( $tabs ) { |
| 314 |
$types = templately()->theme_builder::$templates_manager->get_template_types(); |
| 315 |
|
| 316 |
$template_types = []; |
| 317 |
$status_args = [ 'post_type' => 'templately_library' ]; |
| 318 |
|
| 319 |
$template_types['all'] = [ |
| 320 |
'url' => add_query_arg( $status_args, 'edit.php' ), |
| 321 |
'label' => __( 'All', 'templately' ) |
| 322 |
]; |
| 323 |
|
| 324 |
foreach ( $types as $type_name => $type ) { |
| 325 |
if( $type::get_property( 'builder' ) === false ) { |
| 326 |
continue; |
| 327 |
} |
| 328 |
|
| 329 |
$status_args['type'] = $type_name; |
| 330 |
$template_types[ $type_name ] = [ |
| 331 |
'url' => add_query_arg( $status_args, 'edit.php' ), |
| 332 |
'label' => call_user_func( [ $type, 'get_title' ] ) |
| 333 |
]; |
| 334 |
} |
| 335 |
|
| 336 |
$this->builder::$views->get( 'builder/tabs', [ 'tabs' => $tabs, 'template_types' => $template_types ] ); |
| 337 |
return []; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Retrieves items. |
| 342 |
* |
| 343 |
* @param array $args |
| 344 |
* |
| 345 |
* @return array |
| 346 |
*/ |
| 347 |
public function get_items( array $args = [] ): array { |
| 348 |
$template_types = []; |
| 349 |
|
| 350 |
if ( ! empty( $args['type'] ) ) { |
| 351 |
$template_types = $args['type']; |
| 352 |
unset( $args['type'] ); |
| 353 |
} |
| 354 |
|
| 355 |
$defaults_args = [ |
| 356 |
'post_type' => self::CPT, |
| 357 |
'post_status' => 'publish', |
| 358 |
'posts_per_page' => - 1, |
| 359 |
'orderby' => 'title', |
| 360 |
'order' => 'ASC', |
| 361 |
]; |
| 362 |
|
| 363 |
if ( ! empty( $template_types ) ) { |
| 364 |
$defaults_args['meta_query'] = [ |
| 365 |
[ |
| 366 |
'key' => self::TYPE_META_KEY, |
| 367 |
'value' => $template_types, |
| 368 |
] |
| 369 |
]; |
| 370 |
} |
| 371 |
|
| 372 |
$args = wp_parse_args( $args, $defaults_args ); |
| 373 |
$items = new WP_Query( $args ); |
| 374 |
|
| 375 |
return $items->posts; |
| 376 |
} |
| 377 |
} |