| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly. |
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
if ( ! class_exists( 'Qi_Blocks_Post_Date_Block' ) ) { |
| 7 |
class Qi_Blocks_Post_Date_Block extends Qi_Blocks_Blocks { |
| 8 |
private static $instance; |
| 9 |
|
| 10 |
public function __construct() { |
| 11 |
// Set block data |
| 12 |
$this->set_block_name( 'post-date' ); |
| 13 |
$this->set_block_title( esc_html__( 'Post Date', 'qi-blocks' ) ); |
| 14 |
$this->set_block_subcategory( esc_html__( 'Content', 'qi-blocks' ) ); |
| 15 |
$this->set_block_demo_url( 'https://qodeinteractive.com/qi-blocks-for-gutenberg/post-dates/#post-dates' ); |
| 16 |
$this->set_block_documentation( 'https://qodeinteractive.com/qi-blocks-for-gutenberg/documentation/#post_date' ); |
| 17 |
|
| 18 |
$block_options = array( |
| 19 |
'render_callback' => array( $this, 'dynamic_render_callback' ), |
| 20 |
'uses_context' => array( |
| 21 |
'postType', |
| 22 |
'postId', |
| 23 |
), |
| 24 |
'attributes' => array_merge( |
| 25 |
array( |
| 26 |
'uniqueClass' => array( |
| 27 |
'type' => 'string', |
| 28 |
'default' => '', |
| 29 |
), |
| 30 |
'blockContainerIds' => array( |
| 31 |
'type' => 'string', |
| 32 |
'default' => '', |
| 33 |
), |
| 34 |
'blockContainerData' => array( |
| 35 |
'type' => 'string', |
| 36 |
'default' => '', |
| 37 |
), |
| 38 |
'blockContainerClasses' => array( |
| 39 |
'type' => 'string', |
| 40 |
'default' => '', |
| 41 |
), |
| 42 |
), |
| 43 |
qi_blocks_get_block_container_attributes(), |
| 44 |
array( |
| 45 |
'dateFormat' => array( |
| 46 |
'type' => 'string', |
| 47 |
'default' => '', |
| 48 |
), |
| 49 |
'isLink' => array( |
| 50 |
'type' => 'boolean', |
| 51 |
'default' => false, |
| 52 |
), |
| 53 |
'dateColor' => array( |
| 54 |
'type' => 'string', |
| 55 |
'default' => '', |
| 56 |
), |
| 57 |
'dateHoverColor' => array( |
| 58 |
'type' => 'string', |
| 59 |
'default' => '', |
| 60 |
), |
| 61 |
), |
| 62 |
qi_blocks_get_block_option_typography_attributes( 'date' ) |
| 63 |
), |
| 64 |
); |
| 65 |
|
| 66 |
$this->set_block_options( $block_options ); |
| 67 |
|
| 68 |
parent::__construct(); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @return Qi_Blocks_Post_Date_Block |
| 73 |
*/ |
| 74 |
public static function get_instance() { |
| 75 |
if ( is_null( self::$instance ) ) { |
| 76 |
self::$instance = new self(); |
| 77 |
} |
| 78 |
|
| 79 |
return self::$instance; |
| 80 |
} |
| 81 |
|
| 82 |
function dynamic_render_callback( $attributes, $content, $block ) { |
| 83 |
$html = ''; |
| 84 |
|
| 85 |
if ( ! empty( $block ) && empty( $block->context['postId'] ) ) { |
| 86 |
return ''; |
| 87 |
} |
| 88 |
|
| 89 |
$post_ID = $block->context['postId']; |
| 90 |
$formatted_date = get_the_date( empty( $attributes['dateFormat'] ) ? '' : $attributes['dateFormat'], $post_ID ); |
| 91 |
|
| 92 |
if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) { |
| 93 |
$formatted_date = sprintf( '<a class="qodef-m-link" href="%1s">%2s</a>', get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ), $formatted_date ); |
| 94 |
} |
| 95 |
|
| 96 |
if ( ! empty( $formatted_date ) ) { |
| 97 |
$block_classes = qi_blocks_get_block_holder_classes( 'post-date', $attributes ); |
| 98 |
|
| 99 |
$html .= '<div ' . qi_blocks_get_block_container_html_attributes_string( $attributes ) . '>'; |
| 100 |
$html .= '<div class="' . implode( ' ', $block_classes ) . '">'; |
| 101 |
|
| 102 |
$html .= sprintf( |
| 103 |
'<time class="qodef-m-date" datetime="%1$s">%2$s</time>', |
| 104 |
esc_attr( get_the_date( 'c', $post_ID ) ), |
| 105 |
$formatted_date |
| 106 |
); |
| 107 |
|
| 108 |
$html .= '</div>'; |
| 109 |
$html .= '</div>'; |
| 110 |
} |
| 111 |
|
| 112 |
return $html; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
Qi_Blocks_Post_Date_Block::get_instance(); |
| 117 |
} |
| 118 |
|