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