From 6cdbfa1b0610d17a5c12a1bc4becc996157c1dfb Mon Sep 17 00:00:00 2001 From: Nicolas Morand Date: Thu, 21 Jun 2018 11:50:00 +0200 Subject: [PATCH 1/9] Gets the provider class out to create an master extension. --- src/Provider.php | 589 ----------------------------------------------- 1 file changed, 589 deletions(-) delete mode 100644 src/Provider.php diff --git a/src/Provider.php b/src/Provider.php deleted file mode 100644 index 33f46e1..0000000 --- a/src/Provider.php +++ /dev/null @@ -1,589 +0,0 @@ - array( - * 'scheme' => '#^(http|https)://(www\.)?(youtube\.com/(watch\?v=|embed/|v/)|youtu\.be/)(([^&?/]+)?)#i', - * 'id' => '5', - * 'glue' => '&', - * ), - * 'list' => array( - * 'scheme' => '#^(http|https)://(www\.)?(youtube\.com/(watch\?v=|embed/|v/)|youtu\.be/)[\S]+list=([^&?/]+)?#i', - * 'id' => '5', - * 'prefix' => 'list=', - * ), - * ); - * - * Where 'video' and 'list' are used to define the 'type' key of the $infos property - * when an URL match the regular expression defined as the 'scheme'. - * 'id' stores the index of the string to get from the matches. - * If set, the 'glue' key allows to test multiple schemes and stick ID's with its value. - * 'prefix' can defines an ID prefix. - */ - - protected static $patterns = array(); - - /** - * The player base path. - * - * @var string - */ - - protected static $src; - - /** - * URL of a script to embed. - * - * @var string - */ - - protected static $script; - - /** - * Whether the script is already embed or not. - * - * @var bool - */ - - protected static $scriptEmbedded = false; - - /** - * Default player size. - * - * @var array - */ - - protected static $dims = array( - 'width' => array( - 'default' => '640', - ), - 'height' => array( - 'default' => '', - ), - 'ratio' => array( - 'default' => '16:9', - ), - ); - - /** - * Player parameters and related options/values. - * - * @var array - * @example - * protected static $params = array( - * 'size' => array( - * 'default' => 'large', - * 'force' => true, - * 'valid' => array('large', 'small'), - * ), - * ); - * - * Where 'size' is a player parameter and 'large' is its default value. - * 'force' allows to set the parameter even if its value is the default one. - * The 'valid' key accept an array of values or a type of values as an HTML input type. - */ - - protected static $params = array(); - - /** - * Strings sticking different player URL parts. - * - * @var array - */ - - protected static $glue = array('/', '?', '&'); - - /** - * Caches the class instance. - * - * @var object - */ - - private static $instance = null; - - /** - * Singleton. - */ - - public static function getInstance() - { - $class = get_called_class(); - - if (!isset(self::$instance[$class])) { - self::$instance[$class] = new static(); - } - - return self::$instance[$class]; - } - - /** - * Constructor. - */ - - protected function __construct() - { - self::setProvider(); - self::setExtension(); - - $extension = self::getExtension(); - - add_privs('plugin_prefs.' . $extension, Admin::getPrivs()); - - register_callback( - 'Oui\Player\Provider::optionsLink', - 'plugin_prefs.' . $extension, - null, - 1 - ); - } - - /** - * Gets the play property. - * - * @throws \Exception - */ - - public function setPlay($value, $fallback = false) - { - $this->play = $value; - $infos = $this->getInfos(); - - if (!$infos || !array_key_exists($value, $infos)) { - $this->setInfos($fallback); - } - - return $this; - } - - /** - * Gets the play property. - */ - - protected function getPlay() - { - return explode(', ', $this->play); - } - - /** - * Gets the play property. - */ - - public function setConfig($value) - { - $this->config = $value; - - return $this; - } - - protected function getConfig($att = null) - { - return $att ? $this->config[$att] : $this->config; - } - - protected static function setExtension() - { - static::$extension = Player::getPlugin() . '_' . strtolower(self::getProvider()[0]); - } - - protected static function getExtension() - { - return static::$extension; - } - - protected static function setProvider() - { - static::$provider = substr(strrchr(get_called_class(), '\\'), 1); - } - - /** - * Gets the class name as the provider name. - */ - - public static function getProvider() - { - self::setProvider(); - - return array(static::$provider); - } - - protected static function getScript($wrap = false) - { - if (isset(static::$script)) { - return $wrap ? '' : static::$script; - } - - return false; - } - - protected static function getScriptEmbedded() - { - return static::$scriptEmbedded; - } - - protected static function getDims() - { - return static::$dims; - } - - protected static function getParams() - { - return static::$params; - } - - protected static function getPatterns() - { - return static::$patterns; - } - - protected static function getSrc() - { - return static::$src; - } - - protected static function getGlue($i = null) - { - return $i ? static::$glue[$i] : static::$glue; - } - - protected static function setGlue($i, $value) - { - static::$glue[$i] = $value; - } - - /** - * Links 'options' to the prefs panel. - */ - - public static function optionsLink() - { - header('Location: ?event=prefs#prefs_group_' . self::getExtension()); - } - - /** - * Embeds the provider script. - */ - - public function embedScript() - { - if ($ob = ob_get_contents()) { - ob_clean(); - - echo str_replace( - '', - self::getScript(true) . n . '', - $ob - ); - } - } - - /** - * Collects provider prefs. - * - * @param array $prefs Prefs collected provider after provider. - * @return array Collected prefs merged with ones already provided. - */ - - public static function getPrefs($prefs) - { - $merge_prefs = array_merge(self::getDims(), self::getParams()); - - foreach ($merge_prefs as $pref => $options) { - $options['group'] = strtolower(str_replace('\\', '_', get_called_class())); - $pref = $options['group'] . '_' . $pref; - $prefs[$pref] = $options; - } - - return $prefs; - } - - /** - * Get tag attributes. - * - * @param string $tag The plugin tag. - * @param array $get_atts Stores attributes provider after provider. - * @return array Attributes - */ - - public static function getAtts($tag, $get_atts) - { - $atts = array_merge(self::getDims(), self::getParams()); - - foreach ($atts as $att => $options) { - $att = str_replace('-', '_', $att); - $get_atts[$att] = ''; - } - - return $get_atts; - } - - /** - * Sets the current media(s) infos. - * - * @return array The current media(s) infos. - */ - - public function setInfos($fallback = false) - { - $this->infos = array(); - - foreach ($this->getPlay() as $play) { - $notId = preg_match('/([.][a-z]+)/', $play); // URL or filename. - - if ($notId) { - $glue = null; - - foreach (self::getPatterns() as $pattern => $options) { - if (preg_match($options['scheme'], $play, $matches)) { - $prefix = isset($options['prefix']) ? $options['prefix'] : ''; - - if (!array_key_exists($play, $this->infos)) { - $this->infos[$play] = array( - 'play' => $prefix . $matches[$options['id']], - 'type' => $pattern, - ); - - // Bandcamp and Youtube accept multiple matches. - if (!isset($options['glue'])) { - break; - } else { - $glue = $options['glue']; - } - } else { - $this->infos[$play]['play'] .= $glue . $prefix . $matches[$options['id']]; - $this->infos[$play]['type'] = $pattern; - } - } - } - } elseif ($fallback) { - $this->infos[$play] = array( - 'play' => $play, - 'type' => 'id', - ); - } - } - - - return $this; - } - - /** - * Gets the infos property; set it if necessary. - * - * @return array An associative array of - */ - - public function getInfos() - { - return $this->infos; - } - - /** - * Gets player parameters in in use - * from the plugin tag attributes - * or from the plugin prefs. - * - * @return array Parameters and their values. - */ - - protected function getPlayerParams() - { - $params = array(); - - foreach (self::getParams() as $param => $infos) { - $pref = get_pref(strtolower(str_replace('\\', '_', get_class($this))) . '_' . $param); - $default = $infos['default']; - $att = str_replace('-', '_', $param); - $config = $this->getConfig(); - $value = isset($config[$att]) ? $config[$att] : ''; - - // Adds attributes values in use or modified prefs values as player parameters. - if ($value === '' && ($pref !== $default || isset($infos['force']))) { - // Removes # from the color pref as a color type is used for the pref input. - $params[] = $param . '=' . str_replace('#', '', $pref); - } elseif ($value !== '') { - // Removes the # in the color attribute just in case… - $params[] = $param . '=' . str_replace('#', '', $value); - } - } - - return $params; - } - - /** - * Gets the player size - * from the plugin tag attributes - * or from the plugin prefs. - * - * @return array Player size. - */ - - protected function getSize() - { - foreach (self::getDims() as $dim => $infos) { - $pref = get_pref(strtolower(str_replace('\\', '_', get_class($this))) . '_' . $dim); - $config = $this->getConfig(); - $att = isset($config[$dim]) ? $config[$dim] : ''; - $$dim = $att ? $att : $pref; - } - - if (!$width && !$height) { - trigger_error(gtxt('undefined_player_size')); - } elseif (!$width || !$height) { - if ($ratio) { - // Works out the aspect ratio. - preg_match("/(\d+):(\d+)/", $ratio, $matches); - - if ($matches && $matches[1]!=0 && $matches[2]!=0) { - $aspect = $matches[1] / $matches[2]; - } else { - trigger_error(gtxt('invalid_player_ratio')); - } - - // Calculates the new width/height. - if ($width) { - $height = $width / $aspect; - // Has unit? - preg_match("/(\D+)/", $width, $unit); - // Adds unit if it exists. - isset($unit[0]) ? $height .= $unit[0] : ''; - } else { - $width = $height * $aspect; - preg_match("/(\D+)/", $height, $unit); - isset($unit[0]) ? $width .= $unit[0] : ''; - } - } else { - trigger_error(gtxt('undefined_player_size')); - } - } - - return array('width' => $width, 'height' => $height); - } - - /** - * Whether a provided URL to play matches a provider URL scheme or not. - * - * @return bool - */ - - public function isValid() - { - return $this->getInfos(); - } - - protected function getPlaySrc() - { - $play = $this->getInfos()[$this->getPlay()[0]]['play']; - $glue = self::getGlue(); - $src = self::getSrc() . $glue[0] . $play; - $params = $this->getPlayerParams(); - - if (!empty($params)) { - $joint = strpos($src, $glue[1]) ? $glue[2] : $glue[1]; - $src .= $joint . implode($glue[2], $params); - } - - return $src; - } - - /** - * Generates the player. - * - * @return string HTML - */ - - public function getPlayer() - { - if (self::getScript() && !self::getScriptEmbedded()) { - register_callback(array($this, 'embedScript'), 'textpattern_end'); - static::$scriptEmbedded = true; - } - - $src = $this->getPlaySrc(); - $dims = $this->getSize(); - - extract($dims); - - return sprintf( - '', - $width, - $height, - $src, - 'frameborder="0" allowfullscreen' - ); - } - } -} From 43eb99f0b2d911447b18581ebed2cc625049382a Mon Sep 17 00:00:00 2001 From: Nicolas Morand Date: Thu, 21 Jun 2018 11:50:50 +0200 Subject: [PATCH 2/9] Removes single quotes in comments --- src/Public.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Public.php b/src/Public.php index a5a8127..fd252e6 100644 --- a/src/Public.php +++ b/src/Public.php @@ -31,7 +31,7 @@ class Main extends Player { /** - * The value provided through the 'play' + * The value provided through the play * attribute value of the plugin tag. * * @var string @@ -48,7 +48,7 @@ class Main extends Player protected $provider; /** - * Associative array of 'play' value(s) and their. + * Associative array of play value(s) and their. * * @var array */ From ba5b04b3c0129fb8a0bb528b8a6e134a429f0cf6 Mon Sep 17 00:00:00 2001 From: Nicolas Morand Date: Thu, 21 Jun 2018 11:53:47 +0200 Subject: [PATCH 3/9] Fixes the conditional tag --- src/tags.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/tags.php b/src/tags.php index 37e8d86..c3461b9 100644 --- a/src/tags.php +++ b/src/tags.php @@ -85,18 +85,18 @@ function oui_if_player($atts, $thing) $field = get_pref('oui_player_custom_field'); - if ($play && isset($thisarticle[$play])) { - $play = $thisarticle[$play]; - } elseif (!$play && isset($thisarticle[$field])) { - $play = $thisarticle[$field]; - } else { - $play = false; + if (!$play) { + if (!$play && isset($thisarticle[$field])) { + $play = $thisarticle[$field]; + } else { + $play = false; + } } if ($play) { $class_in_use = $provider ? $namespace . '\\' . ucfirst($provider) : $main_class; - if ($is_valid = $class_in_use::getInstance()->setPlay()->isValid()) { + if ($is_valid = $class_in_use::getInstance()->setPlay($play)->isValid()) { $oui_player_item = array('play' => $play); $provider ? $oui_player_item['provider'] = $provider : ''; } From 7cd1dcae88ad45325b93ea8e2e6cf1f6fb68a99d Mon Sep 17 00:00:00 2001 From: Nicolas Morand Date: Thu, 21 Jun 2018 11:55:51 +0200 Subject: [PATCH 4/9] Prepares responsive embedding --- src/Player.php | 7 +++++++ src/tags.php | 4 ++-- textpacks/en-gb.textpack | 1 + textpacks/fr-fr.textpack | 1 + 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Player.php b/src/Player.php index c5106fa..bd0ba1d 100644 --- a/src/Player.php +++ b/src/Player.php @@ -73,6 +73,9 @@ class Player 'wraptag' => array( 'default' => '', ), + 'responsive' => array( + 'default' => '', + ), ), 'oui_if_player' => array( 'play' => array( @@ -107,6 +110,10 @@ class Player 'provider' => array( 'default' => '', ), + 'responsive' => array( + 'default' => 'true', + 'valid' => array('true', 'false'), + ), 'providers' => array( 'default' => '', ), diff --git a/src/tags.php b/src/tags.php index c3461b9..2955ca2 100644 --- a/src/tags.php +++ b/src/tags.php @@ -58,9 +58,9 @@ function oui_player($atts) $player = $class_in_use::getInstance() ->setPlay($play, true) ->setConfig($lAtts) - ->getPlayer(); + ->getPlayer($wraptag, $class); - return doLabel($label, $labeltag).(($wraptag) ? doTag($player, $wraptag, $class) : $player); + return doLabel($label, $labeltag) . $player; } /** diff --git a/textpacks/en-gb.textpack b/textpacks/en-gb.textpack index 3c9c103..28b3c7c 100644 --- a/textpacks/en-gb.textpack +++ b/textpacks/en-gb.textpack @@ -3,3 +3,4 @@ oui_player => Embedded players (oui_player) oui_player_provider => Favourite provider oui_player_custom_field => Field used by default +oui_player_responsive => Render responsive players diff --git a/textpacks/fr-fr.textpack b/textpacks/fr-fr.textpack index d856c3a..0dab9b9 100755 --- a/textpacks/fr-fr.textpack +++ b/textpacks/fr-fr.textpack @@ -3,3 +3,4 @@ oui_player => Lecteurs embarqués (oui_player) oui_player_provider => Service préféré oui_player_custom_field => Champ utilisé par défaut +oui_player_responsive => Rendre les lecteurs adaptatifs From 88ef5aa75cc659d80dc37995345bdfb7dcbff063 Mon Sep 17 00:00:00 2001 From: Nicolas Morand Date: Thu, 21 Jun 2018 11:58:25 +0200 Subject: [PATCH 5/9] Gets providers by checking class namespaces --- src/Admin.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Admin.php b/src/Admin.php index 6095fac..c5ef5e4 100644 --- a/src/Admin.php +++ b/src/Admin.php @@ -83,7 +83,20 @@ public function __construct() public static function setProviders() { - static::$providers = callback_event(self::getPlugin(), 'plug_providers', 0, array()); + $providers = array(); + + $namespace = __NAMESPACE__ . "\\"; + + foreach(get_declared_classes() as $name) { + if(strpos($name, $namespace) === 0) { + $providers[] = strtolower(substr($name, strlen($namespace))); + } + } + + static::$providers = array_merge(array_diff( + $providers, + array('admin', 'player', 'main', 'provider') + )); } public function install() From 1e29edb727fe8ca36e4ed251b3ac7308ab7ed260 Mon Sep 17 00:00:00 2001 From: Nicolas Morand Date: Thu, 21 Jun 2018 11:59:20 +0200 Subject: [PATCH 6/9] Registers provider dedicated tags --- src/Public.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Public.php b/src/Public.php index fd252e6..b42c6f2 100644 --- a/src/Public.php +++ b/src/Public.php @@ -256,6 +256,10 @@ public function getPlayer() if (txpinterface === 'public') { foreach (Main::getTags() as $tag => $attributes) { \Txp::get('\Textpattern\Tag\Registry')->register($tag); + + foreach (Main::getProviders() as $provider) { + \Txp::get('\Textpattern\Tag\Registry')->register(str_replace('player', strtolower($provider), $tag)); + } } } } From 29bd45d3ca36c779e323c786ccd49a3d21973a69 Mon Sep 17 00:00:00 2001 From: Nicolas Morand Date: Thu, 21 Jun 2018 12:00:30 +0200 Subject: [PATCH 7/9] Fixes providers 'Options' links --- src/Admin.php | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Admin.php b/src/Admin.php index c5ef5e4..4704b29 100644 --- a/src/Admin.php +++ b/src/Admin.php @@ -75,6 +75,17 @@ public function __construct() null, 1 ); + + foreach (self::getProviders() as $provider) { + add_privs('plugin_prefs.' . $plugin . '_' . $provider, self::getPrivs()); + + register_callback( + 'Oui\Player\Admin::optionsLink', + 'plugin_prefs.' . $plugin . '_' . $provider, + null, + 1 + ); + } } /** @@ -120,7 +131,9 @@ public function uninstall() public static function optionsLink() { - header('Location: ?event=prefs#prefs_group_' . self::getPlugin()); + global $event; + + header('Location: ?event=' . str_replace('plugin_prefs.', 'prefs#prefs_group_', $event)); } /** @@ -369,15 +382,5 @@ private function deleteOldPrefs() if (txpinterface === 'admin' && (in_array($event, array('plugin', 'prefs')) || substr($event, 0, strlen($pluginPrefs)) === $pluginPrefs)) { new Admin; - - $providers = Admin::getProviders(); - - if ($providers) { - foreach ($providers as $provider) { - if (in_array($event, array($pluginPrefs . '_' . lcfirst($provider), 'prefs'))) { - $provider::getInstance(); - } - } - } } } From a78c5c1f96685c6e08cf1418389948e815c9afe9 Mon Sep 17 00:00:00 2001 From: Nicolas Morand Date: Thu, 21 Jun 2018 12:00:57 +0200 Subject: [PATCH 8/9] Displays provider preferences by default --- src/Admin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Admin.php b/src/Admin.php index 4704b29..ea7694d 100644 --- a/src/Admin.php +++ b/src/Admin.php @@ -281,7 +281,7 @@ public static function setAllPrefs() // Adds a pref per provider to display/hide its own prefs group. $options = array( - 'default' => '0', + 'default' => '1', 'valid' => array('0', '1'), ); $options['group'] = $plugin; From 294c6971305d3bd6a5348f3a9ce75678e5922a4e Mon Sep 17 00:00:00 2001 From: Nicolas Morand Date: Thu, 21 Jun 2018 12:03:01 +0200 Subject: [PATCH 9/9] Removes the plugin name in the related preferences group name --- textpacks/en-gb.textpack | 2 +- textpacks/fr-fr.textpack | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/textpacks/en-gb.textpack b/textpacks/en-gb.textpack index 28b3c7c..a677bee 100644 --- a/textpacks/en-gb.textpack +++ b/textpacks/en-gb.textpack @@ -1,6 +1,6 @@ #Default language, en-us, en-gb #@admin -oui_player => Embedded players (oui_player) +oui_player => Embedded players oui_player_provider => Favourite provider oui_player_custom_field => Field used by default oui_player_responsive => Render responsive players diff --git a/textpacks/fr-fr.textpack b/textpacks/fr-fr.textpack index 0dab9b9..3f979e5 100755 --- a/textpacks/fr-fr.textpack +++ b/textpacks/fr-fr.textpack @@ -1,6 +1,6 @@ #@language fr-fr #@admin -oui_player => Lecteurs embarqués (oui_player) +oui_player => Lecteurs embarqués oui_player_provider => Service préféré oui_player_custom_field => Champ utilisé par défaut oui_player_responsive => Rendre les lecteurs adaptatifs