| 1 |
<?php |
| 2 |
namespace ULTP\blocks; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
class Archive_Title { |
| 7 |
public function __construct() { |
| 8 |
add_action( 'init', array( $this, 'register' ) ); |
| 9 |
} |
| 10 |
public function get_attributes() { |
| 11 |
|
| 12 |
return array( |
| 13 |
'blockId' => '', |
| 14 |
|
| 15 |
/* |
| 16 |
============================ |
| 17 |
General Settings |
| 18 |
============================*/ |
| 19 |
'layout' => '1', |
| 20 |
// 'contentAlign' => "left", |
| 21 |
'titleShow' => true, |
| 22 |
'excerptShow' => true, |
| 23 |
'prefixShow' => false, |
| 24 |
'showImage' => false, |
| 25 |
|
| 26 |
/* |
| 27 |
============================ |
| 28 |
Title Setting/Style |
| 29 |
============================*/ |
| 30 |
'titleTag' => 'h1', |
| 31 |
'customTaxTitleColor' => false, |
| 32 |
'seperatorTaxTitleLink' => admin_url( 'edit-tags.php?taxonomy=category' ), |
| 33 |
|
| 34 |
/* |
| 35 |
============================ |
| 36 |
Prefix Setting/Style |
| 37 |
============================*/ |
| 38 |
'prefixText' => 'Sample Prefix Text', |
| 39 |
'prefixTop' => false, |
| 40 |
|
| 41 |
/* |
| 42 |
============================ |
| 43 |
Custom Wrapper Style |
| 44 |
============================*/ |
| 45 |
'customTaxColor' => false, |
| 46 |
'seperatorTaxLink' => admin_url( 'edit-tags.php?taxonomy=category' ), |
| 47 |
|
| 48 |
/* |
| 49 |
============================ |
| 50 |
Advance Settings |
| 51 |
============================*/ |
| 52 |
'advanceId' => '', |
| 53 |
'advanceZindex' => '', |
| 54 |
|
| 55 |
// --------------------- |
| 56 |
// Advanced > Responsive |
| 57 |
// --------------------- |
| 58 |
'hideExtraLarge' => false, |
| 59 |
'hideTablet' => false, |
| 60 |
'hideMobile' => false, |
| 61 |
'advanceCss' => '', |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
public function register() { |
| 66 |
register_block_type( |
| 67 |
'ultimate-post/archive-title', |
| 68 |
array( |
| 69 |
'editor_script' => 'ultp-blocks-editor-script', |
| 70 |
'editor_style' => 'ultp-blocks-editor-css', |
| 71 |
'render_callback' => array( $this, 'content' ), |
| 72 |
) |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
public function get_data() { |
| 77 |
if ( is_admin() ) { |
| 78 |
// For Demonstration Purpose |
| 79 |
return array( |
| 80 |
'title' => 'Archive Title', |
| 81 |
'image' => ULTP_URL . 'assets/img/builder-fallback.jpg', |
| 82 |
'desc' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam molestie aliquet molestie.', |
| 83 |
'color' => '#037fff', |
| 84 |
); |
| 85 |
} else { |
| 86 |
$_title = $_image = $_desc = $_color = ''; |
| 87 |
if ( is_archive() ) { |
| 88 |
if ( is_category() || is_tag() || is_tax() ) { |
| 89 |
$obj = get_queried_object(); |
| 90 |
$attachment_id = get_term_meta( $obj->term_id, 'ultp_category_image', true ); |
| 91 |
$_title = $obj->name; |
| 92 |
$_image = $attachment_id ? wp_get_attachment_url( $attachment_id ) : ''; |
| 93 |
$_desc = $obj->description; |
| 94 |
$_color = get_term_meta( $obj->term_id, 'ultp_category_color', true ); |
| 95 |
} elseif ( is_date() ) { |
| 96 |
$_title = is_year() ? get_the_date( 'Y' ) : ( is_month() ? get_the_date( 'F Y' ) : ( is_day() ? get_the_date( 'F j, Y' ) : '' ) ); |
| 97 |
} elseif ( is_author() ) { |
| 98 |
$_title = get_the_author_meta( 'display_name' ); |
| 99 |
$_image = get_avatar_url( get_the_author_meta( 'ID' ) ); |
| 100 |
$_desc = get_the_author_meta( 'description' ); |
| 101 |
} |
| 102 |
} elseif ( is_search() ) { |
| 103 |
$_title = get_search_query(); |
| 104 |
} |
| 105 |
return array( |
| 106 |
'title' => $_title, |
| 107 |
'image' => $_image, |
| 108 |
'desc' => $_desc, |
| 109 |
'color' => $_color, |
| 110 |
); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
public function content( $attr, $noAjax ) { |
| 116 |
$attr = wp_parse_args( $attr, $this->get_attributes() ); |
| 117 |
|
| 118 |
// Dummy |
| 119 |
$data = $this->get_data(); |
| 120 |
$wraper_before = $wraper_after = $post_loop = ''; |
| 121 |
$block_name = 'archive-title'; |
| 122 |
|
| 123 |
$attr['className'] = isset( $attr['className'] ) && $attr['className'] ? preg_replace( '/[^A-Za-z0-9_ -]/', '', $attr['className'] ) : ''; |
| 124 |
$attr['align'] = isset( $attr['align'] ) && $attr['align'] ? preg_replace( '/[^A-Za-z0-9_ -]/', '', $attr['align'] ) : ''; |
| 125 |
$attr['advanceId'] = isset( $attr['advanceId'] ) ? sanitize_html_class( $attr['advanceId'] ) : ''; |
| 126 |
$attr['titleTag'] = in_array( $attr['titleTag'], ultimate_post()->ultp_allowed_block_tags() ) ? $attr['titleTag'] : 'h1'; |
| 127 |
$attr['layout'] = sanitize_html_class( $attr['layout'] ); |
| 128 |
$attr['prefixText'] = wp_kses( $attr['prefixText'], ultimate_post()->ultp_allowed_html_tags() ); |
| 129 |
|
| 130 |
$wraper_before .= '<div ' . ( $attr['advanceId'] ? 'id="' . $attr['advanceId'] . '" ' : '' ) . ' class="wp-block-ultimate-post-' . $block_name . ' ultp-block-' . $attr['blockId'] . '' . ( $attr['align'] ? ' align' . $attr['align'] : '' ) . '' . ( $attr['className'] ? ' ' . $attr['className'] : '' ) . '">'; |
| 131 |
$wraper_before .= '<div class="ultp-block-wrapper">'; |
| 132 |
$wraper_before .= '<div class="ultp-block-archive-title ultp-archive-layout-' . $attr['layout'] . '">'; |
| 133 |
|
| 134 |
$style = $attr['layout'] == '2' ? ( $data['image'] ? 'style="background-image: url(' . $data['image'] . ')' : 'style="background-color:' . ( $data['color'] ? $data['color'] : '#28303d' ) ) . '"' : ''; |
| 135 |
$prefix = ( $attr['prefixShow'] && $attr['prefixText'] ) ? '<span class="ultp-archive-prefix">' . $attr['prefixText'] . '</span> ' : ''; |
| 136 |
|
| 137 |
$name = ( $attr['titleShow'] && $data['title'] ) ? '<' . $attr['titleTag'] . ' class="ultp-archive-name" ' . ( ( $data['color'] && $attr['customTaxTitleColor'] ) ? 'style="color: ' . $data['color'] . '"' : '' ) . '>' . $prefix . $data['title'] . '</' . $attr['titleTag'] . '>' : ''; |
| 138 |
|
| 139 |
$excerpt = ( $attr['excerptShow'] && $data['desc'] ) ? '<div class="ultp-archive-desc">' . $data['desc'] . '</div>' : ''; |
| 140 |
|
| 141 |
// Prefix |
| 142 |
switch ( $attr['layout'] ) { |
| 143 |
case 1: |
| 144 |
$img = ( $attr['showImage'] && $data['image'] ) ? '<img class="ultp-archive-image" src="' . $data['image'] . '" alt="' . $data['title'] . '"/>' : ''; |
| 145 |
$post_loop .= $img . $name . $excerpt; |
| 146 |
break; |
| 147 |
case 2: |
| 148 |
$style_overlay = $attr['customTaxColor'] ? 'style="background-color: ' . $data['color'] . '"' : ''; |
| 149 |
$post_loop .= '<div class="ultp-archive-content" ' . $style . '><div class="ultp-archive-overlay" ' . $style_overlay . '></div>' . $name . $excerpt . '</div>'; |
| 150 |
break; |
| 151 |
} |
| 152 |
|
| 153 |
$wraper_after .= '</div>'; |
| 154 |
$wraper_after .= '</div>'; |
| 155 |
$wraper_after .= '</div>'; |
| 156 |
|
| 157 |
return $wraper_before . $post_loop . $wraper_after; |
| 158 |
} |
| 159 |
} |
| 160 |
|