| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\Editors\BlockEditor\Blocks; |
| 4 |
|
| 5 |
use WPDeveloper\BetterDocs\Editors\BlockEditor\Block; |
| 6 |
|
| 7 |
class ArticleSummary extends Block { |
| 8 |
|
| 9 |
protected $frontend_styles = [ |
| 10 |
'betterdocs-article-summary' |
| 11 |
]; |
| 12 |
|
| 13 |
/** |
| 14 |
* Get block name |
| 15 |
* |
| 16 |
* @return string |
| 17 |
*/ |
| 18 |
public function get_name() { |
| 19 |
return 'article-summary'; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Get default attributes |
| 24 |
* |
| 25 |
* @return array |
| 26 |
*/ |
| 27 |
public function get_default_attributes() { |
| 28 |
return [ |
| 29 |
'blockId' => '', |
| 30 |
'customTitle' => '', |
| 31 |
'showTitle' => true, |
| 32 |
'titleColor' => '#2c3e50', |
| 33 |
'titleFontSize' => 16, |
| 34 |
'titleFontWeight' => '600', |
| 35 |
'backgroundColor' => '#ffffff', |
| 36 |
'borderColor' => '#e1e5e9', |
| 37 |
'borderWidth' => 1, |
| 38 |
'borderRadius' => 8, |
| 39 |
'padding' => null, |
| 40 |
'margin' => null, |
| 41 |
'contentColor' => '#555555', |
| 42 |
'contentFontSize' => 14, |
| 43 |
'contentLineHeight' => 1.6, |
| 44 |
'loadingColor' => '#666666', |
| 45 |
'iconColor' => 'inherit', |
| 46 |
'iconSize' => 14, |
| 47 |
]; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Render the block |
| 52 |
* |
| 53 |
* @param array $attributes Block attributes |
| 54 |
* @param string $content Block content |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
public function render( $attributes, $content ) { |
| 58 |
// For frontend, check if it's a docs post type |
| 59 |
if ( get_post_type() !== 'docs' ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
// Check if Article Summary feature is enabled (skip in editor mode) |
| 64 |
if ( ! $this->is_editor_mode() ) { |
| 65 |
$article_summary = betterdocs()->article_summary; |
| 66 |
if ( ! $article_summary || ! $article_summary->is_enabled() ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// Use the template with view_params |
| 72 |
$this->views( 'templates/parts/article-summary' ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Check if we're in editor mode |
| 77 |
* |
| 78 |
* @return bool |
| 79 |
*/ |
| 80 |
private function is_editor_mode() { |
| 81 |
// Check if we're in Gutenberg editor. |
| 82 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- REST request context check; nonce verified by REST cookie auth. |
| 83 |
$context = isset( $_REQUEST['context'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['context'] ) ) : ''; |
| 84 |
return defined( 'REST_REQUEST' ) && REST_REQUEST && 'edit' === $context; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get view parameters for the template |
| 89 |
* |
| 90 |
* @return array |
| 91 |
*/ |
| 92 |
public function view_params() { |
| 93 |
// Extract basic attributes |
| 94 |
$block_id = $this->attributes['blockId'] ?? ''; |
| 95 |
$custom_title = $this->attributes['customTitle'] ?? ''; |
| 96 |
$show_title = $this->attributes['showTitle'] ?? true; |
| 97 |
|
| 98 |
return [ |
| 99 |
'post_id' => get_the_ID(), |
| 100 |
'custom_title' => $custom_title, |
| 101 |
'show_title' => $show_title, |
| 102 |
'widget_type' => 'blocks', |
| 103 |
'blockId' => $block_id, |
| 104 |
'is_editor_mode' => $this->is_editor_mode(), |
| 105 |
]; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
} |
| 110 |
|