This commit is contained in:
2026-04-26 23:47:49 +02:00
parent 1b95f03d1e
commit b073e009d8
5288 changed files with 1112699 additions and 55536 deletions

View File

@@ -0,0 +1,69 @@
<?php
namespace ElfsightYoutubeGalleryApi;
if (!defined('ABSPATH')) exit;
require_once __DIR__ . '/vendor/autoload.php';
class Api extends Core\Api {
private $routes = array(
'' => 'requestController'
);
const API_BASE_URL = 'https://www.googleapis.com/youtube/v3';
static $API_KEY;
public function __construct($config) {
parent::__construct($config, $this->routes);
}
public function requestController() {
$q = $this->input('q');
$cache_key = $this->Cache->keyFromQuery($q, array('fields', 'callback', '_'));
$data = $this->Cache->get($cache_key);
if (empty($data)) {
$request_url = $this->buildRequestUrl($q);
$response = $this->request('GET', $request_url, array(
'headers' => [
'Referer' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $_SERVER['SCRIPT_URI']
]
));
if ($this->checkResponse($response, false)) {
$data = $response['body'];
$data_arr = json_decode($response['body'], true);
if (!empty($data_arr['error'])) {
$error = $data_arr['error']['errors'][0];
$error_msg = "{$error['message']}: {$error['reason']} ({$error['domain']})";
return $this->error($data_arr['error']['code'], $error_msg, $error);
}
if ((int) $response['http_code'] === 200) {
$this->Cache->set($cache_key, $data);
}
} else {
return $this->error();
}
}
return $this->response($data);
}
public function buildRequestUrl($url) {
self::$API_KEY = $this->input('key') ? $this->input('key') : get_option($this->Helper->getOptionName('api_key'), null);
$url = $this->Helper->removeQueryParam($url, 'key');
$url = $this->Helper->addQueryParam($url, 'key', self::$API_KEY);
return self::API_BASE_URL . urldecode($url);
}
}

View File

@@ -0,0 +1,7 @@
{
"autoload": {
"classmap": [
"vendor/elfsight"
]
}
}

View File

@@ -0,0 +1,5 @@
<?php
// Silence is golden ;)
?>

View File

@@ -0,0 +1,7 @@
<?php
// autoload.php @generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit5fca5cfd667313d8c9e8891fbb853923::getLoader();

View File

@@ -0,0 +1,445 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Autoload;
/**
* ClassLoader implements a PSR-0, PSR-4 and classmap class loader.
*
* $loader = new \Composer\Autoload\ClassLoader();
*
* // register classes with namespaces
* $loader->add('Symfony\Component', __DIR__.'/component');
* $loader->add('Symfony', __DIR__.'/framework');
*
* // activate the autoloader
* $loader->register();
*
* // to enable searching the include path (eg. for PEAR packages)
* $loader->setUseIncludePath(true);
*
* In this example, if you try to use a class in the Symfony\Component
* namespace or one of its children (Symfony\Component\Console for instance),
* the autoloader will first look for the class under the component/
* directory, and it will then fallback to the framework/ directory if not
* found before giving up.
*
* This class is loosely based on the Symfony UniversalClassLoader.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Jordi Boggiano <j.boggiano@seld.be>
* @see http://www.php-fig.org/psr/psr-0/
* @see http://www.php-fig.org/psr/psr-4/
*/
class ClassLoader
{
// PSR-4
private $prefixLengthsPsr4 = array();
private $prefixDirsPsr4 = array();
private $fallbackDirsPsr4 = array();
// PSR-0
private $prefixesPsr0 = array();
private $fallbackDirsPsr0 = array();
private $useIncludePath = false;
private $classMap = array();
private $classMapAuthoritative = false;
private $missingClasses = array();
private $apcuPrefix;
public function getPrefixes()
{
if (!empty($this->prefixesPsr0)) {
return call_user_func_array('array_merge', $this->prefixesPsr0);
}
return array();
}
public function getPrefixesPsr4()
{
return $this->prefixDirsPsr4;
}
public function getFallbackDirs()
{
return $this->fallbackDirsPsr0;
}
public function getFallbackDirsPsr4()
{
return $this->fallbackDirsPsr4;
}
public function getClassMap()
{
return $this->classMap;
}
/**
* @param array $classMap Class to filename map
*/
public function addClassMap(array $classMap)
{
if ($this->classMap) {
$this->classMap = array_merge($this->classMap, $classMap);
} else {
$this->classMap = $classMap;
}
}
/**
* Registers a set of PSR-0 directories for a given prefix, either
* appending or prepending to the ones previously set for this prefix.
*
* @param string $prefix The prefix
* @param array|string $paths The PSR-0 root directories
* @param bool $prepend Whether to prepend the directories
*/
public function add($prefix, $paths, $prepend = false)
{
if (!$prefix) {
if ($prepend) {
$this->fallbackDirsPsr0 = array_merge(
(array) $paths,
$this->fallbackDirsPsr0
);
} else {
$this->fallbackDirsPsr0 = array_merge(
$this->fallbackDirsPsr0,
(array) $paths
);
}
return;
}
$first = $prefix[0];
if (!isset($this->prefixesPsr0[$first][$prefix])) {
$this->prefixesPsr0[$first][$prefix] = (array) $paths;
return;
}
if ($prepend) {
$this->prefixesPsr0[$first][$prefix] = array_merge(
(array) $paths,
$this->prefixesPsr0[$first][$prefix]
);
} else {
$this->prefixesPsr0[$first][$prefix] = array_merge(
$this->prefixesPsr0[$first][$prefix],
(array) $paths
);
}
}
/**
* Registers a set of PSR-4 directories for a given namespace, either
* appending or prepending to the ones previously set for this namespace.
*
* @param string $prefix The prefix/namespace, with trailing '\\'
* @param array|string $paths The PSR-4 base directories
* @param bool $prepend Whether to prepend the directories
*
* @throws \InvalidArgumentException
*/
public function addPsr4($prefix, $paths, $prepend = false)
{
if (!$prefix) {
// Register directories for the root namespace.
if ($prepend) {
$this->fallbackDirsPsr4 = array_merge(
(array) $paths,
$this->fallbackDirsPsr4
);
} else {
$this->fallbackDirsPsr4 = array_merge(
$this->fallbackDirsPsr4,
(array) $paths
);
}
} elseif (!isset($this->prefixDirsPsr4[$prefix])) {
// Register directories for a new namespace.
$length = strlen($prefix);
if ('\\' !== $prefix[$length - 1]) {
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
}
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
$this->prefixDirsPsr4[$prefix] = (array) $paths;
} elseif ($prepend) {
// Prepend directories for an already registered namespace.
$this->prefixDirsPsr4[$prefix] = array_merge(
(array) $paths,
$this->prefixDirsPsr4[$prefix]
);
} else {
// Append directories for an already registered namespace.
$this->prefixDirsPsr4[$prefix] = array_merge(
$this->prefixDirsPsr4[$prefix],
(array) $paths
);
}
}
/**
* Registers a set of PSR-0 directories for a given prefix,
* replacing any others previously set for this prefix.
*
* @param string $prefix The prefix
* @param array|string $paths The PSR-0 base directories
*/
public function set($prefix, $paths)
{
if (!$prefix) {
$this->fallbackDirsPsr0 = (array) $paths;
} else {
$this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
}
}
/**
* Registers a set of PSR-4 directories for a given namespace,
* replacing any others previously set for this namespace.
*
* @param string $prefix The prefix/namespace, with trailing '\\'
* @param array|string $paths The PSR-4 base directories
*
* @throws \InvalidArgumentException
*/
public function setPsr4($prefix, $paths)
{
if (!$prefix) {
$this->fallbackDirsPsr4 = (array) $paths;
} else {
$length = strlen($prefix);
if ('\\' !== $prefix[$length - 1]) {
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
}
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
$this->prefixDirsPsr4[$prefix] = (array) $paths;
}
}
/**
* Turns on searching the include path for class files.
*
* @param bool $useIncludePath
*/
public function setUseIncludePath($useIncludePath)
{
$this->useIncludePath = $useIncludePath;
}
/**
* Can be used to check if the autoloader uses the include path to check
* for classes.
*
* @return bool
*/
public function getUseIncludePath()
{
return $this->useIncludePath;
}
/**
* Turns off searching the prefix and fallback directories for classes
* that have not been registered with the class map.
*
* @param bool $classMapAuthoritative
*/
public function setClassMapAuthoritative($classMapAuthoritative)
{
$this->classMapAuthoritative = $classMapAuthoritative;
}
/**
* Should class lookup fail if not found in the current class map?
*
* @return bool
*/
public function isClassMapAuthoritative()
{
return $this->classMapAuthoritative;
}
/**
* APCu prefix to use to cache found/not-found classes, if the extension is enabled.
*
* @param string|null $apcuPrefix
*/
public function setApcuPrefix($apcuPrefix)
{
$this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null;
}
/**
* The APCu prefix in use, or null if APCu caching is not enabled.
*
* @return string|null
*/
public function getApcuPrefix()
{
return $this->apcuPrefix;
}
/**
* Registers this instance as an autoloader.
*
* @param bool $prepend Whether to prepend the autoloader or not
*/
public function register($prepend = false)
{
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}
/**
* Unregisters this instance as an autoloader.
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
}
/**
* Loads the given class or interface.
*
* @param string $class The name of the class
* @return bool|null True if loaded, null otherwise
*/
public function loadClass($class)
{
if ($file = $this->findFile($class)) {
includeFile($file);
return true;
}
}
/**
* Finds the path to the file where the class is defined.
*
* @param string $class The name of the class
*
* @return string|false The path if found, false otherwise
*/
public function findFile($class)
{
// class map lookup
if (isset($this->classMap[$class])) {
return $this->classMap[$class];
}
if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) {
return false;
}
if (null !== $this->apcuPrefix) {
$file = apcu_fetch($this->apcuPrefix.$class, $hit);
if ($hit) {
return $file;
}
}
$file = $this->findFileWithExtension($class, '.php');
// Search for Hack files if we are running on HHVM
if (false === $file && defined('HHVM_VERSION')) {
$file = $this->findFileWithExtension($class, '.hh');
}
if (null !== $this->apcuPrefix) {
apcu_add($this->apcuPrefix.$class, $file);
}
if (false === $file) {
// Remember that this class does not exist.
$this->missingClasses[$class] = true;
}
return $file;
}
private function findFileWithExtension($class, $ext)
{
// PSR-4 lookup
$logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
$first = $class[0];
if (isset($this->prefixLengthsPsr4[$first])) {
$subPath = $class;
while (false !== $lastPos = strrpos($subPath, '\\')) {
$subPath = substr($subPath, 0, $lastPos);
$search = $subPath . '\\';
if (isset($this->prefixDirsPsr4[$search])) {
$pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
foreach ($this->prefixDirsPsr4[$search] as $dir) {
if (file_exists($file = $dir . $pathEnd)) {
return $file;
}
}
}
}
}
// PSR-4 fallback dirs
foreach ($this->fallbackDirsPsr4 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
return $file;
}
}
// PSR-0 lookup
if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name
$logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
. strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
} else {
// PEAR-like class name
$logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
}
if (isset($this->prefixesPsr0[$first])) {
foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
if (0 === strpos($class, $prefix)) {
foreach ($dirs as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}
}
}
}
// PSR-0 fallback dirs
foreach ($this->fallbackDirsPsr0 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}
// PSR-0 include paths.
if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
return $file;
}
return false;
}
}
/**
* Scope isolated include.
*
* Prevents access to $this/self from included files.
*/
function includeFile($file)
{
include $file;
}

View File

@@ -0,0 +1,21 @@
Copyright (c) Nils Adermann, Jordi Boggiano
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,17 @@
<?php
// autoload_classmap.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'ElfsightYoutubeGalleryApi\\Core\\Api' => $vendorDir . '/elfsight/Api.php',
'ElfsightYoutubeGalleryApi\\Core\\Cache' => $vendorDir . '/elfsight/Cache.php',
'ElfsightYoutubeGalleryApi\\Core\\Debug' => $vendorDir . '/elfsight/Debug.php',
'ElfsightYoutubeGalleryApi\\Core\\Helper' => $vendorDir . '/elfsight/Helper.php',
'ElfsightYoutubeGalleryApi\\Core\\Options' => $vendorDir . '/elfsight/Options.php',
'ElfsightYoutubeGalleryApi\\Core\\Throttle' => $vendorDir . '/elfsight/Throttle.php',
'ElfsightYoutubeGalleryApi\\Core\\Url' => $vendorDir . '/elfsight/Url.php',
'ElfsightYoutubeGalleryApi\\Core\\User' => $vendorDir . '/elfsight/User.php',
);

View File

@@ -0,0 +1,9 @@
<?php
// autoload_namespaces.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
);

View File

@@ -0,0 +1,9 @@
<?php
// autoload_psr4.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
);

View File

@@ -0,0 +1,55 @@
<?php
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit5fca5cfd667313d8c9e8891fbb853923
{
private static $loader;
public static function loadClassLoader($class)
{
if ('Composer\Autoload\ClassLoader' === $class) {
require __DIR__ . '/ClassLoader.php';
}
}
/**
* @return \Composer\Autoload\ClassLoader
*/
public static function getLoader()
{
if (null !== self::$loader) {
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit5fca5cfd667313d8c9e8891fbb853923', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
spl_autoload_unregister(array('ComposerAutoloaderInit5fca5cfd667313d8c9e8891fbb853923', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require_once __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit5fca5cfd667313d8c9e8891fbb853923::getInitializer($loader));
} else {
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->set($namespace, $path);
}
$map = require __DIR__ . '/autoload_psr4.php';
foreach ($map as $namespace => $path) {
$loader->setPsr4($namespace, $path);
}
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
$loader->addClassMap($classMap);
}
}
$loader->register(true);
return $loader;
}
}

View File

@@ -0,0 +1,27 @@
<?php
// autoload_static.php @generated by Composer
namespace Composer\Autoload;
class ComposerStaticInit5fca5cfd667313d8c9e8891fbb853923
{
public static $classMap = array (
'ElfsightYoutubeGalleryApi\\Core\\Api' => __DIR__ . '/..' . '/elfsight/Api.php',
'ElfsightYoutubeGalleryApi\\Core\\Cache' => __DIR__ . '/..' . '/elfsight/Cache.php',
'ElfsightYoutubeGalleryApi\\Core\\Debug' => __DIR__ . '/..' . '/elfsight/Debug.php',
'ElfsightYoutubeGalleryApi\\Core\\Helper' => __DIR__ . '/..' . '/elfsight/Helper.php',
'ElfsightYoutubeGalleryApi\\Core\\Options' => __DIR__ . '/..' . '/elfsight/Options.php',
'ElfsightYoutubeGalleryApi\\Core\\Throttle' => __DIR__ . '/..' . '/elfsight/Throttle.php',
'ElfsightYoutubeGalleryApi\\Core\\Url' => __DIR__ . '/..' . '/elfsight/Url.php',
'ElfsightYoutubeGalleryApi\\Core\\User' => __DIR__ . '/..' . '/elfsight/User.php',
);
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->classMap = ComposerStaticInit5fca5cfd667313d8c9e8891fbb853923::$classMap;
}, null, ClassLoader::class);
}
}

View File

@@ -0,0 +1,385 @@
<?php
namespace ElfsightYoutubeGalleryApi\Core;
if (!defined('PHP_VERSION_ID')) {
$version = explode('.', PHP_VERSION);
define('PHP_VERSION_ID', ($version[0] * 10000 + $version[1] * 100 + $version[2]));
}
abstract class Api {
public $Helper;
public $Cache;
public $Throttle;
public $User;
public $Debug;
public $Url;
public $pluginSlug;
public $pluginFile;
public $debugMode;
public $startTime;
private $proxy;
private $routes;
public static $client;
public static $ERROR_UNKNOWN;
public static $ERROR_INVALID_REQUEST;
public static $ERROR_INVALID_ROUTE;
public static $ERROR_INVALID_AUTH;
public static $ERROR_CURL;
public function __construct($config, $routes) {
self::$ERROR_UNKNOWN = __('Service is unavailable now');
self::$ERROR_INVALID_REQUEST = __('invalid request');
self::$ERROR_INVALID_AUTH = __('Invalid auth');
self::$ERROR_INVALID_ROUTE = __('Requested route not found');
self::$ERROR_CURL = __('The plugin cant make a request. The reason is that cURL PHP Library is not available or requested domain is blocked on your server. To fix this please contact your hosting or server administrator.');
$this->pluginSlug = $config['plugin_slug'];
$this->pluginFile = $config['plugin_file'];
$this->debugMode = isset($config['debug_mode']) ? $config['debug_mode'] : false;
$this->startTime = round(microtime(true) * 1000);
$this->proxy = $this->setProxy($config);
$this->routes = $routes;
// @TODO static Helper
$this->Helper = new Helper($this->pluginSlug);
$this->Cache = new Cache($this->Helper, $config);
$this->Url = new Url();
$this->initOptions($config);
$this->Debug = $this->initDebug($this->debugMode);
if (isset($config['use']) && in_array('throttle', $config['use'])) {
$this->Throttle = new Throttle($this->Helper, $config);
}
if (isset($config['use']) && in_array('user', $config['use'])) {
$this->User = new User($this->Helper, $config);
}
add_action('rest_api_init', array($this, 'registerRoutes'));
add_action('rest_api_init', array($this, 'permalinkRestRouteFix'));
// add_action('rest_api_init', function() {
// header('Access-Control-Allow-Origin: *');
// });
}
public function permalinkRestRouteFix() {
global $wp;
if (isset($wp->query_vars['rest_route']) && strpos($wp->query_vars['rest_route'], $this->pluginSlug . '/api') !== false) {
$split = explode('?q=', $wp->query_vars['rest_route']);
if (count($split) === 2) {
$wp->query_vars['rest_route'] = $split[0];
$_REQUEST['q'] = $split[1];
}
}
}
public function registerRoutes() {
register_rest_route($this->pluginSlug, '/api/', array(
'methods' => 'GET, POST',
'callback' => array($this, 'run'),
'permission_callback' => '__return_true'
));
register_rest_route($this->pluginSlug, '/api/(?P<endpoint>[\w-]+)', array(
'methods' => 'GET, POST',
'callback' => array($this, 'run'),
'permission_callback' => '__return_true',
'args' => array(
'endpoint' => array(
'required' => false,
'default' => '',
'enum' => array_keys($this->routes)
)
)
));
}
public function initDebug($debug_mode) {
if (class_exists('\ElfsightYoutubeGalleryApi\Debug')) {
return new \ElfsightYoutubeGalleryApi\Debug($this, $debug_mode);
} else {
return new \ElfsightYoutubeGalleryApi\Core\Debug($this, $debug_mode);
}
}
public function initOptions($config) {
if (class_exists('\ElfsightYoutubeGalleryApi\Options')) {
return new \ElfsightYoutubeGalleryApi\Options($this->Helper, $config);
} else {
return new \ElfsightYoutubeGalleryApi\Core\Options($this->Helper, $config);
}
}
public function run(\WP_REST_Request $request) {
$endpoint = $request->get_param('endpoint');
$route = isset($this->routes[$endpoint]) ? $this->routes[$endpoint] : null;
if (empty($route) || !method_exists($this, $route)) {
$this->error(400, self::$ERROR_INVALID_REQUEST, self::$ERROR_INVALID_ROUTE);
}
return call_user_func(array($this, $route));
}
public function request($type, $url, $options = array()) {
$type = strtoupper($type);
$curl = curl_init();
$request_url = $url;
if (!empty($options['query'])) {
$request_url .= '?' . http_build_query($options['query']);
}
$curl_options = array(
CURLOPT_URL => $request_url,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CONNECTTIMEOUT => 60,
CURLOPT_TIMEOUT => 60,
CURLOPT_FOLLOWLOCATION => !empty($options['follow']) && $options['follow'],
CURLOPT_HTTPHEADER => $this->getHeadersList($options),
CURLOPT_PROXY => $this->proxy['url'],
CURLOPT_PROXYUSERPWD => $this->proxy['credentials']
);
curl_setopt_array($curl, $curl_options);
$response = curl_exec($curl);
$info = curl_getinfo($curl);
$error = curl_error($curl);
curl_close($curl);
if (isset($options['debug']) && $options['debug']) {
return [$response, $info, $error];
}
if ($info['http_code'] === 0) {
$this->error(400, self::$ERROR_CURL, $error);
}
return $this->formatResponse($response);
}
private function getHeadersList($options = array()) {
$headers_raw_list = array();
$cookies_raw_list = array();
$cookies = !empty(self::$client['cookies']) ? self::$client['cookies'] : array();
$headers = !empty(self::$client['headers']) ? self::$client['headers'] : array();
if (!empty($options['cookies'])) {
$cookies = $this->Helper->arrayMergeAssoc($cookies, $options['cookies']);
}
if (isset($options['headers'])) {
$headers = $this->Helper->arrayMergeAssoc($headers, $options['headers']);
}
foreach ($cookies as $cookie_name => $cookie_value) {
$cookies_raw_list[] = $cookie_name . '=' . $cookie_value;
}
unset($cookie_name, $cookie_data);
$headers['Cookie'] = implode('; ', $cookies_raw_list);
foreach ($headers as $header_key => $header_value) {
$headers_raw_list[] = $header_key . ': ' . $header_value;
}
unset($header_key, $header_value);
return $headers_raw_list;
}
public function formatResponse($response) {
@list ($response_headers_str, $response_body_encoded, $alt_body_encoded) = explode("\r\n\r\n", $response);
if ($alt_body_encoded) {
$response_headers_str = $response_body_encoded;
$response_body_encoded = $alt_body_encoded;
}
$response_body = $response_body_encoded;
$response_headers_raw_list = explode("\r\n", $response_headers_str);
$response_http = array_shift($response_headers_raw_list);
preg_match('#^([^\s]+)\s(\d+)\s?([^$]+)?$#', $response_http, $response_http_matches);
array_shift($response_http_matches);
list ($response_http_protocol, $response_http_code) = $response_http_matches;
$response_http_message = '';
if (isset($response_http_matches[2])) {
$response_http_message = $response_http_matches[2];
}
$response_headers = array();
$response_cookies = array();
foreach ($response_headers_raw_list as $header_row) {
list ($header_key, $header_value) = explode(': ', $header_row, 2);
if (strtolower($header_key) === 'set-cookie') {
$cookie_params = explode('; ', $header_value);
if (empty($cookie_params[0])) {
continue;
}
list ($cookie_name, $cookie_value) = explode('=', $cookie_params[0]);
$response_cookies[$cookie_name] = $cookie_value;
} else {
$response_headers[$header_key] = $header_value;
}
}
unset($header_row, $header_key, $header_value, $cookie_name, $cookie_value);
if ($response_cookies) {
self::$client['cookies'] = $this->Helper->arrayMergeAssoc(self::$client['cookies'] ?: array(), $response_cookies);
}
return array(
'status' => 1,
'http_protocol' => $response_http_protocol,
'http_code' => (int) $response_http_code,
'http_message' => $response_http_message,
'headers' => $response_headers,
'cookies' => $response_cookies,
'body' => $response_body
);
}
public function response($data, $options = array()) {
if (ob_get_length()) {
ob_end_clean();
ob_start();
}
$default_options = array(
'encode' => false,
'plain' => false
);
$options = !empty($options) ? array_merge($default_options, $options) : $default_options;
$callback = $this->input('callback', null, false);
$output = $options['encode'] ? json_encode($data) : $data;
$content_type = $options['plain'] ? 'text/html' : 'application/json';
if (!empty($callback)) {
$callback = htmlspecialchars(strip_tags($callback));
$validate_callback = preg_match('#^jQuery[0-9]*\_[0-9]*$#', $callback);
if ($validate_callback) {
$output = '/**/ ' . $callback . '(' . $output . ')';
$content_type = 'application/javascript';
}
}
header('Content-type: ' . $content_type . '; charset=utf-8');
exit($output);
}
public function error($code = 400, $error_message = null, $additional = '') {
if (!$error_message) {
$error_message = self::$ERROR_UNKNOWN;
}
$error = array(
'meta' => array(
'code' => $code,
'error_message' => $error_message
)
);
if ($additional) {
$additional && $error['meta']['_additional'] = $additional;
}
$this->response($error, array('encode' => true));
}
public function subError($additional) {
return $this->error(400, self::$ERROR_UNKNOWN, $additional);
}
public function input($name, $default = null, $check_empty = true) {
$query = array();
if (empty($_REQUEST)) {
$parsed_url = parse_url($_SERVER['REQUEST_URI']);
if (isset($parsed_url['query'])) {
parse_str($parsed_url['query'], $query);
}
} else {
$query = $_REQUEST;
}
$value = isset($query[$name]) ? $query[$name] : $default;
if (empty($value) && $check_empty) {
$this->error(400, self::$ERROR_INVALID_REQUEST, $name . ' is not defined');
}
return is_string($value) ? urldecode($value) : $value;
}
private function setProxy($config) {
$url = null;
$credentials = null;
if (isset($config['proxy'])) {
$proxy_config = $config['proxy'];
if (isset($proxy_config['proxy']) && !empty($proxy_config['proxy'])) {
if (!empty($proxy_config['proxy']['server'])) {
$url = $proxy_config['proxy']['server'];
}
if (!empty($proxy_config['proxy']['user']) && !empty($proxy_config['proxy']['password'])) {
$credentials = $proxy_config['proxy']['user'] . ':' . $proxy_config['proxy']['password'];
}
}
}
return array(
'url' => $url,
'credentials' => $credentials
);
}
public function checkResponse($response, $checkCode = true) {
$hasResponse = !empty($response) && !empty($response['body']) && !empty($response['http_code']);
if (!$hasResponse) {
return false;
}
if ($checkCode && (int) $response['http_code'] !== 200) {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,92 @@
<?php
namespace ElfsightYoutubeGalleryApi\Core;
class Cache {
private $Helper;
private $pluginFile;
private $cacheTime;
private $tableName;
public function __construct($Helper, $config) {
$this->Helper = $Helper;
$this->pluginFile = $config['plugin_file'];
$this->cacheTime = !empty($config['cache_time']) ? $config['cache_time'] : 43200;
$clear_cache = isset($config['clear_cache_on_update']) ? $config['clear_cache_on_update'] : true;
$this->tableName = $this->Helper->getTableName('cache');
if (!$this->Helper->tableExist($this->tableName)) {
$this->Helper->tableCreate($this->tableName, array('key' => 'text', 'data' => 'longtext'));
}
if ($clear_cache) {
register_deactivation_hook($this->pluginFile, array($this, 'dropTable'));
}
}
public function keyFromQuery($query, $excluded_params = array('access_token')) {
$key = $query;
foreach ($excluded_params as $param) {
$key = $this->Helper->removeQueryParam($key, $param);
}
return preg_replace('#\?$#', '', $key);
}
public function get($key, $check_expire = true) {
$cache = $this->Helper->tableRowGet($this->tableName, array('key' => $key));
if (!$cache || ($check_expire && time() > $cache['updated_at'] + $this->cacheTime)) {
return null;
}
return $cache['data'];
}
public function expired($key) {
$cache = $this->Helper->tableRowGet($this->tableName, array('key' => $key));
return !$cache || $cache && time() > $cache['updated_at'] + $this->cacheTime;
}
public function set($key, $data, $merge = false) {
if (empty($data)) {
return false;
}
$data = $merge ? $this->merge($data, $this->get($key, false)) : $data;
$data = is_array($data) ? json_encode($data) : $data;
return !!$this->Helper->tableRowUpdate(
$this->tableName,
array(
'key' => $key,
'data' => $data
),
array('key' => $key)
);
}
private function merge($data, $cache_data_json){
if (empty($cache_data_json) || empty($data)) {
return $data;
} else {
$cache_data = json_decode($cache_data_json, true);
$unique_data = $this->Helper->uniqueSort(array_merge_recursive($data, $cache_data), 'created_time');
return $unique_data; // @TODO slice by limit
}
}
public function dropTable() {
global $wpdb;
return $wpdb->query("DROP TABLE IF EXISTS $this->tableName");
}
}

View File

@@ -0,0 +1,104 @@
<?php
namespace ElfsightYoutubeGalleryApi\Core;
class Debug {
public $Api;
public $Helper;
public $debugMode;
static $pass = '8JwSgpRpB4cDhY2q';
public function __construct($Api, $debug_mode = false) {
$this->Api = $Api;
$this->Helper = $Api->Helper;
$this->debugMode = $debug_mode;
add_action('rest_api_init', array($this, 'registerRoutes'));
}
public function registerRoutes() {
register_rest_route($this->Api->pluginSlug, '/api/debug/(?P<endpoint>[\w-]+)', array(
'methods' => 'GET',
'callback' => array($this, 'run'),
'permission_callback' => '__return_true',
'args' => array(
'endpoint' => array(
'required' => true
)
)
));
}
public function run(\WP_REST_Request $request) {
$params = $request->get_params();
$endpoint = $params['endpoint'];
if (empty($endpoint) || !method_exists($this, $endpoint)) {
$this->Api->error(400, 'invalid request', 'requested route not found');
}
return call_user_func(array($this, $endpoint), $params);
}
private function restrict($params) {
if (!isset($params['pass']) || $params['pass'] !== self::$pass) {
$this->Api->error(400, 'restricted');
}
}
public function request($params) {
$test_url = isset($params['test_url']) ? $params['test_url'] : 'https://www.google.com';
$test_request = $this->Api->request('get', $test_url, array('debug' => true));
list($curl_response, $curl_info, $curl_error) = $test_request;
$data = array(
'info' => $curl_info,
'error' => $curl_error
);
if (isset($params['with_response']) && $params['with_response'] === 'true') {
$data['response'] = $curl_response;
}
$this->Api->response(array(
'status' => $curl_info['http_code'],
'test_url' => $test_url,
'request_data' => $data
), array('encode' => true));
}
public function php($params) {
$this->restrict($params);
$avail_what = array(4,8);
$what = isset($params['what']) && in_array($params['what'], $avail_what) ? $params['what'] : 4;
header('Content-type: text/html; charset=UTF-8');
phpinfo($what);
exit();
}
public function dump($data, $die = false)
{
$this->dd($data, $die);
}
public function dd($data, $die = true)
{
if (!$this->debugMode) {
return;
}
header('Content-type: text/html; charset=UTF-8');
echo '<pre>';
print_r($data);
echo '</pre>';
$die && die();
}
}

View File

@@ -0,0 +1,163 @@
<?php
namespace ElfsightYoutubeGalleryApi\Core;
class Helper {
public $pluginSlug;
public function __construct($pluginSlug) {
$this->pluginSlug = $pluginSlug;
}
public function getOptionName($name, $delimiter = '_') {
return str_replace('-', $delimiter, $this->pluginSlug) . ($name ? $delimiter . $name : '');
}
public function getPluginSlug() {
return $this->pluginSlug;
}
public function getTableName($name) {
global $wpdb;
return esc_sql($wpdb->prefix . $this->getOptionName($name));
}
public function tableExist($table_name) {
global $wpdb;
return !!$wpdb->get_var($wpdb->prepare(
"SHOW TABLES LIKE %s",
$table_name
));
}
public function tableCreate($table_name, $columns) {
global $wpdb;
$columns['updated_at'] = 'int(12)';
$columns_query = '';
foreach ($columns as $key => $type) {
$columns_query .= '`' . $key . '` ' . $type . ' NOT NULL,';
}
return $wpdb->query(
"CREATE TABLE $table_name (`id` int(11) unsigned NOT NULL AUTO_INCREMENT, $columns_query PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;"
);
}
public function tableRowGet($table_name, $where) {
global $wpdb;
list ($key, $value) = $this->getKeyValue($where);
return $wpdb->get_row($wpdb->prepare(
"SELECT * FROM $table_name WHERE `$key` = %s",
esc_sql($value)
), ARRAY_A);
}
public function tableRowExist($table_name, $where) {
global $wpdb;
list ($key, $value) = $this->getKeyValue($where);
return !!$wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM $table_name WHERE `$key` = %s",
esc_sql($value)
));
}
public function getKeyValue($array) {
$keys = array_keys($array);
$values = array_values($array);
return array($keys[0], $values[0]);
}
public function tableRowUpdate($table_name, $data, $where) {
global $wpdb;
$data['updated_at'] = time();
if ($this->tableRowExist($table_name, $where)) {
$status = $wpdb->update(
$table_name,
$data,
$where
);
} else {
$status = $wpdb->insert(
$table_name,
$data
);
}
return !!$status;
}
public function addQueryParam($url, $key, $val) {
return $url . (strpos($url,'?') !== false ? '&' : '?') . $key . '=' . $val;
}
public function removeQueryParam($url, $key) {
$result = $url;
$url_data = parse_url($url);
if (!empty($url_data['query'])) {
parse_str($url_data['query'], $query_params);
if (!empty($query_params[$key])) {
unset($query_params[$key]);
}
$result = $url_data['path'] . '?' . http_build_query($query_params);
}
return $result;
}
public function uniqueSort($array, $key) {
$result = array();
$array_unique = array();
$ids = array();
$timestamps = array();
foreach ($array as $item) {
if (!in_array($item['id'], $ids)) {
$ids_unique[] = $item['id'];
$array_unique[$item['id']] = $item;
$timestamps[$item['id']] = $item[$key];
}
}
arsort($timestamps);
foreach ($timestamps as $id => $node) {
$result[] = $array_unique[$id];
}
return $result;
}
public function arrayMergeAssoc() {
$mixed = null;
$arrays = func_get_args();
foreach ($arrays as $k => $arr) {
if ($k === 0) {
$mixed = $arr;
continue;
}
$mixed = array_combine(
array_merge(array_keys($mixed), array_keys($arr)),
array_merge(array_values($mixed), array_values($arr))
);
}
return $mixed;
}
}

View File

@@ -0,0 +1,113 @@
<?php
namespace ElfsightYoutubeGalleryApi\Core;
class Options {
public $Helper;
public $apiUrl;
public $editorSettings;
public function __construct($Helper) {
$this->Helper = $Helper;
add_filter($this->Helper->getOptionName('shortcode_options'), array($this, 'shortcodeOptionsFilter'));
add_filter($this->Helper->getOptionName('widget_options'), array($this, 'widgetOptionsFilter'));
add_filter($this->Helper->getOptionName('editor_settings'), array($this, 'editorSettingsFilter'));
}
public function editorSettingsFilter($config) {
$this->editorSettings = $config;
$this->apiUrl = rest_url($this->Helper->getPluginSlug() . '/api');
$this->addOptions();
$this->modifyOptions();
$this->deleteOptions();
return $this->editorSettings;
}
public function addOptions() {
$this->addOption(array(
'id' => 'apiUrl',
'type' => 'hidden',
'defaultValue' => $this->apiUrl
));
}
public function modifyOptions() {
}
public function deleteOptions() {
}
public function addOption($data) {
if (!is_array($this->editorSettings)) {
return;
}
array_push($this->editorSettings['properties'], $data);
}
public function modifyOption($id, $data, &$properties = null) {
if (!isset($properties)) {
$properties = &$this->editorSettings['properties'];
}
if (!is_array($properties)) {
return;
}
foreach ($properties as &$property) {
if (!empty($property['id']) && $property['id'] === $id) {
$property = array_merge($property, $data);
}
if ($property['type'] === 'subgroup') {
$this->modifyOption($id, $data, $property['subgroup']['properties']);
}
}
}
public function deleteOption($id, &$properties = null) {
if (!isset($properties)) {
$properties = &$this->editorSettings['properties'];
}
foreach ($properties as $i => &$property) {
if ($property['type'] === 'subgroup') {
$this->modifyOption($id, $property['subgroup']['properties']);
}
if (!empty($property['id']) && $property['id'] === $id) {
array_splice($properties, $i, 1);
return;
}
}
}
public function shortcodeOptionsFilter($options) {
$this->apiUrl = rest_url($this->Helper->getPluginSlug() . '/api');
if (is_array($options)) {
$options['apiUrl'] = $this->apiUrl;
}
return $options;
}
public function widgetOptionsFilter($options_json) {
$options = json_decode($options_json, true);
if (is_array($options)) {
unset($options['api']);
unset($options['apiUrl']);
}
return json_encode($options);
}
}

View File

@@ -0,0 +1,67 @@
<?php
namespace ElfsightYoutubeGalleryApi\Core;
class Throttle {
private $Helper;
private $pluginFile;
private $tableName;
private $calls;
private $time;
public function __construct($Helper, $config) {
$this->Helper = $Helper;
$this->pluginFile = $config['plugin_file'];
$this->calls = $config['throttle']['calls'];
$this->time = $config['throttle']['time'];
$this->tableName = $this->Helper->getTableName('throttle');
if (!$this->Helper->tableExist($this->tableName)) {
$this->Helper->tableCreate($this->tableName, array('calls' => 'int(2)'));
}
register_deactivation_hook($this->pluginFile, array($this, 'dropTable'));
}
public function isLimited() {
$usage = $this->Helper->tableRowGet($this->tableName, array('id' => 1));
if ($usage && $usage['calls'] > $this->calls) {
if (time() < $usage['updated_at'] + $this->time) {
return true;
} else {
return false;
}
} else {
return false;
}
}
public function increment() {
$usage = $this->Helper->tableRowGet($this->tableName, array('id' => 1));
$calls_cnt = $usage && $usage['calls'] ? $usage['calls'] + 1 : 1;
if ($usage && $usage['calls'] > $this->calls && time() > $usage['updated_at'] + $this->time) {
$calls_cnt = 0;
}
return !!$this->Helper->tableRowUpdate(
$this->tableName,
array('calls' => $calls_cnt),
array('id' => 1)
);
}
public function dropTable() {
global $wpdb;
return $wpdb->query("DROP TABLE IF EXISTS $this->tableName");
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace ElfsightYoutubeGalleryApi\Core;
class Url {
public static function buildUrl($path, $params = [])
{
return $path . (!empty($params) ? '?' . http_build_query($params) : '');
}
public static function addQueryParams($subject, $params = [])
{
list($url, $query_params) = self::parseUrl($subject);
foreach ($params as $key => $value) {
$query_params[$key] = $value;
}
return self::buildUrl($url, $query_params);
}
public static function parseUrl($subject)
{
$url_data = parse_url($subject);
$query_params = [];
if (!empty($url_data['query'])) {
parse_str($url_data['query'], $query_params);
}
$url = "{$url_data['scheme']}://{$url_data['host']}{$url_data['path']}";
return [$url, $query_params];
}
public function setRequestUrl($url, $baseUrl = '', $params = array()){
$requestUrl = $url;
if (stripos($requestUrl, $baseUrl) === false) {
$requestUrl = $baseUrl . preg_replace('/^\/+/', '', $requestUrl);
}
return self::addQueryParams($requestUrl, $params);
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace ElfsightYoutubeGalleryApi\Core;
class User {
private $Helper;
private $pluginFile;
private $tableName;
public function __construct($Helper, $config) {
$this->Helper = $Helper;
$this->pluginFile = $config['plugin_file'];
$this->tableName = $this->Helper->getTableName('user');
if (!$this->Helper->tableExist($this->tableName)) {
$this->Helper->tableCreate($this->tableName, array('public_id' => 'varchar(255)', 'data' => 'text'));
}
}
public function get($public_id) {
return $this->Helper->tableRowGet($this->tableName, array('public_id' => $public_id));
}
public function set($public_id, $data) {
return !!$this->Helper->tableRowUpdate(
$this->tableName,
array(
'public_id' => $public_id,
'data' => $data
),
array('public_id' => $public_id)
);
}
}

View File

@@ -0,0 +1,5 @@
<?php
// Silence is golden ;)
?>