'', ), $atts, 'gdocs_markdown' ); // Validate document ID if (empty($atts['id'])) { return '

' . esc_html__('Error: Google Doc ID is required.', 'gdocs-markdown') . '

'; } // Build the Google Docs export URL $url = sprintf( 'https://docs.google.com/document/d/%s/export?format=md', esc_attr($atts['id']) ); // Fetch the markdown content $response = wp_remote_get($url); // Check for errors if (is_wp_error($response)) { return '

' . esc_html__('Error: Unable to fetch document content.', 'gdocs-markdown') . '

'; } // Get the response body $markdown = wp_remote_retrieve_body($response); // Check if content is empty if (empty($markdown)) { return '

' . esc_html__('Error: Document is empty or not accessible.', 'gdocs-markdown') . '

'; } // Clean markdown by removing images $markdown = $this->clean_markdown($markdown); // Include Parsedown if it exists $parsedown_path = plugin_dir_path(__FILE__) . 'Parsedown.php'; if (!file_exists($parsedown_path)) { return '

' . esc_html__('Error: Parsedown library not found.', 'gdocs-markdown') . '

'; } require_once $parsedown_path; try { // Create Parsedown instance and convert markdown to HTML $parsedown = new Parsedown(); $parsedown->setSafeMode(true); $html = $parsedown->text($markdown); // Define allowed HTML tags $allowed_html = array( 'div' => array( 'class' => array(), ), 'p' => array(), 'h1' => array(), 'h2' => array(), 'h3' => array(), 'h4' => array(), 'h5' => array(), 'h6' => array(), 'strong' => array(), 'em' => array(), 'blockquote' => array(), 'ul' => array(), 'ol' => array(), 'li' => array(), 'a' => array( 'href' => array(), 'title' => array(), ), ); // Return the processed content return wp_kses( sprintf('
%s
', $html), $allowed_html ); } catch (Exception $e) { return '

' . esc_html__('Error: Failed to parse markdown content.', 'gdocs-markdown') . '

'; } } } // Initialize the plugin new GDocsMarkdown(); // Activation hook register_activation_hook(__FILE__, 'gdocs_markdown_activate'); /** * Plugin activation callback */ function gdocs_markdown_activate() { // Check if Parsedown exists $parsedown_path = plugin_dir_path(__FILE__) . 'Parsedown.php'; if (!file_exists($parsedown_path)) { wp_die( esc_html__('Parsedown library is required but not found. Please install Parsedown.php in the plugin directory.', 'gdocs-markdown'), esc_html__('Plugin Activation Error', 'gdocs-markdown'), array('back_link' => true) ); } }