Handler.php
108 lines
| 1 | <?php |
| 2 | |
| 3 | namespace EmbedPress\Ends\Front; |
| 4 | |
| 5 | use EmbedPress\Core; |
| 6 | use EmbedPress\Ends\Back\Handler as BackEndHandler; |
| 7 | use EmbedPress\Ends\Handler as EndHandlerAbstract; |
| 8 | use EmbedPress\Shortcode; |
| 9 | |
| 10 | (defined('ABSPATH') && defined('EMBEDPRESS_IS_LOADED')) or die("No direct script access allowed."); |
| 11 | |
| 12 | /** |
| 13 | * The public-facing functionality of the plugin. |
| 14 | * Defines the plugin name, version, and enqueue the public-specific stylesheets and scripts. |
| 15 | * |
| 16 | * @package EmbedPress |
| 17 | * @subpackage EmbedPress/Ends/Front |
| 18 | * @author EmbedPress <help@embedpress.com> |
| 19 | * @copyright Copyright (C) 2018 EmbedPress. All rights reserved. |
| 20 | * @license GPLv2 or later |
| 21 | * @since 1.0.0 |
| 22 | */ |
| 23 | class Handler extends EndHandlerAbstract |
| 24 | { |
| 25 | /** |
| 26 | * Method that register all stylesheets for the public area. |
| 27 | * |
| 28 | * @since 1.0.0 |
| 29 | * @static |
| 30 | * |
| 31 | * @return void |
| 32 | */ |
| 33 | public static function enqueueStyles() |
| 34 | { |
| 35 | wp_enqueue_style(EMBEDPRESS_PLG_NAME, EMBEDPRESS_URL_ASSETS . 'css/embedpress.css'); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Passes any unlinked URLs to EmbedPress\Shortcode::do_shortcode() for potential embedding. |
| 40 | * |
| 41 | * @since 1.5.0 |
| 42 | * @static |
| 43 | * |
| 44 | * @param string $content The content to be searched. |
| 45 | * |
| 46 | * @return string The potentially modified content. |
| 47 | */ |
| 48 | public static function autoEmbedUrls($content) |
| 49 | { |
| 50 | // Replace line breaks from all HTML elements with placeholders. |
| 51 | $content = wp_replace_in_html_tags($content, ["\n" => '<!-- embedpress-line-break -->']); |
| 52 | |
| 53 | // Look for links in the content (not wrapped by shortcode) |
| 54 | if (preg_match('#(^|\s|>)https?://#i', $content)) { |
| 55 | $callbackFingerprint = ['\\EmbedPress\\Ends\\Front\\Handler', 'autoEmbedUrlsCallback']; |
| 56 | |
| 57 | // Find URLs on their own line. |
| 58 | $content = preg_replace_callback('|^(\s*)(https?://[^\s<>"]+)(\s*)$|im', $callbackFingerprint, $content); |
| 59 | // Find URLs in their own paragraph. |
| 60 | $content = preg_replace_callback('|(<p(?: [^>]*)?>\s*)(https?://[^\s<>"]+)(\s*<\/p>)|i', |
| 61 | $callbackFingerprint, $content); |
| 62 | } |
| 63 | |
| 64 | // Put the line breaks back. |
| 65 | return str_replace('<!-- embedpress-line-break -->', "\n", $content); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Callback function for \EmbedPress\Ends\Front\Handler::autoEmbedUrls(). |
| 70 | * |
| 71 | * @since 1.5.0 |
| 72 | * @static |
| 73 | * |
| 74 | * @param array $match A regex match array. |
| 75 | * |
| 76 | * @return string The embed HTML on success, otherwise the original URL. |
| 77 | */ |
| 78 | public static function autoEmbedUrlsCallback($match) |
| 79 | { |
| 80 | $return = Shortcode::do_shortcode([], $match[2]); |
| 81 | |
| 82 | return $match[1] . $return . $match[3]; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * A callback called by the WP `the_editor` filter. |
| 87 | * |
| 88 | * @since 1.6.0 |
| 89 | * @static |
| 90 | * |
| 91 | * @param string $editorHTML The HTML which will be rendered as an editor, like TinyMCE. |
| 92 | * |
| 93 | * @return string The HTML which will be rendered as an editor, like TinyMCE |
| 94 | */ |
| 95 | public static function renderPreviewBoxInEditors($editorHTML) |
| 96 | { |
| 97 | $plgSettings = Core::getSettings(); |
| 98 | |
| 99 | if ( ! is_admin() && (bool)$plgSettings->enablePluginInFront) { |
| 100 | $backEndHandler = new BackEndHandler(EMBEDPRESS_PLG_NAME, EMBEDPRESS_VERSION); |
| 101 | |
| 102 | $backEndHandler->enqueueScripts(); |
| 103 | } |
| 104 | |
| 105 | return $editorHTML; |
| 106 | } |
| 107 | } |
| 108 |