'string', '_ahmaipsu_enabled' => 'string', '_ahmaipsu_content_type' => 'string', ); foreach ($keys as $key => $type) { register_post_meta( '', $key, array( 'show_in_rest' => true, 'single' => true, 'type' => $type, 'auth_callback' => $auth, ) ); } } /** * Register the dynamic summary block from the built assets. */ function ahmaipsu_register_summary_block() { $dir = AHMAIPSU_PLUGIN_DIR . 'build/summary'; if (!file_exists($dir . '/block.json')) { return; } register_block_type( $dir, array( 'render_callback' => 'ahmaipsu_render_summary_block', ) ); } /** * Map a block style class to a plugin theme key. * * @param string $class_name Block className. * @param string $fallback Settings theme. * @return string */ function ahmaipsu_block_theme_from_class($class_name, $fallback = 'classic') { if (strpos($class_name, 'is-style-minimal') !== false) { return 'minimal'; } if (strpos($class_name, 'is-style-card') !== false) { return 'card'; } return $fallback ? $fallback : 'classic'; } /** * Dynamic render. Frontend is silent when empty or per-post off. * * @param array $attributes Block attributes. * @return string */ function ahmaipsu_render_summary_block($attributes) { $post_id = get_the_ID(); if (!$post_id) { return ''; } $enabled = get_post_meta($post_id, '_ahmaipsu_enabled', true); $summary = get_post_meta($post_id, '_ahmaipsu_content', true); if (empty($enabled) || empty($summary)) { return ''; } $options = get_option('ahmaipsu_settings', array()); $content_type = get_post_meta($post_id, '_ahmaipsu_content_type', true) ?: 'summary'; $default_theme = isset($options['ahmaipsu_theme']) ? $options['ahmaipsu_theme'] : 'classic'; $class_name = isset($attributes['className']) ? $attributes['className'] : ''; $theme = ahmaipsu_block_theme_from_class($class_name, $default_theme); $disclaimer = isset($options['ahmaipsu_disclaimer']) ? $options['ahmaipsu_disclaimer'] : 'This summary was generated by AI and may contain inaccuracies or omissions. Please refer to the full article for complete information.'; $show_title = !isset($attributes['showTitle']) || $attributes['showTitle']; $show_disclaimer = !isset($attributes['showDisclaimer']) || $attributes['showDisclaimer']; if (!wp_style_is('ahmaipsu-frontend', 'enqueued')) { wp_enqueue_style( 'ahmaipsu-frontend', AHMAIPSU_PLUGIN_URL . 'dist/css/frontend.min.css', array(), AHMAIPSU_VERSION ); } return ahmaipsu_render_summary($summary, $disclaimer, $theme, $content_type, $show_title, $show_disclaimer); } /** * Editor extras: frontend CSS + AJAX config. */ function ahmaipsu_block_editor_assets() { wp_enqueue_style( 'ahmaipsu-frontend', AHMAIPSU_PLUGIN_URL . 'dist/css/frontend.min.css', array(), AHMAIPSU_VERSION ); $post_id = get_the_ID(); if (!$post_id) { global $post; $post_id = ($post && isset($post->ID)) ? $post->ID : 0; } $options = get_option('ahmaipsu_settings', array()); $handle = 'ahm-ai-post-summary-summary-editor-script'; $data = array( 'ajaxUrl' => admin_url('admin-ajax.php'), 'postId' => (int) $post_id, 'regenerateNonce' => $post_id ? wp_create_nonce('ahmaipsu_regenerate_' . $post_id) : '', 'enableNonce' => $post_id ? wp_create_nonce('ahmaipsu_enable_' . $post_id) : '', 'theme' => isset($options['ahmaipsu_theme']) ? $options['ahmaipsu_theme'] : 'classic', 'disclaimer' => isset($options['ahmaipsu_disclaimer']) ? $options['ahmaipsu_disclaimer'] : '', 'customSummaryTitle' => isset($options['ahmaipsu_custom_summary_title']) ? $options['ahmaipsu_custom_summary_title'] : '', 'customTakeawaysTitle' => isset($options['ahmaipsu_custom_key_takeaways_title']) ? $options['ahmaipsu_custom_key_takeaways_title'] : '', ); wp_add_inline_script( $handle, 'window.ahmaipsuBlock = ' . wp_json_encode($data) . ';', 'before' ); // Fallback if the generated handle differs. wp_register_script('ahmaipsu-block-config', false, array(), AHMAIPSU_VERSION, true); wp_enqueue_script('ahmaipsu-block-config'); wp_add_inline_script( 'ahmaipsu-block-config', 'window.ahmaipsuBlock = ' . wp_json_encode($data) . ';', 'before' ); } /** * Turn on per-post summaries without generating. */ function ahmaipsu_ajax_enable_post() { if (!isset($_POST['post_id']) || !isset($_POST['nonce'])) { wp_send_json_error('Missing required parameters'); } $post_id = intval($_POST['post_id']); if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'ahmaipsu_enable_' . $post_id)) { wp_send_json_error('Security check failed'); } if (!current_user_can('edit_post', $post_id)) { wp_send_json_error('Permission denied'); } update_post_meta($post_id, '_ahmaipsu_enabled', '1'); wp_send_json_success(array('enabled' => 1)); }