AMP
7 years ago
Ends
7 years ago
Plugins
7 years ago
Providers
7 years ago
ThirdParty
7 years ago
AutoLoader.php
7 years ago
Compatibility.php
7 years ago
Core.php
7 years ago
CoreLegacy.php
7 years ago
DisablerLegacy.php
7 years ago
Loader.php
7 years ago
RestAPI.php
7 years ago
Shortcode.php
7 years ago
index.html
7 years ago
CoreLegacy.php
416 lines
| 1 | <?php |
| 2 | |
| 3 | namespace EmbedPress; |
| 4 | |
| 5 | use EmbedPress\Ends\Back\Handler as EndHandlerAdmin; |
| 6 | use EmbedPress\Ends\Front\Handler as EndHandlerPublic; |
| 7 | |
| 8 | (defined('ABSPATH') && defined('EMBEDPRESS_IS_LOADED')) or die("No direct script access allowed."); |
| 9 | |
| 10 | /** |
| 11 | * Entity that glues together all pieces that the plugin is made of, for WordPress before 5.0. |
| 12 | * |
| 13 | * @package EmbedPress |
| 14 | * @author EmbedPress <help@embedpress.com> |
| 15 | * @copyright Copyright (C) 2018 EmbedPress. All rights reserved. |
| 16 | * @license GPLv2 or later |
| 17 | * @since 1.0.0 |
| 18 | */ |
| 19 | class CoreLegacy |
| 20 | { |
| 21 | /** |
| 22 | * The name of the plugin. |
| 23 | * |
| 24 | * @since 1.0.0 |
| 25 | * @access protected |
| 26 | * |
| 27 | * @var string $pluginName The name of the plugin. |
| 28 | */ |
| 29 | protected $pluginName; |
| 30 | |
| 31 | /** |
| 32 | * The version of the plugin. |
| 33 | * |
| 34 | * @since 1.0.0 |
| 35 | * @access protected |
| 36 | * |
| 37 | * @var string $pluginVersion The version of the plugin. |
| 38 | */ |
| 39 | protected $pluginVersion; |
| 40 | |
| 41 | /** |
| 42 | * An instance of the plugin loader. |
| 43 | * |
| 44 | * @since 1.0.0 |
| 45 | * @access protected |
| 46 | * |
| 47 | * @var \EmbedPress\Loader $pluginVersion The version of the plugin. |
| 48 | */ |
| 49 | protected $loaderInstance; |
| 50 | |
| 51 | /** |
| 52 | * An associative array storing all registered/active EmbedPress plugins and their namespaces. |
| 53 | * |
| 54 | * @since 1.4.0 |
| 55 | * @access private |
| 56 | * @static |
| 57 | * |
| 58 | * @var array |
| 59 | */ |
| 60 | private static $plugins = []; |
| 61 | |
| 62 | /** |
| 63 | * Initialize the plugin and set its properties. |
| 64 | * |
| 65 | * @since 1.0.0 |
| 66 | * |
| 67 | * @return void |
| 68 | */ |
| 69 | public function __construct() |
| 70 | { |
| 71 | $this->pluginName = EMBEDPRESS_PLG_NAME; |
| 72 | $this->pluginVersion = EMBEDPRESS_VERSION; |
| 73 | |
| 74 | $this->loaderInstance = new Loader(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Method that retrieves the plugin name. |
| 79 | * |
| 80 | * @since 1.0.0 |
| 81 | * |
| 82 | * @return string |
| 83 | */ |
| 84 | public function getPluginName() |
| 85 | { |
| 86 | return $this->pluginName; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Method that retrieves the plugin version. |
| 91 | * |
| 92 | * @since 1.0.0 |
| 93 | * |
| 94 | * @return string |
| 95 | */ |
| 96 | public function getPluginVersion() |
| 97 | { |
| 98 | return $this->pluginVersion; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Method that retrieves the loader instance. |
| 103 | * |
| 104 | * @since 1.0.0 |
| 105 | * |
| 106 | * @return \EmbedPress\Loader |
| 107 | */ |
| 108 | public function getLoader() |
| 109 | { |
| 110 | return $this->loaderInstance; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Method responsible to connect all required hooks in order to make the plugin work. |
| 115 | * |
| 116 | * @since 1.0.0 |
| 117 | * |
| 118 | * @return void |
| 119 | */ |
| 120 | public function initialize() |
| 121 | { |
| 122 | global $wp_actions; |
| 123 | |
| 124 | if (is_admin()) { |
| 125 | $plgSettings = self::getSettings(); |
| 126 | |
| 127 | $settingsClassNamespace = '\\EmbedPress\\Ends\\Back\\Settings'; |
| 128 | add_action('admin_menu', [$settingsClassNamespace, 'registerMenuItem']); |
| 129 | add_action('admin_init', [$settingsClassNamespace, 'registerActions']); |
| 130 | unset($settingsClassNamespace); |
| 131 | |
| 132 | add_filter('plugin_action_links_embedpress/embedpress.php', |
| 133 | ['\\EmbedPress\\CoreLegacy', 'handleActionLinks'], 10, 2); |
| 134 | |
| 135 | add_action('admin_enqueue_scripts', ['\\EmbedPress\\Ends\\Back\\Handler', 'enqueueStyles']); |
| 136 | |
| 137 | add_action('init', ['\\EmbedPress\\DisablerLegacy', 'run'], 1); |
| 138 | add_action('init', [$this, 'configureTinyMCE'], 1); |
| 139 | |
| 140 | $plgHandlerAdminInstance = new EndHandlerAdmin($this->getPluginName(), $this->getPluginVersion()); |
| 141 | |
| 142 | if ((bool)$plgSettings->enablePluginInAdmin) { |
| 143 | $this->loaderInstance->add_action('admin_enqueue_scripts', $plgHandlerAdminInstance, 'enqueueScripts'); |
| 144 | } |
| 145 | |
| 146 | $onAjaxCallbackName = "doShortcodeReceivedViaAjax"; |
| 147 | $this->loaderInstance->add_action('wp_ajax_embedpress_do_ajax_request', $plgHandlerAdminInstance, |
| 148 | $onAjaxCallbackName); |
| 149 | $this->loaderInstance->add_action('wp_ajax_nopriv_embedpress_do_ajax_request', $plgHandlerAdminInstance, |
| 150 | $onAjaxCallbackName); |
| 151 | |
| 152 | $this->loaderInstance->add_action('wp_ajax_embedpress_get_embed_url_info', $plgHandlerAdminInstance, |
| 153 | "getUrlInfoViaAjax"); |
| 154 | |
| 155 | unset($onAjaxCallbackName, $plgHandlerAdminInstance); |
| 156 | } else { |
| 157 | add_action('init', ['\\EmbedPress\\DisablerLegacy', 'run'], 1); |
| 158 | |
| 159 | $plgHandlerPublicInstance = new EndHandlerPublic($this->getPluginName(), $this->getPluginVersion()); |
| 160 | |
| 161 | $this->loaderInstance->add_action('wp_enqueue_scripts', $plgHandlerPublicInstance, 'enqueueScripts'); |
| 162 | $this->loaderInstance->add_action('wp_enqueue_scripts', $plgHandlerPublicInstance, 'enqueueStyles'); |
| 163 | |
| 164 | unset($plgHandlerPublicInstance); |
| 165 | } |
| 166 | |
| 167 | // Add support for embeds on AMP pages |
| 168 | add_filter('pp_embed_parsed_content', ['\\EmbedPress\\AMP\\EmbedHandler', 'processParsedContent'], 10, 3); |
| 169 | |
| 170 | // Add support for our embeds on Beaver Builder. Without this it only run the native embeds. |
| 171 | add_filter('fl_builder_before_render_shortcodes', |
| 172 | ['\\EmbedPress\\ThirdParty\\BeaverBuilder', 'before_render_shortcodes']); |
| 173 | |
| 174 | $this->loaderInstance->run(); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Callback called right after the plugin has been activated. |
| 179 | * |
| 180 | * @since 1.0.0 |
| 181 | * @static |
| 182 | * |
| 183 | * @return void |
| 184 | */ |
| 185 | public static function onPluginActivationCallback() |
| 186 | { |
| 187 | add_filter('rewrite_rules_array', ['\\EmbedPress\\DisablerLegacy', 'disableDefaultEmbedRewriteRules']); |
| 188 | flush_rewrite_rules(); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Callback called right after the plugin has been deactivated. |
| 193 | * |
| 194 | * @since 1.0.0 |
| 195 | * @static |
| 196 | * |
| 197 | * @return void |
| 198 | */ |
| 199 | public static function onPluginDeactivationCallback() |
| 200 | { |
| 201 | remove_filter('rewrite_rules_array', ['\\EmbedPress\\DisablerLegacy', 'disableDefaultEmbedRewriteRules']); |
| 202 | flush_rewrite_rules(); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Method that retrieves all additional service providers defined in the ~<plugin_root_path>/providers.php file. |
| 207 | * |
| 208 | * @since 1.0.0 |
| 209 | * @static |
| 210 | * |
| 211 | * @return array |
| 212 | */ |
| 213 | public static function getAdditionalServiceProviders() |
| 214 | { |
| 215 | $additionalProvidersFilePath = EMBEDPRESS_PATH_BASE . 'providers.php'; |
| 216 | if (file_exists($additionalProvidersFilePath)) { |
| 217 | include $additionalProvidersFilePath; |
| 218 | |
| 219 | if (isset($additionalServiceProviders)) { |
| 220 | return $additionalServiceProviders; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | return []; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Method that checks if an embed of a given service provider can be responsive. |
| 229 | * |
| 230 | * @since 1.0.0 |
| 231 | * @static |
| 232 | * |
| 233 | * @param string $serviceProviderAlias The service's slug. |
| 234 | * |
| 235 | * @return boolean |
| 236 | */ |
| 237 | public static function canServiceProviderBeResponsive($serviceProviderAlias) |
| 238 | { |
| 239 | $providers = [ |
| 240 | |
| 241 | ]; |
| 242 | |
| 243 | return in_array($serviceProviderAlias, [ |
| 244 | "dailymotion", |
| 245 | "kickstarter", |
| 246 | "rutube", |
| 247 | "ted", |
| 248 | "vimeo", |
| 249 | "youtube", |
| 250 | "ustream", |
| 251 | "google-docs", |
| 252 | "animatron", |
| 253 | "amcharts", |
| 254 | "on-aol-com", |
| 255 | "animoto", |
| 256 | "videojug", |
| 257 | 'issuu', |
| 258 | ]); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Method that retrieves the plugin settings defined by the user. |
| 263 | * |
| 264 | * @since 1.0.0 |
| 265 | * @static |
| 266 | * |
| 267 | * @return object |
| 268 | */ |
| 269 | public static function getSettings() |
| 270 | { |
| 271 | $settings = get_option(EMBEDPRESS_PLG_NAME); |
| 272 | |
| 273 | if ( ! isset($settings['enablePluginInAdmin'])) { |
| 274 | $settings['enablePluginInAdmin'] = true; |
| 275 | } |
| 276 | |
| 277 | if ( ! isset($settings['enablePluginInFront'])) { |
| 278 | $settings['enablePluginInFront'] = true; |
| 279 | } |
| 280 | |
| 281 | return (object)$settings; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Method that register an EmbedPress plugin. |
| 286 | * |
| 287 | * @since 1.4.0 |
| 288 | * @static |
| 289 | * |
| 290 | * @param array $pluginMeta Associative array containing plugin's name, slug and namespace |
| 291 | * |
| 292 | * @return void |
| 293 | */ |
| 294 | public static function registerPlugin($pluginMeta) |
| 295 | { |
| 296 | $pluginMeta = json_decode(json_encode($pluginMeta)); |
| 297 | |
| 298 | if (empty($pluginMeta->name) || empty($pluginMeta->slug) || empty($pluginMeta->namespace)) { |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | if ( ! isset(self::$plugins[$pluginMeta->slug])) { |
| 303 | AutoLoader::register($pluginMeta->namespace, |
| 304 | WP_PLUGIN_DIR . '/' . EMBEDPRESS_PLG_NAME . '-' . $pluginMeta->slug . '/' . $pluginMeta->name); |
| 305 | |
| 306 | $plugin = "{$pluginMeta->namespace}\Plugin"; |
| 307 | if (\defined("{$plugin}::SLUG") && $plugin::SLUG !== null) { |
| 308 | self::$plugins[$pluginMeta->slug] = $pluginMeta->namespace; |
| 309 | |
| 310 | $bsFilePath = $plugin::PATH . EMBEDPRESS_PLG_NAME . '-' . $plugin::SLUG . '.php'; |
| 311 | |
| 312 | register_activation_hook($bsFilePath, [$plugin::NAMESPACE_STRING, 'onActivationCallback']); |
| 313 | register_deactivation_hook($bsFilePath, [$plugin::NAMESPACE_STRING, 'onDeactivationCallback']); |
| 314 | |
| 315 | add_action('admin_init', [$plugin, 'onLoadAdminCallback']); |
| 316 | |
| 317 | add_action(EMBEDPRESS_PLG_NAME . ':' . $plugin::SLUG . ':settings:register', |
| 318 | [$plugin, 'registerSettings']); |
| 319 | add_action(EMBEDPRESS_PLG_NAME . ':settings:render:tab', [$plugin, 'renderTab']); |
| 320 | |
| 321 | add_filter('plugin_action_links_embedpress-' . $plugin::SLUG . '/embedpress-' . $plugin::SLUG . '.php', |
| 322 | [$plugin, 'handleActionLinks'], 10, 2); |
| 323 | |
| 324 | $plugin::registerEvents(); |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Retrieve all registered plugins. |
| 331 | * |
| 332 | * @since 1.4.0 |
| 333 | * @static |
| 334 | * |
| 335 | * @return array |
| 336 | */ |
| 337 | public static function getPlugins() |
| 338 | { |
| 339 | return self::$plugins; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Handle links displayed below the plugin name in the WordPress Installed Plugins page. |
| 344 | * |
| 345 | * @since 1.4.0 |
| 346 | * @static |
| 347 | * |
| 348 | * @return array |
| 349 | */ |
| 350 | public static function handleActionLinks($links, $file) |
| 351 | { |
| 352 | $settingsLink = '<a href="' . admin_url('admin.php?page=embedpress') . '" aria-label="' . __('Open settings page', |
| 353 | 'embedpress') . '">' . __('Settings', 'embedpress') . '</a>'; |
| 354 | |
| 355 | array_unshift($links, $settingsLink); |
| 356 | |
| 357 | return $links; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Method that ensures the API's url are whitelisted to WordPress external requests. |
| 362 | * |
| 363 | * @since 1.4.0 |
| 364 | * @static |
| 365 | * |
| 366 | * @param boolean $isAllowed |
| 367 | * @param string $host |
| 368 | * @param string $url |
| 369 | * |
| 370 | * @return boolean |
| 371 | */ |
| 372 | public static function allowApiHost($isAllowed, $host, $url) |
| 373 | { |
| 374 | if ($host === EMBEDPRESS_LICENSES_API_HOST) { |
| 375 | $isAllowed = true; |
| 376 | } |
| 377 | |
| 378 | return $isAllowed; |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Add filters to configure the TinyMCE editor. |
| 383 | * |
| 384 | * @since 1.6.2 |
| 385 | */ |
| 386 | public function configureTinyMCE() |
| 387 | { |
| 388 | add_filter('teeny_mce_before_init', [$this, 'hookOnPaste']); |
| 389 | add_filter('tiny_mce_before_init', [$this, 'hookOnPaste']); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Hook the onPaste methof to the paste_preprocess config in the editor. |
| 394 | * |
| 395 | * @since 1.6.2 |
| 396 | * |
| 397 | * @param array $mceInit |
| 398 | * |
| 399 | * @return array |
| 400 | */ |
| 401 | public function hookOnPaste($mceInit) |
| 402 | { |
| 403 | $settings = self::getSettings(); |
| 404 | |
| 405 | if (isset($settings->enablePluginInAdmin) && $settings->enablePluginInAdmin) { |
| 406 | // We hook here because the onPaste is sometimes called after the content was already added to the editor. |
| 407 | // If you copy text from the editor and paste there, it will give no way to use a normal onPaste event hook |
| 408 | // to modify the input since it was already injected. |
| 409 | $mceInit['paste_preprocess'] = 'function (plugin, args) {EmbedPress.onPaste(plugin, args);}'; |
| 410 | } |
| 411 | |
| 412 | |
| 413 | return $mceInit; |
| 414 | } |
| 415 | } |
| 416 |