| 1 |
<?php |
| 2 |
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- view template receives variables via extract(); prefixing is impractical. |
| 3 |
/** |
| 4 |
* Template part for displaying Article Summary |
| 5 |
* |
| 6 |
* @author WPDeveloper |
| 7 |
* @package BetterDocs/Templates/Parts |
| 8 |
*/ |
| 9 |
|
| 10 |
// If this file is called directly, abort. |
| 11 |
if ( ! defined( 'WPINC' ) ) { |
| 12 |
die; |
| 13 |
} |
| 14 |
|
| 15 |
// Check if Article Summary feature is enabled (skip check in editor mode) |
| 16 |
if ( ! isset( $is_editor_mode ) || ! $is_editor_mode ) { |
| 17 |
$article_summary = betterdocs()->article_summary; |
| 18 |
if ( ! $article_summary || ! $article_summary->is_enabled() ) { |
| 19 |
return; |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
// Set default parameters |
| 24 |
$post_id = isset( $post_id ) ? $post_id : get_the_ID(); |
| 25 |
$custom_title = isset( $custom_title ) ? $custom_title : ''; |
| 26 |
$show_title = isset( $show_title ) ? $show_title : true; |
| 27 |
$widget_type = isset( $widget_type ) ? $widget_type : 'template'; |
| 28 |
$blockId = isset( $blockId ) ? $blockId : ''; |
| 29 |
$is_editor_mode = isset( $is_editor_mode ) ? $is_editor_mode : false; |
| 30 |
|
| 31 |
// Check if the document has content (skip check in editor mode) |
| 32 |
if ( ! $is_editor_mode ) { |
| 33 |
$post_content = get_post_field( 'post_content', $post_id ); |
| 34 |
|
| 35 |
// Return early if no content exists |
| 36 |
if ( empty( $post_content ) || trim( wp_strip_all_tags( $post_content ) ) === '' ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
// Check if post is password protected and user hasn't provided correct password |
| 42 |
// This applies to all contexts: templates, Gutenberg blocks, and Elementor widgets |
| 43 |
if ( ! $is_editor_mode && post_password_required( $post_id ) ) { |
| 44 |
// Don't show article summary for password-protected posts until password is provided |
| 45 |
// The post_password_required() function checks both if post has password and if correct password cookie exists |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
// Generate wrapper classes - always start with base class |
| 50 |
$wrapper_classes = [ 'betterdocs-article-summary' ]; |
| 51 |
|
| 52 |
// Add widget-specific classes |
| 53 |
if ( $widget_type === 'blocks' && ! empty( $blockId ) ) { |
| 54 |
$wrapper_classes[] = $blockId; |
| 55 |
} |
| 56 |
if ( $widget_type === 'elementor' ) { |
| 57 |
$wrapper_classes[] = 'betterdocs-elementor'; |
| 58 |
} |
| 59 |
|
| 60 |
// Add editor mode class if in editor |
| 61 |
if ( $is_editor_mode ) { |
| 62 |
$wrapper_classes[] = 'betterdocs-editor-mode'; |
| 63 |
} |
| 64 |
|
| 65 |
// Ensure we always have the base article summary class |
| 66 |
if ( ! in_array( 'betterdocs-article-summary', $wrapper_classes ) ) { |
| 67 |
array_unshift( $wrapper_classes, 'betterdocs-article-summary' ); |
| 68 |
} |
| 69 |
|
| 70 |
// Create separate collapsible arrow SVG icons |
| 71 |
$arrow_right = '<svg class="angle-icon angle-right" width="12" height="12" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 72 |
<path d="M6 9L12 15L18 9" stroke="#98A2B3" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/> |
| 73 |
</svg>'; |
| 74 |
|
| 75 |
$arrow_down = '<svg class="angle-icon angle-down" width="12" height="12" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 76 |
<path d="M18 15L12 9L6 15" stroke="#98A2B3" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/> |
| 77 |
</svg>'; |
| 78 |
|
| 79 |
// Determine title text |
| 80 |
$title_text = ! empty( $custom_title ) ? $custom_title : __( 'Doc Summary', 'betterdocs' ); |
| 81 |
|
| 82 |
// Generate wrapper attributes - always use our classes for consistency |
| 83 |
$wrapper_attr = 'class="' . esc_attr( implode( ' ', $wrapper_classes ) ) . '"'; |
| 84 |
|
| 85 |
// If custom wrapper attributes are provided (from blocks/elementor), merge them |
| 86 |
if ( isset( $custom_wrapper_attr ) && ! empty( $custom_wrapper_attr ) && is_string( $custom_wrapper_attr ) ) { |
| 87 |
// Extract any additional attributes but keep our classes |
| 88 |
if ( strpos( $custom_wrapper_attr, 'style=' ) !== false ) { |
| 89 |
preg_match( '/style="([^"]*)"/', $custom_wrapper_attr, $style_matches ); |
| 90 |
if ( ! empty( $style_matches[1] ) ) { |
| 91 |
$wrapper_attr .= ' style="' . esc_attr( $style_matches[1] ) . '"'; |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
// Ensure style variables are strings |
| 97 |
$title_style = isset( $title_style ) && is_string( $title_style ) ? $title_style : ''; |
| 98 |
$content_style = isset( $content_style ) && is_string( $content_style ) ? $content_style : ''; |
| 99 |
$icon_style = isset( $icon_style ) && is_string( $icon_style ) ? $icon_style : ''; |
| 100 |
$loading_style = isset( $loading_style ) && is_string( $loading_style ) ? $loading_style : ''; |
| 101 |
?> |
| 102 |
|
| 103 |
<div <?php echo $wrapper_attr; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> id="betterdocs-article-summary" data-post-id="<?php echo esc_attr( $post_id ); ?>" data-post-type="<?php echo esc_attr( get_post_type( $post_id ) ); ?>"> |
| 104 |
<?php if ( $show_title ) : ?> |
| 105 |
<div class="betterdocs-summary-header" id="betterdocs-summary-toggle"> |
| 106 |
<span class="betterdocs-summary-title" <?php echo ! empty( $title_style ) ? 'style="' . esc_attr( $title_style ) . '"' : ''; ?>> |
| 107 |
<img src="<?php echo esc_url( betterdocs()->assets->icon( 'ai-summary-icon.svg' ) ); ?>" alt="<?php echo esc_attr__( 'AI Doc Summarizer', 'betterdocs' ); ?>" /> |
| 108 |
<?php echo esc_html( $title_text ); ?> |
| 109 |
<span class="betterdocs-summary-arrow" <?php echo ! empty( $icon_style ) ? 'style="' . esc_attr( $icon_style ) . '"' : ''; ?>> |
| 110 |
<?php echo $arrow_right; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- SVG icons cannot be escaped ?> |
| 111 |
<?php echo $arrow_down; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- SVG icons cannot be escaped ?> |
| 112 |
</span> |
| 113 |
</span> |
| 114 |
</div> |
| 115 |
<?php endif; ?> |
| 116 |
|
| 117 |
<div class="betterdocs-summary-content" id="betterdocs-summary-content" style="display: <?php echo $is_editor_mode ? 'block' : 'none'; ?>;"> |
| 118 |
<div class="betterdocs-summary-loading" id="betterdocs-summary-loading" style="display: none;" <?php echo ! empty( $loading_style ) ? 'data-style="' . esc_attr( $loading_style ) . '"' : ''; ?>> |
| 119 |
<img src="<?php echo esc_url( betterdocs()->assets->icon( 'thinking-spinner.gif' ) ); ?>" alt="<?php echo esc_attr__( 'AI Doc Summarizer Thinking', 'betterdocs' ); ?>" /> |
| 120 |
<span class="betterdocs-thinking-text"> |
| 121 |
<?php echo esc_html__( 'Thinking', 'betterdocs' ); ?><span class="betterdocs-thinking-dots"></span> |
| 122 |
</span> |
| 123 |
</div> |
| 124 |
<div class="betterdocs-summary-text" id="betterdocs-summary-text" style="display: <?php echo $is_editor_mode ? 'block' : 'none'; ?>;" <?php echo ! empty( $content_style ) ? 'data-style="' . esc_attr( $content_style ) . '"' : ''; ?>> |
| 125 |
<?php if ( $is_editor_mode ) : ?> |
| 126 |
<div class="betterdocs-summary-preview"> |
| 127 |
<p><?php echo esc_html__( 'This is a preview of the AI Doc Summarizer. The actual AI-generated summary will appear here when viewed on the frontend.', 'betterdocs' ); ?></p> |
| 128 |
<p><?php echo esc_html__( 'The summary will be automatically generated based on the article content using OpenAI technology.', 'betterdocs' ); ?></p> |
| 129 |
</div> |
| 130 |
<?php endif; ?> |
| 131 |
</div> |
| 132 |
</div> |
| 133 |
</div> |
| 134 |
|
| 135 |
<?php if ( $is_editor_mode ) : ?> |
| 136 |
<style> |
| 137 |
/* Always show Article Summary as expanded in editor mode */ |
| 138 |
.betterdocs-article-summary.betterdocs-editor-mode .betterdocs-summary-content { |
| 139 |
display: block !important; |
| 140 |
} |
| 141 |
.betterdocs-article-summary.betterdocs-editor-mode .angle-icon.angle-right { |
| 142 |
display: none !important; |
| 143 |
} |
| 144 |
.betterdocs-article-summary.betterdocs-editor-mode .angle-icon.angle-down { |
| 145 |
display: block !important; |
| 146 |
} |
| 147 |
</style> |
| 148 |
<?php endif; ?> |
| 149 |
|
| 150 |
<style> |
| 151 |
/* Animated thinking dots */ |
| 152 |
.betterdocs-thinking-dots { |
| 153 |
display: inline-block; |
| 154 |
min-width: 20px; |
| 155 |
text-align: left; |
| 156 |
} |
| 157 |
|
| 158 |
.betterdocs-thinking-dots::after { |
| 159 |
content: ''; |
| 160 |
animation: betterdocs-thinking-dots 1.5s infinite; |
| 161 |
} |
| 162 |
|
| 163 |
@keyframes betterdocs-thinking-dots { |
| 164 |
0% { content: ''; } |
| 165 |
16.66% { content: '.'; } |
| 166 |
33.33% { content: '..'; } |
| 167 |
50% { content: '...'; } |
| 168 |
66.66% { content: '....'; } |
| 169 |
83.33% { content: '.....'; } |
| 170 |
100% { content: ''; } |
| 171 |
} |
| 172 |
|
| 173 |
/* Ensure smooth animation */ |
| 174 |
.betterdocs-summary-loading { |
| 175 |
display: flex; |
| 176 |
align-items: center; |
| 177 |
gap: 1px; |
| 178 |
} |
| 179 |
|
| 180 |
.betterdocs-thinking-text { |
| 181 |
display: flex; |
| 182 |
align-items: baseline; |
| 183 |
} |
| 184 |
</style> |
| 185 |
|