'; /** * Client instance (reserved for future use) * @var mixed */ private $client = null; /** * Notices array * @var array */ public $notices = []; /** * Stories per page limit * @var int */ public $limit = 50; /** * Plan limit * @var mixed */ private $plan_limit_aiautotool; /** * Plan name * @var string */ public $name_plan; /** * Configuration array * @var array */ public $config = array(); /** * Notice message * @var string */ public $notice; /** * Constructor * * Initializes the Web Stories feature: * - Loads settings from database * - Sets default logo if not configured * - Checks if feature is active * - Registers AJAX handlers * * @since 1.0.0 */ public function __construct() { $this->name_plan = __('Webstories', 'ai-auto-tool'); $this->setting = get_option($this->namesetting, array()); // Set default logo if not configured if (!isset($this->setting['logo'])) { // Use local plugin logo as fallback instead of external URL $this->setting['logo'] = defined('AIAUTOTOOL_URI') ? AIAUTOTOOL_URI . 'images/logo.svg' : plugins_url('../../images/logo.svg', __FILE__); } $this->active = get_option($this->active_option_name, false); // Initialize if active if ($this->active == 'true') { $this->init(); } // Register AJAX handlers add_action('wp_ajax_update_active_option_canonical_' . $this->active_option_name, array($this, 'update_active_option_callback')); } /** * AJAX callback to update active option * * Handles the activation/deactivation of the feature. * Verifies nonce for security. * * @since 1.0.0 * @return void Dies after output */ public function update_active_option_callback() { check_ajax_referer('aiautotool_nonce', 'security'); if (!current_user_can('manage_options')) { wp_die(__('Permission denied', 'ai-auto-tool'), '', array('response' => 403)); } if (isset($_POST['active'])) { $active = sanitize_text_field($_POST['active']); update_option($this->active_option_name, $active, null, 'no'); // Flush rewrite rules on activation if ($active === 'true') { delete_option($this->aiautotool_rewriter_flushed); } echo $active; } wp_die(); } /** * Initialize the feature * * Registers all necessary hooks: * - Custom rewrite rules * - Query vars * - Template include * - Settings registration * * @since 1.0.0 * @return void */ public function init() { // Hook into WordPress to register the custom URL add_action('init', array($this, 'add_custom_rewrite_rule')); // Hook into WordPress to add custom rewrite rules add_filter('query_vars', array($this, 'custom_rewrite_query_vars')); // Hook into WordPress to handle the custom template add_filter('template_include', array($this, 'custom_rewrite_rule_template')); // Register settings register_setting('aiautotool_webstories_config_group', $this->namesetting, array( 'type' => 'array', 'sanitize_callback' => array($this, 'sanitize_webstories_setting'), )); } /** * Sanitize webstories setting * * @param mixed $value Input value * @return array Sanitized array */ public function sanitize_webstories_setting($value) { if (!is_array($value)) { return array(); } return map_deep($value, 'sanitize_text_field'); } /** * Render settings page * * Displays the configuration interface for: * - Publisher logo (96x96px) * - Background audio files (MP3) * - Story link in posts option * - Google Analytics ID * * @since 1.0.0 * @return void */ public function render_setting() { ?> active == 'true') { echo ''; } } /** * Render feature box * * Displays the feature card in the main plugin dashboard. * Loads description from feature-descriptions.php * * @since 1.0.0 * @return void */ public function render_feature() { // Load feature descriptions require_once plugin_dir_path(dirname(__FILE__)) . 'lib/feature-descriptions.php'; $autoToolBox = new AutoToolBox( $this->icon . " " . esc_html__('Web Stories Generator', 'ai-auto-tool'), AiAutoTool_Feature_Descriptions::get('webstories'), "https://doc.aiautotool.com/features/webstories", $this->active_option_name, $this->active, defined('AIAUTOTOOL_URI') ? AIAUTOTOOL_URI . 'images/logo.svg' : plugins_url('../../images/logo.svg', __FILE__) ); echo $autoToolBox->generateHTML(); } /** * Custom rewrite rule handler * * Displays the stories listing page with pagination. * Shows posts with featured images in a grid layout. * * Output: * - Bootstrap-styled grid * - Post thumbnails and titles * - Pagination * - Links to individual stories * * @since 1.0.0 * @return void */ public function custom_rewrite_rule_handler() { ?> <?php echo esc_html(get_bloginfo('name')); ?> - <?php esc_html_e('Web Stories', 'ai-auto-tool'); ?>

' . esc_html(get_bloginfo('name')) . ''; ?>

'post', 'post_status' => 'publish', 'posts_per_page' => 30, 'orderby' => 'date', 'order' => 'DESC', 'meta_query' => array( array( 'key' => '_thumbnail_id', 'compare' => 'EXISTS', ), ), 'paged' => $paged, ); $query = new WP_Query($args); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); $post_title = get_the_title(); $post_name = get_post_field('post_name', get_the_ID()); $post_thumbnail_url = get_the_post_thumbnail_url(get_the_ID(), 'medium'); ?>
' . esc_html__('No stories found. Create posts with featured images to generate stories.', 'ai-auto-tool') . '
'; } ?>
max_num_pages > 1): ?>

<?php esc_attr_e('Powered by aiautotool.com', 'ai-auto-tool'); ?>
aiautotool_rewriter_flushed)) { flush_rewrite_rules(false); update_option($this->aiautotool_rewriter_flushed, 1); } } /** * Register custom query vars * * Adds custom query variables for stories routing: * - custom_aiwebstories * - postname * - aiwebstories_feed * * @since 1.0.0 * @param array $query_vars Existing query vars * @return array Modified query vars */ public function custom_rewrite_query_vars($query_vars) { $query_vars[] = 'custom_aiwebstories'; $query_vars[] = 'postname'; $query_vars[] = 'aiwebstories_feed'; return $query_vars; } /** * Custom template router * * Routes requests to appropriate handlers: * - Feed requests → custom_feed_template() * - Single story → display_theme_for_postname() * - List page → custom_rewrite_rule_handler() * * @since 1.0.0 * @param string $template Default template path * @return string Template path */ public function custom_rewrite_rule_template($template) { if (get_query_var('custom_aiwebstories')) { if (get_query_var('aiwebstories_feed')) { // RSS feed $this->custom_feed_template(); exit; } else { $postname = get_query_var('postname'); if ($postname) { // Single story $this->display_theme_for_postname($postname); exit; } else { // Stories list $this->custom_rewrite_rule_handler(); exit; } } } return $template; } /** * Display single story * * Generates an AMP-compliant Web Story from a post. * * Process: * 1. Fetch post by slug * 2. Extract images from content * 3. Split content into paragraphs * 4. Generate AMP story pages * 5. Add structured data (Schema.org) * 6. Include social sharing * * @since 1.0.0 * @param string $postname Post slug * @return void Outputs HTML and exits */ public function display_theme_for_postname($postname) { $page = get_page_by_path($postname, OBJECT, 'post'); if (!$page) { // Post not found status_header(404); echo '

' . esc_html__('Story not found', 'ai-auto-tool') . '

'; echo '

' . esc_html__('View all stories', 'ai-auto-tool') . '

'; exit; } // Get post data $post_id = $page->ID; $post_title = get_the_title($post_id); $post_url = get_permalink($post_id); $post_content = get_the_content(null, false, $post_id); $url_thumb = get_the_post_thumbnail_url($post_id, 'large'); $post_date = get_post_time('Y-m-d\TH:i:sP', true, $post_id); $post_author = get_the_author_meta('display_name', get_post_field('post_author', $post_id)); // Extract images from content $image_urls = array(); $dom = new DOMDocument(); libxml_use_internal_errors(true); $dom->loadHTML(mb_convert_encoding($post_content, 'HTML-ENTITIES', 'UTF-8')); libxml_clear_errors(); $images = $dom->getElementsByTagName('img'); foreach ($images as $image) { $src = $image->getAttribute('src'); if ($src && filter_var($src, FILTER_VALIDATE_URL)) { $image_urls[] = $src; } } // Fallback to featured image if (empty($url_thumb) && !empty($image_urls)) { $url_thumb = $image_urls[0]; } // Split content into paragraphs $contenttext = strip_tags($post_content); $paragraphs = array_filter(explode('.', $contenttext), function($p) { return !empty(trim($p)); }); // Prepare structured data $article_body_content = implode("\n", $paragraphs); $article_body_unicode = json_encode($article_body_content, JSON_UNESCAPED_UNICODE); ?> <?php echo esc_html($post_title); ?> setting['mp31'])): ?> background-audio="setting['mp31']); ?>" >

$paragraph) { $paragraph = trim($paragraph); if (empty($paragraph) || strlen($paragraph) < 10) { continue; } $img = $url_thumb; if (isset($image_urls[$index])) { $img = $image_urls[$index]; } ?> . setting['analytic'])): ?>
'post', 'post_status' => 'publish', 'posts_per_page' => 50, 'meta_query' => array( array( 'key' => '_thumbnail_id', 'compare' => 'EXISTS', ), ), ); $query = new WP_Query($args); echo ''; ?> <?php bloginfo_rss('name'); ?> - <?php esc_html_e('Web Stories', 'ai-auto-tool'); ?> have_posts()) { while ($query->have_posts()) { $query->the_post(); $post_name = get_post_field('post_name', get_the_ID()); $urllink = $this->gen_url_storiepost($post_name); $description = get_the_excerpt(); $content = get_the_content(); $thumbnail_url = get_the_post_thumbnail_url(get_the_ID(), 'large'); ?> <?php echo esc_html(get_the_title()); ?> - <?php esc_html_e('Web Story', 'ai-auto-tool'); ?> ]]> ]]> <?php echo esc_attr(get_the_title()); ?>

]]>
0