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