@@ -4,52 +4,52 @@ |
||
| 4 | 4 | |
| 5 | 5 | class ComposerAutoloaderInit30c84c47a44576eaa4b38cc478d5e11b { |
| 6 | 6 | |
| 7 | - private static $loader; |
|
| 8 | - |
|
| 9 | - public static function loadClassLoader( $class ) { |
|
| 10 | - if ( 'Composer\Autoload\ClassLoader' === $class ) { |
|
| 11 | - require __DIR__ . '/ClassLoader.php'; |
|
| 12 | - } |
|
| 13 | - } |
|
| 14 | - |
|
| 15 | - /** |
|
| 16 | - * @return \Composer\Autoload\ClassLoader |
|
| 17 | - */ |
|
| 18 | - public static function getLoader() { |
|
| 19 | - if ( null !== self::$loader ) { |
|
| 20 | - return self::$loader; |
|
| 21 | - } |
|
| 22 | - |
|
| 23 | - require __DIR__ . '/platform_check.php'; |
|
| 24 | - |
|
| 25 | - spl_autoload_register( array( 'ComposerAutoloaderInit30c84c47a44576eaa4b38cc478d5e11b', 'loadClassLoader' ), true, true ); |
|
| 26 | - self::$loader = $loader = new \Composer\Autoload\ClassLoader(); |
|
| 27 | - spl_autoload_unregister( array( 'ComposerAutoloaderInit30c84c47a44576eaa4b38cc478d5e11b', 'loadClassLoader' ) ); |
|
| 28 | - |
|
| 29 | - $useStaticLoader = PHP_VERSION_ID >= 50600 && ! defined( 'HHVM_VERSION' ) && ( ! function_exists( 'zend_loader_file_encoded' ) || ! zend_loader_file_encoded() ); |
|
| 30 | - if ( $useStaticLoader ) { |
|
| 31 | - require __DIR__ . '/autoload_static.php'; |
|
| 32 | - |
|
| 33 | - call_user_func( \Composer\Autoload\ComposerStaticInit30c84c47a44576eaa4b38cc478d5e11b::getInitializer( $loader ) ); |
|
| 34 | - } else { |
|
| 35 | - $map = require __DIR__ . '/autoload_namespaces.php'; |
|
| 36 | - foreach ( $map as $namespace => $path ) { |
|
| 37 | - $loader->set( $namespace, $path ); |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - $map = require __DIR__ . '/autoload_psr4.php'; |
|
| 41 | - foreach ( $map as $namespace => $path ) { |
|
| 42 | - $loader->setPsr4( $namespace, $path ); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - $classMap = require __DIR__ . '/autoload_classmap.php'; |
|
| 46 | - if ( $classMap ) { |
|
| 47 | - $loader->addClassMap( $classMap ); |
|
| 48 | - } |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - $loader->register( true ); |
|
| 52 | - |
|
| 53 | - return $loader; |
|
| 54 | - } |
|
| 7 | + private static $loader; |
|
| 8 | + |
|
| 9 | + public static function loadClassLoader( $class ) { |
|
| 10 | + if ( 'Composer\Autoload\ClassLoader' === $class ) { |
|
| 11 | + require __DIR__ . '/ClassLoader.php'; |
|
| 12 | + } |
|
| 13 | + } |
|
| 14 | + |
|
| 15 | + /** |
|
| 16 | + * @return \Composer\Autoload\ClassLoader |
|
| 17 | + */ |
|
| 18 | + public static function getLoader() { |
|
| 19 | + if ( null !== self::$loader ) { |
|
| 20 | + return self::$loader; |
|
| 21 | + } |
|
| 22 | + |
|
| 23 | + require __DIR__ . '/platform_check.php'; |
|
| 24 | + |
|
| 25 | + spl_autoload_register( array( 'ComposerAutoloaderInit30c84c47a44576eaa4b38cc478d5e11b', 'loadClassLoader' ), true, true ); |
|
| 26 | + self::$loader = $loader = new \Composer\Autoload\ClassLoader(); |
|
| 27 | + spl_autoload_unregister( array( 'ComposerAutoloaderInit30c84c47a44576eaa4b38cc478d5e11b', 'loadClassLoader' ) ); |
|
| 28 | + |
|
| 29 | + $useStaticLoader = PHP_VERSION_ID >= 50600 && ! defined( 'HHVM_VERSION' ) && ( ! function_exists( 'zend_loader_file_encoded' ) || ! zend_loader_file_encoded() ); |
|
| 30 | + if ( $useStaticLoader ) { |
|
| 31 | + require __DIR__ . '/autoload_static.php'; |
|
| 32 | + |
|
| 33 | + call_user_func( \Composer\Autoload\ComposerStaticInit30c84c47a44576eaa4b38cc478d5e11b::getInitializer( $loader ) ); |
|
| 34 | + } else { |
|
| 35 | + $map = require __DIR__ . '/autoload_namespaces.php'; |
|
| 36 | + foreach ( $map as $namespace => $path ) { |
|
| 37 | + $loader->set( $namespace, $path ); |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + $map = require __DIR__ . '/autoload_psr4.php'; |
|
| 41 | + foreach ( $map as $namespace => $path ) { |
|
| 42 | + $loader->setPsr4( $namespace, $path ); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + $classMap = require __DIR__ . '/autoload_classmap.php'; |
|
| 46 | + if ( $classMap ) { |
|
| 47 | + $loader->addClassMap( $classMap ); |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + $loader->register( true ); |
|
| 52 | + |
|
| 53 | + return $loader; |
|
| 54 | + } |
|
| 55 | 55 | } |
@@ -6,9 +6,9 @@ discard block |
||
| 6 | 6 | |
| 7 | 7 | private static $loader; |
| 8 | 8 | |
| 9 | - public static function loadClassLoader( $class ) { |
|
| 10 | - if ( 'Composer\Autoload\ClassLoader' === $class ) { |
|
| 11 | - require __DIR__ . '/ClassLoader.php'; |
|
| 9 | + public static function loadClassLoader($class) { |
|
| 10 | + if ('Composer\Autoload\ClassLoader' === $class) { |
|
| 11 | + require __DIR__.'/ClassLoader.php'; |
|
| 12 | 12 | } |
| 13 | 13 | } |
| 14 | 14 | |
@@ -16,39 +16,39 @@ discard block |
||
| 16 | 16 | * @return \Composer\Autoload\ClassLoader |
| 17 | 17 | */ |
| 18 | 18 | public static function getLoader() { |
| 19 | - if ( null !== self::$loader ) { |
|
| 19 | + if (null !== self::$loader) { |
|
| 20 | 20 | return self::$loader; |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | - require __DIR__ . '/platform_check.php'; |
|
| 23 | + require __DIR__.'/platform_check.php'; |
|
| 24 | 24 | |
| 25 | - spl_autoload_register( array( 'ComposerAutoloaderInit30c84c47a44576eaa4b38cc478d5e11b', 'loadClassLoader' ), true, true ); |
|
| 25 | + spl_autoload_register(array('ComposerAutoloaderInit30c84c47a44576eaa4b38cc478d5e11b', 'loadClassLoader'), true, true); |
|
| 26 | 26 | self::$loader = $loader = new \Composer\Autoload\ClassLoader(); |
| 27 | - spl_autoload_unregister( array( 'ComposerAutoloaderInit30c84c47a44576eaa4b38cc478d5e11b', 'loadClassLoader' ) ); |
|
| 27 | + spl_autoload_unregister(array('ComposerAutoloaderInit30c84c47a44576eaa4b38cc478d5e11b', 'loadClassLoader')); |
|
| 28 | 28 | |
| 29 | - $useStaticLoader = PHP_VERSION_ID >= 50600 && ! defined( 'HHVM_VERSION' ) && ( ! function_exists( 'zend_loader_file_encoded' ) || ! zend_loader_file_encoded() ); |
|
| 30 | - if ( $useStaticLoader ) { |
|
| 31 | - require __DIR__ . '/autoload_static.php'; |
|
| 29 | + $useStaticLoader = PHP_VERSION_ID >= 50600 && ! defined('HHVM_VERSION') && ( ! function_exists('zend_loader_file_encoded') || ! zend_loader_file_encoded()); |
|
| 30 | + if ($useStaticLoader) { |
|
| 31 | + require __DIR__.'/autoload_static.php'; |
|
| 32 | 32 | |
| 33 | - call_user_func( \Composer\Autoload\ComposerStaticInit30c84c47a44576eaa4b38cc478d5e11b::getInitializer( $loader ) ); |
|
| 33 | + call_user_func(\Composer\Autoload\ComposerStaticInit30c84c47a44576eaa4b38cc478d5e11b::getInitializer($loader)); |
|
| 34 | 34 | } else { |
| 35 | - $map = require __DIR__ . '/autoload_namespaces.php'; |
|
| 36 | - foreach ( $map as $namespace => $path ) { |
|
| 37 | - $loader->set( $namespace, $path ); |
|
| 35 | + $map = require __DIR__.'/autoload_namespaces.php'; |
|
| 36 | + foreach ($map as $namespace => $path) { |
|
| 37 | + $loader->set($namespace, $path); |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | - $map = require __DIR__ . '/autoload_psr4.php'; |
|
| 41 | - foreach ( $map as $namespace => $path ) { |
|
| 42 | - $loader->setPsr4( $namespace, $path ); |
|
| 40 | + $map = require __DIR__.'/autoload_psr4.php'; |
|
| 41 | + foreach ($map as $namespace => $path) { |
|
| 42 | + $loader->setPsr4($namespace, $path); |
|
| 43 | 43 | } |
| 44 | 44 | |
| 45 | - $classMap = require __DIR__ . '/autoload_classmap.php'; |
|
| 46 | - if ( $classMap ) { |
|
| 47 | - $loader->addClassMap( $classMap ); |
|
| 45 | + $classMap = require __DIR__.'/autoload_classmap.php'; |
|
| 46 | + if ($classMap) { |
|
| 47 | + $loader->addClassMap($classMap); |
|
| 48 | 48 | } |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | - $loader->register( true ); |
|
| 51 | + $loader->register(true); |
|
| 52 | 52 | |
| 53 | 53 | return $loader; |
| 54 | 54 | } |
@@ -10,8 +10,8 @@ discard block |
||
| 10 | 10 | */ |
| 11 | 11 | function wl_set_entity_main_type( $post_id, $type_uri ) { |
| 12 | 12 | |
| 13 | - Wordlift_Entity_Type_Service::get_instance() |
|
| 14 | - ->set( $post_id, $type_uri ); |
|
| 13 | + Wordlift_Entity_Type_Service::get_instance() |
|
| 14 | + ->set( $post_id, $type_uri ); |
|
| 15 | 15 | |
| 16 | 16 | } |
| 17 | 17 | |
@@ -20,39 +20,39 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | function wl_print_entity_type_inline_js() { |
| 22 | 22 | |
| 23 | - $terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'get' => 'all' ) ); |
|
| 24 | - |
|
| 25 | - // Load the type data. |
|
| 26 | - $schema_service = Wordlift_Schema_Service::get_instance(); |
|
| 27 | - $entity_types = array_reduce( |
|
| 28 | - $terms, |
|
| 29 | - function ( $carry, $term ) use ( $schema_service ) { |
|
| 30 | - $type = $schema_service->get_schema( $term->slug ); |
|
| 31 | - |
|
| 32 | - // Skip if no `uri`. |
|
| 33 | - if ( empty( $type['uri'] ) ) { |
|
| 34 | - return $carry; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - $carry[] = array( |
|
| 38 | - 'label' => $term->name, |
|
| 39 | - 'uri' => $type['uri'], |
|
| 40 | - 'css' => $type['css_class'], |
|
| 41 | - 'sameAs' => isset( $type['same_as'] ) ? $type['same_as'] : array(), |
|
| 42 | - 'slug' => $term->slug, |
|
| 43 | - 'templates' => ( isset( $type['templates'] ) ? $type['templates'] : array() ), |
|
| 44 | - ); |
|
| 45 | - |
|
| 46 | - return $carry; |
|
| 47 | - }, |
|
| 48 | - array() |
|
| 49 | - ); |
|
| 50 | - |
|
| 51 | - // Hook to the Block Editor script. |
|
| 52 | - wp_localize_script( 'wl-block-editor', '_wlEntityTypes', $entity_types ); |
|
| 53 | - |
|
| 54 | - // Hook to the Classic Editor script, see Wordlift_Admin_Post_Edit_Page. |
|
| 55 | - wp_localize_script( 'wl-classic-editor', '_wlEntityTypes', $entity_types ); |
|
| 23 | + $terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'get' => 'all' ) ); |
|
| 24 | + |
|
| 25 | + // Load the type data. |
|
| 26 | + $schema_service = Wordlift_Schema_Service::get_instance(); |
|
| 27 | + $entity_types = array_reduce( |
|
| 28 | + $terms, |
|
| 29 | + function ( $carry, $term ) use ( $schema_service ) { |
|
| 30 | + $type = $schema_service->get_schema( $term->slug ); |
|
| 31 | + |
|
| 32 | + // Skip if no `uri`. |
|
| 33 | + if ( empty( $type['uri'] ) ) { |
|
| 34 | + return $carry; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + $carry[] = array( |
|
| 38 | + 'label' => $term->name, |
|
| 39 | + 'uri' => $type['uri'], |
|
| 40 | + 'css' => $type['css_class'], |
|
| 41 | + 'sameAs' => isset( $type['same_as'] ) ? $type['same_as'] : array(), |
|
| 42 | + 'slug' => $term->slug, |
|
| 43 | + 'templates' => ( isset( $type['templates'] ) ? $type['templates'] : array() ), |
|
| 44 | + ); |
|
| 45 | + |
|
| 46 | + return $carry; |
|
| 47 | + }, |
|
| 48 | + array() |
|
| 49 | + ); |
|
| 50 | + |
|
| 51 | + // Hook to the Block Editor script. |
|
| 52 | + wp_localize_script( 'wl-block-editor', '_wlEntityTypes', $entity_types ); |
|
| 53 | + |
|
| 54 | + // Hook to the Classic Editor script, see Wordlift_Admin_Post_Edit_Page. |
|
| 55 | + wp_localize_script( 'wl-classic-editor', '_wlEntityTypes', $entity_types ); |
|
| 56 | 56 | |
| 57 | 57 | } |
| 58 | 58 | |
@@ -8,10 +8,10 @@ discard block |
||
| 8 | 8 | * |
| 9 | 9 | * @deprecated use Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ) |
| 10 | 10 | */ |
| 11 | -function wl_set_entity_main_type( $post_id, $type_uri ) { |
|
| 11 | +function wl_set_entity_main_type($post_id, $type_uri) { |
|
| 12 | 12 | |
| 13 | 13 | Wordlift_Entity_Type_Service::get_instance() |
| 14 | - ->set( $post_id, $type_uri ); |
|
| 14 | + ->set($post_id, $type_uri); |
|
| 15 | 15 | |
| 16 | 16 | } |
| 17 | 17 | |
@@ -20,17 +20,17 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | function wl_print_entity_type_inline_js() { |
| 22 | 22 | |
| 23 | - $terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'get' => 'all' ) ); |
|
| 23 | + $terms = get_terms(Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array('get' => 'all')); |
|
| 24 | 24 | |
| 25 | 25 | // Load the type data. |
| 26 | 26 | $schema_service = Wordlift_Schema_Service::get_instance(); |
| 27 | 27 | $entity_types = array_reduce( |
| 28 | 28 | $terms, |
| 29 | - function ( $carry, $term ) use ( $schema_service ) { |
|
| 30 | - $type = $schema_service->get_schema( $term->slug ); |
|
| 29 | + function($carry, $term) use ($schema_service) { |
|
| 30 | + $type = $schema_service->get_schema($term->slug); |
|
| 31 | 31 | |
| 32 | 32 | // Skip if no `uri`. |
| 33 | - if ( empty( $type['uri'] ) ) { |
|
| 33 | + if (empty($type['uri'])) { |
|
| 34 | 34 | return $carry; |
| 35 | 35 | } |
| 36 | 36 | |
@@ -38,9 +38,9 @@ discard block |
||
| 38 | 38 | 'label' => $term->name, |
| 39 | 39 | 'uri' => $type['uri'], |
| 40 | 40 | 'css' => $type['css_class'], |
| 41 | - 'sameAs' => isset( $type['same_as'] ) ? $type['same_as'] : array(), |
|
| 41 | + 'sameAs' => isset($type['same_as']) ? $type['same_as'] : array(), |
|
| 42 | 42 | 'slug' => $term->slug, |
| 43 | - 'templates' => ( isset( $type['templates'] ) ? $type['templates'] : array() ), |
|
| 43 | + 'templates' => (isset($type['templates']) ? $type['templates'] : array()), |
|
| 44 | 44 | ); |
| 45 | 45 | |
| 46 | 46 | return $carry; |
@@ -49,13 +49,13 @@ discard block |
||
| 49 | 49 | ); |
| 50 | 50 | |
| 51 | 51 | // Hook to the Block Editor script. |
| 52 | - wp_localize_script( 'wl-block-editor', '_wlEntityTypes', $entity_types ); |
|
| 52 | + wp_localize_script('wl-block-editor', '_wlEntityTypes', $entity_types); |
|
| 53 | 53 | |
| 54 | 54 | // Hook to the Classic Editor script, see Wordlift_Admin_Post_Edit_Page. |
| 55 | - wp_localize_script( 'wl-classic-editor', '_wlEntityTypes', $entity_types ); |
|
| 55 | + wp_localize_script('wl-classic-editor', '_wlEntityTypes', $entity_types); |
|
| 56 | 56 | |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | // Allow Classic and Block Editor scripts to register first. |
| 60 | -add_action( 'admin_print_scripts-post.php', 'wl_print_entity_type_inline_js', 11 ); |
|
| 61 | -add_action( 'admin_print_scripts-post-new.php', 'wl_print_entity_type_inline_js', 11 ); |
|
| 60 | +add_action('admin_print_scripts-post.php', 'wl_print_entity_type_inline_js', 11); |
|
| 61 | +add_action('admin_print_scripts-post-new.php', 'wl_print_entity_type_inline_js', 11); |
|
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | - exit; |
|
| 14 | + exit; |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | /** |
@@ -21,520 +21,520 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_Countries { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * An array that will contain country codes => country names pairs. It gets lazily loaded the first time by the |
|
| 26 | - * `get_countries` function. |
|
| 27 | - * |
|
| 28 | - * @since 3.18.0 |
|
| 29 | - * @var array An array of country codes => country names pairs or NULL if not initialized yet. |
|
| 30 | - */ |
|
| 31 | - private static $countries = array(); |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * The list of supported country codes, this is populated by self::lazy_populate_codes_and_country_codes_array. |
|
| 35 | - * |
|
| 36 | - * WARNING! If you change the list of supported countries, *you have* to add the related flag |
|
| 37 | - * in the images/flags folder. |
|
| 38 | - * |
|
| 39 | - * @since 3.18.0 |
|
| 40 | - * |
|
| 41 | - * @var array An array of country codes => supported_languages_array |
|
| 42 | - */ |
|
| 43 | - public static $codes = array(); |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * The list of country codes, this is populated by self::lazy_populate_codes_and_country_codes_array. |
|
| 47 | - * |
|
| 48 | - * WARNING! If you change the list of supported countries, *you have* to add the related flag |
|
| 49 | - * in the images/flags folder. |
|
| 50 | - * |
|
| 51 | - * @since 3.18.0 |
|
| 52 | - * |
|
| 53 | - * @var array An array of country codes => country names. |
|
| 54 | - */ |
|
| 55 | - private static $country_codes = array(); |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * An array of flag filenames. |
|
| 59 | - * |
|
| 60 | - * @since 3.20.0 |
|
| 61 | - * |
|
| 62 | - * @var array An array of flag filenames. |
|
| 63 | - */ |
|
| 64 | - private static $country_flags = array( |
|
| 65 | - 'af' => 'Afghanistan', |
|
| 66 | - 'ax' => 'Aland', |
|
| 67 | - 'al' => 'Albania', |
|
| 68 | - 'dz' => 'Algeria', |
|
| 69 | - 'as' => 'American-Samoa', |
|
| 70 | - 'ad' => 'Andorra', |
|
| 71 | - 'ao' => 'Angola', |
|
| 72 | - 'ai' => 'Anguilla', |
|
| 73 | - 'aq' => 'Antarctica', |
|
| 74 | - 'ag' => 'Antigua-and-Barbuda', |
|
| 75 | - 'ar' => 'Argentina', |
|
| 76 | - 'am' => 'Armenia', |
|
| 77 | - 'aw' => 'Aruba', |
|
| 78 | - 'au' => 'Australia', |
|
| 79 | - 'at' => 'Austria', |
|
| 80 | - 'az' => 'Azerbaijan', |
|
| 81 | - 'bs' => 'Bahamas', |
|
| 82 | - 'bh' => 'Bahrain', |
|
| 83 | - 'bd' => 'Bangladesh', |
|
| 84 | - 'bb' => 'Barbados', |
|
| 85 | - 'by' => 'Belarus', |
|
| 86 | - 'be' => 'Belgium', |
|
| 87 | - 'bz' => 'Belize', |
|
| 88 | - 'bj' => 'Benin', |
|
| 89 | - 'bm' => 'Bermuda', |
|
| 90 | - 'bt' => 'Bhutan', |
|
| 91 | - 'bo' => 'Bolivia', |
|
| 92 | - // Uses Netherlands' flag, see https://en.wikipedia.org/wiki/Caribbean_Netherlands. |
|
| 93 | - 'bq' => 'Netherlands', |
|
| 94 | - 'ba' => 'Bosnia-and-Herzegovina', |
|
| 95 | - 'bw' => 'Botswana', |
|
| 96 | - 'bv' => 'Bouvet Island', |
|
| 97 | - 'br' => 'Brazil', |
|
| 98 | - 'io' => null, |
|
| 99 | - 'bn' => 'Brunei', |
|
| 100 | - 'bg' => 'Bulgaria', |
|
| 101 | - 'bf' => 'Burkina-Faso', |
|
| 102 | - 'bi' => 'Burundi', |
|
| 103 | - 'kh' => 'Cambodia', |
|
| 104 | - 'cm' => 'Cameroon', |
|
| 105 | - 'ca' => 'Canada', |
|
| 106 | - 'cv' => 'Cape-Verde', |
|
| 107 | - 'ky' => 'Cayman-Islands', |
|
| 108 | - 'cf' => 'Central-African-Republic', |
|
| 109 | - 'td' => 'Chad', |
|
| 110 | - 'cl' => 'Chile', |
|
| 111 | - 'cn' => 'China', |
|
| 112 | - 'cx' => 'Christmas-Island', |
|
| 113 | - 'cc' => 'Cocos-Keeling-Islands', |
|
| 114 | - 'co' => 'Colombia', |
|
| 115 | - 'km' => 'Comoros', |
|
| 116 | - 'cg' => 'Republic-of-the-Congo', |
|
| 117 | - 'cd' => 'Democratic-Republic-of-the-Congo', |
|
| 118 | - 'ck' => 'Cook-Islands', |
|
| 119 | - 'cr' => 'Costa-Rica', |
|
| 120 | - 'ci' => 'Cote-dIvoire', |
|
| 121 | - 'hr' => 'Croatia', |
|
| 122 | - 'cu' => 'Cuba', |
|
| 123 | - 'cw' => 'Curacao', |
|
| 124 | - 'cy' => 'Cyprus', |
|
| 125 | - 'cz' => 'Czech-Republic', |
|
| 126 | - 'dk' => 'Denmark', |
|
| 127 | - 'dj' => 'Djibouti', |
|
| 128 | - 'dm' => 'Dominica', |
|
| 129 | - 'do' => 'Dominican-Republic', |
|
| 130 | - 'ec' => 'Ecuador', |
|
| 131 | - 'eg' => 'Egypt', |
|
| 132 | - 'sv' => 'El-Salvador', |
|
| 133 | - 'gq' => 'Equatorial-Guinea', |
|
| 134 | - 'er' => 'Eritrea', |
|
| 135 | - 'ee' => 'Estonia', |
|
| 136 | - 'et' => 'Ethiopia', |
|
| 137 | - 'fk' => 'Falkland-Islands', |
|
| 138 | - 'fo' => 'Faroes', |
|
| 139 | - 'fj' => 'Fiji', |
|
| 140 | - 'fi' => 'Finland', |
|
| 141 | - 'fr' => 'France', |
|
| 142 | - // Uses France's flag, see https://en.wikipedia.org/wiki/French_Guiana. |
|
| 143 | - 'gf' => 'France', |
|
| 144 | - 'pf' => 'French-Polynesia', |
|
| 145 | - 'tf' => 'French-Southern-Territories', |
|
| 146 | - 'ga' => 'Gabon', |
|
| 147 | - 'gm' => 'Gambia', |
|
| 148 | - 'ge' => 'Georgia', |
|
| 149 | - 'de' => 'Germany', |
|
| 150 | - 'gh' => 'Ghana', |
|
| 151 | - 'gi' => 'Gibraltar', |
|
| 152 | - 'gr' => 'Greece', |
|
| 153 | - 'gl' => 'Greenland', |
|
| 154 | - 'gd' => 'Grenada', |
|
| 155 | - // Uses France's flag, see https://en.wikipedia.org/wiki/Guadeloupe. |
|
| 156 | - 'gp' => 'France', |
|
| 157 | - 'gu' => 'Guam', |
|
| 158 | - 'gt' => 'Guatemala', |
|
| 159 | - 'gg' => 'Guernsey', |
|
| 160 | - 'gn' => 'Guinea', |
|
| 161 | - 'gw' => 'Guinea-Bissau', |
|
| 162 | - 'gy' => 'Guyana', |
|
| 163 | - 'ht' => 'Haiti', |
|
| 164 | - // Uses Australia's flag, see https://en.wikipedia.org/wiki/Heard_Island_and_McDonald_Islands. |
|
| 165 | - 'hm' => 'Australia', |
|
| 166 | - 'va' => 'Vatican-City', |
|
| 167 | - 'hn' => 'Honduras', |
|
| 168 | - 'hk' => 'Hong-Kong', |
|
| 169 | - 'hu' => 'Hungary', |
|
| 170 | - 'is' => 'Iceland', |
|
| 171 | - 'in' => 'India', |
|
| 172 | - 'id' => 'Indonesia', |
|
| 173 | - 'ir' => 'Iran', |
|
| 174 | - 'iq' => 'Iraq', |
|
| 175 | - 'ie' => 'Ireland', |
|
| 176 | - 'im' => 'Isle-of-Man', |
|
| 177 | - 'il' => 'Israel', |
|
| 178 | - 'it' => 'Italy', |
|
| 179 | - 'jm' => 'Jamaica', |
|
| 180 | - 'jp' => 'Japan', |
|
| 181 | - 'je' => 'Jersey', |
|
| 182 | - 'jo' => 'Jordan', |
|
| 183 | - 'kz' => 'Kazakhstan', |
|
| 184 | - 'ke' => 'Kenya', |
|
| 185 | - 'ki' => 'Kiribati', |
|
| 186 | - 'kp' => 'North-Korea', |
|
| 187 | - 'kr' => 'South-Korea', |
|
| 188 | - 'kw' => 'Kuwait', |
|
| 189 | - 'kg' => 'Kyrgyzstan', |
|
| 190 | - 'la' => 'Laos', |
|
| 191 | - 'lv' => 'Latvia', |
|
| 192 | - 'lb' => 'Lebanon', |
|
| 193 | - 'ls' => 'Lesotho', |
|
| 194 | - 'lr' => 'Liberia', |
|
| 195 | - 'ly' => 'Libya', |
|
| 196 | - 'li' => 'Liechtenstein', |
|
| 197 | - 'lt' => 'Lithuania', |
|
| 198 | - 'lu' => 'Luxembourg', |
|
| 199 | - 'mo' => 'Macau', |
|
| 200 | - 'mk' => 'Macedonia', |
|
| 201 | - 'mg' => 'Madagascar', |
|
| 202 | - 'mw' => 'Malawi', |
|
| 203 | - 'my' => 'Malaysia', |
|
| 204 | - 'mv' => 'Maldives', |
|
| 205 | - 'ml' => 'Mali', |
|
| 206 | - 'mt' => 'Malta', |
|
| 207 | - 'mh' => 'Marshall-Islands', |
|
| 208 | - 'mq' => 'Martinique', |
|
| 209 | - 'mr' => 'Mauritania', |
|
| 210 | - 'mu' => 'Mauritius', |
|
| 211 | - 'yt' => 'Mayotte', |
|
| 212 | - 'mx' => 'Mexico', |
|
| 213 | - 'fm' => 'Micronesia', |
|
| 214 | - 'md' => 'Moldova', |
|
| 215 | - 'mc' => 'Monaco', |
|
| 216 | - 'mn' => 'Mongolia', |
|
| 217 | - 'me' => 'Montenegro', |
|
| 218 | - 'ms' => 'Montserrat', |
|
| 219 | - 'ma' => 'Morocco', |
|
| 220 | - 'mz' => 'Mozambique', |
|
| 221 | - 'mm' => 'Myanmar', |
|
| 222 | - 'na' => 'Namibia', |
|
| 223 | - 'nr' => 'Nauru', |
|
| 224 | - 'np' => 'Nepal', |
|
| 225 | - 'nl' => 'Netherlands', |
|
| 226 | - 'nc' => 'New-Caledonia', |
|
| 227 | - 'nz' => 'New-Zealand', |
|
| 228 | - 'ni' => 'Nicaragua', |
|
| 229 | - 'ne' => 'Niger', |
|
| 230 | - 'ng' => 'Nigeria', |
|
| 231 | - 'nu' => 'Niue', |
|
| 232 | - 'nf' => 'Norfolk-Island', |
|
| 233 | - 'mp' => 'Northern-Mariana-Islands', |
|
| 234 | - 'no' => 'Norway', |
|
| 235 | - 'om' => 'Oman', |
|
| 236 | - 'pk' => 'Pakistan', |
|
| 237 | - 'pw' => 'Palau', |
|
| 238 | - 'ps' => 'Palestine', |
|
| 239 | - 'pa' => 'Panama', |
|
| 240 | - 'pg' => 'Papua-New-Guinea', |
|
| 241 | - 'py' => 'Paraguay', |
|
| 242 | - 'pe' => 'Peru', |
|
| 243 | - 'ph' => 'Philippines', |
|
| 244 | - 'pn' => 'Pitcairn-Islands', |
|
| 245 | - 'pl' => 'Poland', |
|
| 246 | - 'pt' => 'Portugal', |
|
| 247 | - 'pr' => 'Puerto Rico', |
|
| 248 | - 'qa' => 'Qatar', |
|
| 249 | - // Uses France's flag, see https://en.wikipedia.org/wiki/R%C3%A9union. |
|
| 250 | - 're' => 'France', |
|
| 251 | - 'ro' => 'Romania', |
|
| 252 | - 'ru' => 'Russia', |
|
| 253 | - 'rw' => 'Rwanda', |
|
| 254 | - 'bl' => 'Saint-Barthelemy', |
|
| 255 | - 'sh' => 'Saint-Helena', |
|
| 256 | - 'kn' => 'Saint-Kitts-and-Nevis', |
|
| 257 | - 'lc' => 'Saint-Lucia', |
|
| 258 | - 'mf' => 'Saint-Martin', |
|
| 259 | - // Uses France's flag, see https://en.wikipedia.org/wiki/Saint_Pierre_and_Miquelon. |
|
| 260 | - 'pm' => 'France', |
|
| 261 | - 'vc' => 'Saint-Vincent-and-the-Grenadines', |
|
| 262 | - 'ws' => 'Samoa', |
|
| 263 | - 'sm' => 'San-Marino', |
|
| 264 | - 'st' => 'Sao-Tome-and-Principe', |
|
| 265 | - 'sa' => 'Saudi-Arabia', |
|
| 266 | - 'sn' => 'Senegal', |
|
| 267 | - 'rs' => 'Serbia', |
|
| 268 | - 'sc' => 'Seychelles', |
|
| 269 | - 'sl' => 'Sierra-Leone', |
|
| 270 | - 'sg' => 'Singapore', |
|
| 271 | - 'sx' => null, |
|
| 272 | - 'sk' => 'Slovakia', |
|
| 273 | - 'si' => 'Slovenia', |
|
| 274 | - 'sb' => 'Solomon-Islands', |
|
| 275 | - 'so' => 'Somalia', |
|
| 276 | - 'za' => 'South-Africa', |
|
| 277 | - 'gs' => 'South-Georgia-and-the-South-Sandwich-Islands', |
|
| 278 | - 'ss' => 'South-Sudan', |
|
| 279 | - 'es' => 'Spain', |
|
| 280 | - 'lk' => 'Sri-Lanka', |
|
| 281 | - 'sd' => 'Sudan', |
|
| 282 | - 'sr' => 'Suriname', |
|
| 283 | - // Uses Norway's flag, see https://en.wikipedia.org/wiki/Svalbard_and_Jan_Mayen. |
|
| 284 | - 'sj' => 'Norway', |
|
| 285 | - 'sz' => 'Swaziland', |
|
| 286 | - 'se' => 'Sweden', |
|
| 287 | - 'ch' => 'Switzerland', |
|
| 288 | - 'sy' => 'Syria', |
|
| 289 | - 'tw' => 'Taiwan', |
|
| 290 | - 'tj' => 'Tajikistan', |
|
| 291 | - 'tz' => 'Tanzania', |
|
| 292 | - 'th' => 'Thailand', |
|
| 293 | - 'tl' => 'East-Timor', |
|
| 294 | - 'tg' => 'Togo', |
|
| 295 | - 'tk' => 'Tokelau', |
|
| 296 | - 'to' => 'Tonga', |
|
| 297 | - 'tt' => 'Trinidad-and-Tobago', |
|
| 298 | - 'tn' => 'Tunisia', |
|
| 299 | - 'tr' => 'Turkey', |
|
| 300 | - 'tm' => 'Turkmenistan', |
|
| 301 | - 'tc' => 'Turks-and-Caicos-Islands', |
|
| 302 | - 'tv' => 'Tuvalu', |
|
| 303 | - 'ug' => 'Uganda', |
|
| 304 | - 'ua' => 'Ukraine', |
|
| 305 | - 'ae' => 'United-Arab-Emirates', |
|
| 306 | - 'gb' => 'United-Kingdom', |
|
| 307 | - 'uk' => 'United-Kingdom', |
|
| 308 | - 'us' => 'United-States', |
|
| 309 | - 'um' => 'United-States', |
|
| 310 | - 'uy' => 'Uruguay', |
|
| 311 | - 'uz' => 'Uzbekistan', |
|
| 312 | - 'vu' => 'Vanuatu', |
|
| 313 | - 've' => 'Venezuela', |
|
| 314 | - 'vn' => 'Vietnam', |
|
| 315 | - 'vg' => 'British-Virgin-Islands', |
|
| 316 | - 'vi' => 'US-Virgin-Islands', |
|
| 317 | - 'wf' => 'Wallis-And-Futuna', |
|
| 318 | - 'eh' => 'Western-Sahara', |
|
| 319 | - 'ye' => 'Yemen', |
|
| 320 | - 'zm' => 'Zambia', |
|
| 321 | - 'zw' => 'Zimbabwe', |
|
| 322 | - ); |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * Parse_country_code_json_file_to_array. |
|
| 326 | - * |
|
| 327 | - * @param string $file_name The json file name where the supported country |
|
| 328 | - * and languages are present. |
|
| 329 | - * |
|
| 330 | - * @return array An Array having two maps, country_code_language_map and country_code_name_map. |
|
| 331 | - */ |
|
| 332 | - public static function parse_country_code_json_file_to_array( $file_name ) { |
|
| 333 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
|
| 334 | - $json_file_contents = file_get_contents( $file_name ); |
|
| 335 | - $decoded_array = json_decode( $json_file_contents, true ); |
|
| 336 | - // decoded array would be null if the json_decode parses |
|
| 337 | - // invalid content. |
|
| 338 | - if ( null === $decoded_array ) { |
|
| 339 | - return array( |
|
| 340 | - 'country_code_name_map' => array(), |
|
| 341 | - 'country_code_language_map' => array(), |
|
| 342 | - ); |
|
| 343 | - } |
|
| 344 | - $result = array(); |
|
| 345 | - // country_code => country_language map. |
|
| 346 | - $country_code_language_map = array(); |
|
| 347 | - // country_code => country_name map. |
|
| 348 | - $country_code_name_map = array(); |
|
| 349 | - foreach ( $decoded_array as $key => $value ) { |
|
| 350 | - $country_code_language_map[ $key ] = $value['supportedLang']; |
|
| 351 | - $country_code_name_map [ $key ] = $value['defaultLoc']['loc_name']; |
|
| 352 | - } |
|
| 353 | - $result['country_code_language_map'] = $country_code_language_map; |
|
| 354 | - $result['country_code_name_map'] = $country_code_name_map; |
|
| 355 | - |
|
| 356 | - return $result; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Get the list of WordLift's supported country codes from json file mapping country_code => languages. |
|
| 361 | - * |
|
| 362 | - * @param string $file_name The json file where the supported country codes and language_codes are stored. |
|
| 363 | - * |
|
| 364 | - * @return array An Array having two maps, country_code_language_map and country_code_name_map. |
|
| 365 | - * @since 3.22.5.1 |
|
| 366 | - */ |
|
| 367 | - public static function get_codes_from_json_file( $file_name ) { |
|
| 368 | - if ( file_exists( $file_name ) ) { |
|
| 369 | - return self::parse_country_code_json_file_to_array( $file_name ); |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - return array( |
|
| 373 | - 'country_code_name_map' => array(), |
|
| 374 | - 'country_code_language_map' => array(), |
|
| 375 | - ); |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * Returns the country language pairs. |
|
| 380 | - * |
|
| 381 | - * @return array The country language pairs. |
|
| 382 | - * @since 3.18.0 |
|
| 383 | - */ |
|
| 384 | - public static function get_codes() { |
|
| 385 | - return self::$codes; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * Populate self::codes and self::country_codes if not done before. |
|
| 390 | - * |
|
| 391 | - * @param string $file_name The json file where the supported country codes and language_codes are stored. |
|
| 392 | - * |
|
| 393 | - * @return void |
|
| 394 | - * @since 3.22.5.1 |
|
| 395 | - */ |
|
| 396 | - private static function lazy_populate_codes_and_country_codes_array( $file_name ) { |
|
| 397 | - if ( null === $file_name ) { |
|
| 398 | - $file_name = __DIR__ . '/supported-countries.json'; |
|
| 399 | - } |
|
| 400 | - if ( 0 === count( self::$codes ) || 0 === count( self::$country_codes ) ) { |
|
| 401 | - // populate the two arrays. |
|
| 402 | - $result_array = self::get_codes_from_json_file( $file_name ); |
|
| 403 | - self::$codes = $result_array['country_code_language_map']; |
|
| 404 | - self::$country_codes = $result_array['country_code_name_map']; |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * Reset codes_and_country_codes static variable, used for testing |
|
| 411 | - * |
|
| 412 | - * @return void |
|
| 413 | - * @since 3.22.5.1 |
|
| 414 | - */ |
|
| 415 | - public static function reset_codes_and_country_codes() { |
|
| 416 | - self::$codes = array(); |
|
| 417 | - self::$country_codes = array(); |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - /** |
|
| 421 | - * Get the list of WordLift's supported countries in an array with country code => country name pairs. |
|
| 422 | - * |
|
| 423 | - * @param string|false $lang Optional. The language code we are looking for. Default `any`. |
|
| 424 | - * |
|
| 425 | - * @param string|null $file_name Optional. The json file containing country codes |
|
| 426 | - * and language data. |
|
| 427 | - * |
|
| 428 | - * @return array An array with country code => country name pairs. |
|
| 429 | - * @since 3.18.0 |
|
| 430 | - */ |
|
| 431 | - public static function get_countries( $lang = false, $file_name = null ) { |
|
| 432 | - |
|
| 433 | - // populate the codes and countries array if it is not done before. |
|
| 434 | - self::lazy_populate_codes_and_country_codes_array( $file_name ); |
|
| 435 | - |
|
| 436 | - // Lazily load the countries. |
|
| 437 | - // $lang_key = false === $lang ? 'any' : $lang; |
|
| 438 | - $lang_key = 'any'; |
|
| 439 | - $lang = ''; |
|
| 440 | - |
|
| 441 | - if ( isset( self::$countries[ $lang_key ] ) ) { |
|
| 442 | - return self::$countries[ $lang_key ]; |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - // Prepare the array. |
|
| 446 | - self::$countries[ $lang ] = array(); |
|
| 447 | - |
|
| 448 | - // Get the country names from WP's own (multisite) function. |
|
| 449 | - foreach ( self::$codes as $key => $languages ) { |
|
| 450 | - if ( |
|
| 451 | - // Process all countries if there is no language specified. |
|
| 452 | - empty( $lang ) || |
|
| 453 | - |
|
| 454 | - // Or if there are no language limitations for current country. |
|
| 455 | - empty( self::$codes[ $key ] ) || |
|
| 456 | - |
|
| 457 | - // Or if the language code exists for current country. |
|
| 458 | - ! empty( $lang ) && in_array( $lang, self::$codes[ $key ], true ) |
|
| 459 | - ) { |
|
| 460 | - self::$countries[ $lang_key ][ $key ] = self::format_country_code( $key ); |
|
| 461 | - } |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - // Sort by country name. |
|
| 465 | - asort( self::$countries[ $lang_key ] ); |
|
| 466 | - |
|
| 467 | - // We don't sort here because `asort` returns bool instead of sorted array. |
|
| 468 | - return self::$countries[ $lang_key ]; |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - /** |
|
| 472 | - * Returns the country for a country code. This function is a clone of WP's function provided in `ms.php`. |
|
| 473 | - * |
|
| 474 | - * @param string $code Optional. The two-letter country code. Default empty. |
|
| 475 | - * |
|
| 476 | - * @return string The country corresponding to $code if it exists. If it does not exist, |
|
| 477 | - * then the first two letters of $code is returned. |
|
| 478 | - * @since 3.18.0 |
|
| 479 | - */ |
|
| 480 | - private static function format_country_code( $code = '' ) { |
|
| 481 | - |
|
| 482 | - $code = strtolower( substr( $code, 0, 2 ) ); |
|
| 483 | - /** |
|
| 484 | - * Filters the country codes. |
|
| 485 | - * |
|
| 486 | - * @param array $country_codes Key/value pair of country codes where key is the short version. |
|
| 487 | - * @param string $code A two-letter designation of the country. |
|
| 488 | - * |
|
| 489 | - * @since 3.18.0 |
|
| 490 | - */ |
|
| 491 | - $country_codes = apply_filters( 'country_code', self::$country_codes, $code ); |
|
| 492 | - |
|
| 493 | - return strtr( $code, $country_codes ); |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - /** |
|
| 497 | - * Get a flag URL. |
|
| 498 | - * |
|
| 499 | - * @param string $country_code The country code. |
|
| 500 | - * |
|
| 501 | - * @return string|null The flag url or null if not available. |
|
| 502 | - * @since 3.20.0 |
|
| 503 | - */ |
|
| 504 | - public static function get_flag_url( $country_code ) { |
|
| 505 | - |
|
| 506 | - // Bail out if we don't have the flag. |
|
| 507 | - if ( ! isset( self::$country_flags[ $country_code ] ) |
|
| 508 | - || ( self::$country_flags[ $country_code ] ) === null ) { |
|
| 509 | - return null; |
|
| 510 | - } |
|
| 511 | - |
|
| 512 | - return plugin_dir_url( __DIR__ ) |
|
| 513 | - . 'images/flags/16/' |
|
| 514 | - . self::$country_flags[ $country_code ] |
|
| 515 | - . '.png'; |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - /** |
|
| 519 | - * Get a country name given a country code. |
|
| 520 | - * |
|
| 521 | - * @param string $country_code The 2-letters country code. |
|
| 522 | - * |
|
| 523 | - * @return null|string The country name (in English) or null if not found. |
|
| 524 | - * @since 3.20.0 |
|
| 525 | - */ |
|
| 526 | - public static function get_country_name( $country_code ) { |
|
| 527 | - |
|
| 528 | - /** |
|
| 529 | - * @since 3.27.6 |
|
| 530 | - * |
|
| 531 | - * @see https://github.com/insideout10/wordlift-plugin/issues/1188 |
|
| 532 | - */ |
|
| 533 | - if ( ! isset( self::$country_codes[ $country_code ] ) ) { |
|
| 534 | - return ''; |
|
| 535 | - } |
|
| 536 | - |
|
| 537 | - return self::$country_codes[ $country_code ]; |
|
| 538 | - } |
|
| 24 | + /** |
|
| 25 | + * An array that will contain country codes => country names pairs. It gets lazily loaded the first time by the |
|
| 26 | + * `get_countries` function. |
|
| 27 | + * |
|
| 28 | + * @since 3.18.0 |
|
| 29 | + * @var array An array of country codes => country names pairs or NULL if not initialized yet. |
|
| 30 | + */ |
|
| 31 | + private static $countries = array(); |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * The list of supported country codes, this is populated by self::lazy_populate_codes_and_country_codes_array. |
|
| 35 | + * |
|
| 36 | + * WARNING! If you change the list of supported countries, *you have* to add the related flag |
|
| 37 | + * in the images/flags folder. |
|
| 38 | + * |
|
| 39 | + * @since 3.18.0 |
|
| 40 | + * |
|
| 41 | + * @var array An array of country codes => supported_languages_array |
|
| 42 | + */ |
|
| 43 | + public static $codes = array(); |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * The list of country codes, this is populated by self::lazy_populate_codes_and_country_codes_array. |
|
| 47 | + * |
|
| 48 | + * WARNING! If you change the list of supported countries, *you have* to add the related flag |
|
| 49 | + * in the images/flags folder. |
|
| 50 | + * |
|
| 51 | + * @since 3.18.0 |
|
| 52 | + * |
|
| 53 | + * @var array An array of country codes => country names. |
|
| 54 | + */ |
|
| 55 | + private static $country_codes = array(); |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * An array of flag filenames. |
|
| 59 | + * |
|
| 60 | + * @since 3.20.0 |
|
| 61 | + * |
|
| 62 | + * @var array An array of flag filenames. |
|
| 63 | + */ |
|
| 64 | + private static $country_flags = array( |
|
| 65 | + 'af' => 'Afghanistan', |
|
| 66 | + 'ax' => 'Aland', |
|
| 67 | + 'al' => 'Albania', |
|
| 68 | + 'dz' => 'Algeria', |
|
| 69 | + 'as' => 'American-Samoa', |
|
| 70 | + 'ad' => 'Andorra', |
|
| 71 | + 'ao' => 'Angola', |
|
| 72 | + 'ai' => 'Anguilla', |
|
| 73 | + 'aq' => 'Antarctica', |
|
| 74 | + 'ag' => 'Antigua-and-Barbuda', |
|
| 75 | + 'ar' => 'Argentina', |
|
| 76 | + 'am' => 'Armenia', |
|
| 77 | + 'aw' => 'Aruba', |
|
| 78 | + 'au' => 'Australia', |
|
| 79 | + 'at' => 'Austria', |
|
| 80 | + 'az' => 'Azerbaijan', |
|
| 81 | + 'bs' => 'Bahamas', |
|
| 82 | + 'bh' => 'Bahrain', |
|
| 83 | + 'bd' => 'Bangladesh', |
|
| 84 | + 'bb' => 'Barbados', |
|
| 85 | + 'by' => 'Belarus', |
|
| 86 | + 'be' => 'Belgium', |
|
| 87 | + 'bz' => 'Belize', |
|
| 88 | + 'bj' => 'Benin', |
|
| 89 | + 'bm' => 'Bermuda', |
|
| 90 | + 'bt' => 'Bhutan', |
|
| 91 | + 'bo' => 'Bolivia', |
|
| 92 | + // Uses Netherlands' flag, see https://en.wikipedia.org/wiki/Caribbean_Netherlands. |
|
| 93 | + 'bq' => 'Netherlands', |
|
| 94 | + 'ba' => 'Bosnia-and-Herzegovina', |
|
| 95 | + 'bw' => 'Botswana', |
|
| 96 | + 'bv' => 'Bouvet Island', |
|
| 97 | + 'br' => 'Brazil', |
|
| 98 | + 'io' => null, |
|
| 99 | + 'bn' => 'Brunei', |
|
| 100 | + 'bg' => 'Bulgaria', |
|
| 101 | + 'bf' => 'Burkina-Faso', |
|
| 102 | + 'bi' => 'Burundi', |
|
| 103 | + 'kh' => 'Cambodia', |
|
| 104 | + 'cm' => 'Cameroon', |
|
| 105 | + 'ca' => 'Canada', |
|
| 106 | + 'cv' => 'Cape-Verde', |
|
| 107 | + 'ky' => 'Cayman-Islands', |
|
| 108 | + 'cf' => 'Central-African-Republic', |
|
| 109 | + 'td' => 'Chad', |
|
| 110 | + 'cl' => 'Chile', |
|
| 111 | + 'cn' => 'China', |
|
| 112 | + 'cx' => 'Christmas-Island', |
|
| 113 | + 'cc' => 'Cocos-Keeling-Islands', |
|
| 114 | + 'co' => 'Colombia', |
|
| 115 | + 'km' => 'Comoros', |
|
| 116 | + 'cg' => 'Republic-of-the-Congo', |
|
| 117 | + 'cd' => 'Democratic-Republic-of-the-Congo', |
|
| 118 | + 'ck' => 'Cook-Islands', |
|
| 119 | + 'cr' => 'Costa-Rica', |
|
| 120 | + 'ci' => 'Cote-dIvoire', |
|
| 121 | + 'hr' => 'Croatia', |
|
| 122 | + 'cu' => 'Cuba', |
|
| 123 | + 'cw' => 'Curacao', |
|
| 124 | + 'cy' => 'Cyprus', |
|
| 125 | + 'cz' => 'Czech-Republic', |
|
| 126 | + 'dk' => 'Denmark', |
|
| 127 | + 'dj' => 'Djibouti', |
|
| 128 | + 'dm' => 'Dominica', |
|
| 129 | + 'do' => 'Dominican-Republic', |
|
| 130 | + 'ec' => 'Ecuador', |
|
| 131 | + 'eg' => 'Egypt', |
|
| 132 | + 'sv' => 'El-Salvador', |
|
| 133 | + 'gq' => 'Equatorial-Guinea', |
|
| 134 | + 'er' => 'Eritrea', |
|
| 135 | + 'ee' => 'Estonia', |
|
| 136 | + 'et' => 'Ethiopia', |
|
| 137 | + 'fk' => 'Falkland-Islands', |
|
| 138 | + 'fo' => 'Faroes', |
|
| 139 | + 'fj' => 'Fiji', |
|
| 140 | + 'fi' => 'Finland', |
|
| 141 | + 'fr' => 'France', |
|
| 142 | + // Uses France's flag, see https://en.wikipedia.org/wiki/French_Guiana. |
|
| 143 | + 'gf' => 'France', |
|
| 144 | + 'pf' => 'French-Polynesia', |
|
| 145 | + 'tf' => 'French-Southern-Territories', |
|
| 146 | + 'ga' => 'Gabon', |
|
| 147 | + 'gm' => 'Gambia', |
|
| 148 | + 'ge' => 'Georgia', |
|
| 149 | + 'de' => 'Germany', |
|
| 150 | + 'gh' => 'Ghana', |
|
| 151 | + 'gi' => 'Gibraltar', |
|
| 152 | + 'gr' => 'Greece', |
|
| 153 | + 'gl' => 'Greenland', |
|
| 154 | + 'gd' => 'Grenada', |
|
| 155 | + // Uses France's flag, see https://en.wikipedia.org/wiki/Guadeloupe. |
|
| 156 | + 'gp' => 'France', |
|
| 157 | + 'gu' => 'Guam', |
|
| 158 | + 'gt' => 'Guatemala', |
|
| 159 | + 'gg' => 'Guernsey', |
|
| 160 | + 'gn' => 'Guinea', |
|
| 161 | + 'gw' => 'Guinea-Bissau', |
|
| 162 | + 'gy' => 'Guyana', |
|
| 163 | + 'ht' => 'Haiti', |
|
| 164 | + // Uses Australia's flag, see https://en.wikipedia.org/wiki/Heard_Island_and_McDonald_Islands. |
|
| 165 | + 'hm' => 'Australia', |
|
| 166 | + 'va' => 'Vatican-City', |
|
| 167 | + 'hn' => 'Honduras', |
|
| 168 | + 'hk' => 'Hong-Kong', |
|
| 169 | + 'hu' => 'Hungary', |
|
| 170 | + 'is' => 'Iceland', |
|
| 171 | + 'in' => 'India', |
|
| 172 | + 'id' => 'Indonesia', |
|
| 173 | + 'ir' => 'Iran', |
|
| 174 | + 'iq' => 'Iraq', |
|
| 175 | + 'ie' => 'Ireland', |
|
| 176 | + 'im' => 'Isle-of-Man', |
|
| 177 | + 'il' => 'Israel', |
|
| 178 | + 'it' => 'Italy', |
|
| 179 | + 'jm' => 'Jamaica', |
|
| 180 | + 'jp' => 'Japan', |
|
| 181 | + 'je' => 'Jersey', |
|
| 182 | + 'jo' => 'Jordan', |
|
| 183 | + 'kz' => 'Kazakhstan', |
|
| 184 | + 'ke' => 'Kenya', |
|
| 185 | + 'ki' => 'Kiribati', |
|
| 186 | + 'kp' => 'North-Korea', |
|
| 187 | + 'kr' => 'South-Korea', |
|
| 188 | + 'kw' => 'Kuwait', |
|
| 189 | + 'kg' => 'Kyrgyzstan', |
|
| 190 | + 'la' => 'Laos', |
|
| 191 | + 'lv' => 'Latvia', |
|
| 192 | + 'lb' => 'Lebanon', |
|
| 193 | + 'ls' => 'Lesotho', |
|
| 194 | + 'lr' => 'Liberia', |
|
| 195 | + 'ly' => 'Libya', |
|
| 196 | + 'li' => 'Liechtenstein', |
|
| 197 | + 'lt' => 'Lithuania', |
|
| 198 | + 'lu' => 'Luxembourg', |
|
| 199 | + 'mo' => 'Macau', |
|
| 200 | + 'mk' => 'Macedonia', |
|
| 201 | + 'mg' => 'Madagascar', |
|
| 202 | + 'mw' => 'Malawi', |
|
| 203 | + 'my' => 'Malaysia', |
|
| 204 | + 'mv' => 'Maldives', |
|
| 205 | + 'ml' => 'Mali', |
|
| 206 | + 'mt' => 'Malta', |
|
| 207 | + 'mh' => 'Marshall-Islands', |
|
| 208 | + 'mq' => 'Martinique', |
|
| 209 | + 'mr' => 'Mauritania', |
|
| 210 | + 'mu' => 'Mauritius', |
|
| 211 | + 'yt' => 'Mayotte', |
|
| 212 | + 'mx' => 'Mexico', |
|
| 213 | + 'fm' => 'Micronesia', |
|
| 214 | + 'md' => 'Moldova', |
|
| 215 | + 'mc' => 'Monaco', |
|
| 216 | + 'mn' => 'Mongolia', |
|
| 217 | + 'me' => 'Montenegro', |
|
| 218 | + 'ms' => 'Montserrat', |
|
| 219 | + 'ma' => 'Morocco', |
|
| 220 | + 'mz' => 'Mozambique', |
|
| 221 | + 'mm' => 'Myanmar', |
|
| 222 | + 'na' => 'Namibia', |
|
| 223 | + 'nr' => 'Nauru', |
|
| 224 | + 'np' => 'Nepal', |
|
| 225 | + 'nl' => 'Netherlands', |
|
| 226 | + 'nc' => 'New-Caledonia', |
|
| 227 | + 'nz' => 'New-Zealand', |
|
| 228 | + 'ni' => 'Nicaragua', |
|
| 229 | + 'ne' => 'Niger', |
|
| 230 | + 'ng' => 'Nigeria', |
|
| 231 | + 'nu' => 'Niue', |
|
| 232 | + 'nf' => 'Norfolk-Island', |
|
| 233 | + 'mp' => 'Northern-Mariana-Islands', |
|
| 234 | + 'no' => 'Norway', |
|
| 235 | + 'om' => 'Oman', |
|
| 236 | + 'pk' => 'Pakistan', |
|
| 237 | + 'pw' => 'Palau', |
|
| 238 | + 'ps' => 'Palestine', |
|
| 239 | + 'pa' => 'Panama', |
|
| 240 | + 'pg' => 'Papua-New-Guinea', |
|
| 241 | + 'py' => 'Paraguay', |
|
| 242 | + 'pe' => 'Peru', |
|
| 243 | + 'ph' => 'Philippines', |
|
| 244 | + 'pn' => 'Pitcairn-Islands', |
|
| 245 | + 'pl' => 'Poland', |
|
| 246 | + 'pt' => 'Portugal', |
|
| 247 | + 'pr' => 'Puerto Rico', |
|
| 248 | + 'qa' => 'Qatar', |
|
| 249 | + // Uses France's flag, see https://en.wikipedia.org/wiki/R%C3%A9union. |
|
| 250 | + 're' => 'France', |
|
| 251 | + 'ro' => 'Romania', |
|
| 252 | + 'ru' => 'Russia', |
|
| 253 | + 'rw' => 'Rwanda', |
|
| 254 | + 'bl' => 'Saint-Barthelemy', |
|
| 255 | + 'sh' => 'Saint-Helena', |
|
| 256 | + 'kn' => 'Saint-Kitts-and-Nevis', |
|
| 257 | + 'lc' => 'Saint-Lucia', |
|
| 258 | + 'mf' => 'Saint-Martin', |
|
| 259 | + // Uses France's flag, see https://en.wikipedia.org/wiki/Saint_Pierre_and_Miquelon. |
|
| 260 | + 'pm' => 'France', |
|
| 261 | + 'vc' => 'Saint-Vincent-and-the-Grenadines', |
|
| 262 | + 'ws' => 'Samoa', |
|
| 263 | + 'sm' => 'San-Marino', |
|
| 264 | + 'st' => 'Sao-Tome-and-Principe', |
|
| 265 | + 'sa' => 'Saudi-Arabia', |
|
| 266 | + 'sn' => 'Senegal', |
|
| 267 | + 'rs' => 'Serbia', |
|
| 268 | + 'sc' => 'Seychelles', |
|
| 269 | + 'sl' => 'Sierra-Leone', |
|
| 270 | + 'sg' => 'Singapore', |
|
| 271 | + 'sx' => null, |
|
| 272 | + 'sk' => 'Slovakia', |
|
| 273 | + 'si' => 'Slovenia', |
|
| 274 | + 'sb' => 'Solomon-Islands', |
|
| 275 | + 'so' => 'Somalia', |
|
| 276 | + 'za' => 'South-Africa', |
|
| 277 | + 'gs' => 'South-Georgia-and-the-South-Sandwich-Islands', |
|
| 278 | + 'ss' => 'South-Sudan', |
|
| 279 | + 'es' => 'Spain', |
|
| 280 | + 'lk' => 'Sri-Lanka', |
|
| 281 | + 'sd' => 'Sudan', |
|
| 282 | + 'sr' => 'Suriname', |
|
| 283 | + // Uses Norway's flag, see https://en.wikipedia.org/wiki/Svalbard_and_Jan_Mayen. |
|
| 284 | + 'sj' => 'Norway', |
|
| 285 | + 'sz' => 'Swaziland', |
|
| 286 | + 'se' => 'Sweden', |
|
| 287 | + 'ch' => 'Switzerland', |
|
| 288 | + 'sy' => 'Syria', |
|
| 289 | + 'tw' => 'Taiwan', |
|
| 290 | + 'tj' => 'Tajikistan', |
|
| 291 | + 'tz' => 'Tanzania', |
|
| 292 | + 'th' => 'Thailand', |
|
| 293 | + 'tl' => 'East-Timor', |
|
| 294 | + 'tg' => 'Togo', |
|
| 295 | + 'tk' => 'Tokelau', |
|
| 296 | + 'to' => 'Tonga', |
|
| 297 | + 'tt' => 'Trinidad-and-Tobago', |
|
| 298 | + 'tn' => 'Tunisia', |
|
| 299 | + 'tr' => 'Turkey', |
|
| 300 | + 'tm' => 'Turkmenistan', |
|
| 301 | + 'tc' => 'Turks-and-Caicos-Islands', |
|
| 302 | + 'tv' => 'Tuvalu', |
|
| 303 | + 'ug' => 'Uganda', |
|
| 304 | + 'ua' => 'Ukraine', |
|
| 305 | + 'ae' => 'United-Arab-Emirates', |
|
| 306 | + 'gb' => 'United-Kingdom', |
|
| 307 | + 'uk' => 'United-Kingdom', |
|
| 308 | + 'us' => 'United-States', |
|
| 309 | + 'um' => 'United-States', |
|
| 310 | + 'uy' => 'Uruguay', |
|
| 311 | + 'uz' => 'Uzbekistan', |
|
| 312 | + 'vu' => 'Vanuatu', |
|
| 313 | + 've' => 'Venezuela', |
|
| 314 | + 'vn' => 'Vietnam', |
|
| 315 | + 'vg' => 'British-Virgin-Islands', |
|
| 316 | + 'vi' => 'US-Virgin-Islands', |
|
| 317 | + 'wf' => 'Wallis-And-Futuna', |
|
| 318 | + 'eh' => 'Western-Sahara', |
|
| 319 | + 'ye' => 'Yemen', |
|
| 320 | + 'zm' => 'Zambia', |
|
| 321 | + 'zw' => 'Zimbabwe', |
|
| 322 | + ); |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * Parse_country_code_json_file_to_array. |
|
| 326 | + * |
|
| 327 | + * @param string $file_name The json file name where the supported country |
|
| 328 | + * and languages are present. |
|
| 329 | + * |
|
| 330 | + * @return array An Array having two maps, country_code_language_map and country_code_name_map. |
|
| 331 | + */ |
|
| 332 | + public static function parse_country_code_json_file_to_array( $file_name ) { |
|
| 333 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
|
| 334 | + $json_file_contents = file_get_contents( $file_name ); |
|
| 335 | + $decoded_array = json_decode( $json_file_contents, true ); |
|
| 336 | + // decoded array would be null if the json_decode parses |
|
| 337 | + // invalid content. |
|
| 338 | + if ( null === $decoded_array ) { |
|
| 339 | + return array( |
|
| 340 | + 'country_code_name_map' => array(), |
|
| 341 | + 'country_code_language_map' => array(), |
|
| 342 | + ); |
|
| 343 | + } |
|
| 344 | + $result = array(); |
|
| 345 | + // country_code => country_language map. |
|
| 346 | + $country_code_language_map = array(); |
|
| 347 | + // country_code => country_name map. |
|
| 348 | + $country_code_name_map = array(); |
|
| 349 | + foreach ( $decoded_array as $key => $value ) { |
|
| 350 | + $country_code_language_map[ $key ] = $value['supportedLang']; |
|
| 351 | + $country_code_name_map [ $key ] = $value['defaultLoc']['loc_name']; |
|
| 352 | + } |
|
| 353 | + $result['country_code_language_map'] = $country_code_language_map; |
|
| 354 | + $result['country_code_name_map'] = $country_code_name_map; |
|
| 355 | + |
|
| 356 | + return $result; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Get the list of WordLift's supported country codes from json file mapping country_code => languages. |
|
| 361 | + * |
|
| 362 | + * @param string $file_name The json file where the supported country codes and language_codes are stored. |
|
| 363 | + * |
|
| 364 | + * @return array An Array having two maps, country_code_language_map and country_code_name_map. |
|
| 365 | + * @since 3.22.5.1 |
|
| 366 | + */ |
|
| 367 | + public static function get_codes_from_json_file( $file_name ) { |
|
| 368 | + if ( file_exists( $file_name ) ) { |
|
| 369 | + return self::parse_country_code_json_file_to_array( $file_name ); |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + return array( |
|
| 373 | + 'country_code_name_map' => array(), |
|
| 374 | + 'country_code_language_map' => array(), |
|
| 375 | + ); |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * Returns the country language pairs. |
|
| 380 | + * |
|
| 381 | + * @return array The country language pairs. |
|
| 382 | + * @since 3.18.0 |
|
| 383 | + */ |
|
| 384 | + public static function get_codes() { |
|
| 385 | + return self::$codes; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * Populate self::codes and self::country_codes if not done before. |
|
| 390 | + * |
|
| 391 | + * @param string $file_name The json file where the supported country codes and language_codes are stored. |
|
| 392 | + * |
|
| 393 | + * @return void |
|
| 394 | + * @since 3.22.5.1 |
|
| 395 | + */ |
|
| 396 | + private static function lazy_populate_codes_and_country_codes_array( $file_name ) { |
|
| 397 | + if ( null === $file_name ) { |
|
| 398 | + $file_name = __DIR__ . '/supported-countries.json'; |
|
| 399 | + } |
|
| 400 | + if ( 0 === count( self::$codes ) || 0 === count( self::$country_codes ) ) { |
|
| 401 | + // populate the two arrays. |
|
| 402 | + $result_array = self::get_codes_from_json_file( $file_name ); |
|
| 403 | + self::$codes = $result_array['country_code_language_map']; |
|
| 404 | + self::$country_codes = $result_array['country_code_name_map']; |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * Reset codes_and_country_codes static variable, used for testing |
|
| 411 | + * |
|
| 412 | + * @return void |
|
| 413 | + * @since 3.22.5.1 |
|
| 414 | + */ |
|
| 415 | + public static function reset_codes_and_country_codes() { |
|
| 416 | + self::$codes = array(); |
|
| 417 | + self::$country_codes = array(); |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | + * Get the list of WordLift's supported countries in an array with country code => country name pairs. |
|
| 422 | + * |
|
| 423 | + * @param string|false $lang Optional. The language code we are looking for. Default `any`. |
|
| 424 | + * |
|
| 425 | + * @param string|null $file_name Optional. The json file containing country codes |
|
| 426 | + * and language data. |
|
| 427 | + * |
|
| 428 | + * @return array An array with country code => country name pairs. |
|
| 429 | + * @since 3.18.0 |
|
| 430 | + */ |
|
| 431 | + public static function get_countries( $lang = false, $file_name = null ) { |
|
| 432 | + |
|
| 433 | + // populate the codes and countries array if it is not done before. |
|
| 434 | + self::lazy_populate_codes_and_country_codes_array( $file_name ); |
|
| 435 | + |
|
| 436 | + // Lazily load the countries. |
|
| 437 | + // $lang_key = false === $lang ? 'any' : $lang; |
|
| 438 | + $lang_key = 'any'; |
|
| 439 | + $lang = ''; |
|
| 440 | + |
|
| 441 | + if ( isset( self::$countries[ $lang_key ] ) ) { |
|
| 442 | + return self::$countries[ $lang_key ]; |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + // Prepare the array. |
|
| 446 | + self::$countries[ $lang ] = array(); |
|
| 447 | + |
|
| 448 | + // Get the country names from WP's own (multisite) function. |
|
| 449 | + foreach ( self::$codes as $key => $languages ) { |
|
| 450 | + if ( |
|
| 451 | + // Process all countries if there is no language specified. |
|
| 452 | + empty( $lang ) || |
|
| 453 | + |
|
| 454 | + // Or if there are no language limitations for current country. |
|
| 455 | + empty( self::$codes[ $key ] ) || |
|
| 456 | + |
|
| 457 | + // Or if the language code exists for current country. |
|
| 458 | + ! empty( $lang ) && in_array( $lang, self::$codes[ $key ], true ) |
|
| 459 | + ) { |
|
| 460 | + self::$countries[ $lang_key ][ $key ] = self::format_country_code( $key ); |
|
| 461 | + } |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + // Sort by country name. |
|
| 465 | + asort( self::$countries[ $lang_key ] ); |
|
| 466 | + |
|
| 467 | + // We don't sort here because `asort` returns bool instead of sorted array. |
|
| 468 | + return self::$countries[ $lang_key ]; |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + /** |
|
| 472 | + * Returns the country for a country code. This function is a clone of WP's function provided in `ms.php`. |
|
| 473 | + * |
|
| 474 | + * @param string $code Optional. The two-letter country code. Default empty. |
|
| 475 | + * |
|
| 476 | + * @return string The country corresponding to $code if it exists. If it does not exist, |
|
| 477 | + * then the first two letters of $code is returned. |
|
| 478 | + * @since 3.18.0 |
|
| 479 | + */ |
|
| 480 | + private static function format_country_code( $code = '' ) { |
|
| 481 | + |
|
| 482 | + $code = strtolower( substr( $code, 0, 2 ) ); |
|
| 483 | + /** |
|
| 484 | + * Filters the country codes. |
|
| 485 | + * |
|
| 486 | + * @param array $country_codes Key/value pair of country codes where key is the short version. |
|
| 487 | + * @param string $code A two-letter designation of the country. |
|
| 488 | + * |
|
| 489 | + * @since 3.18.0 |
|
| 490 | + */ |
|
| 491 | + $country_codes = apply_filters( 'country_code', self::$country_codes, $code ); |
|
| 492 | + |
|
| 493 | + return strtr( $code, $country_codes ); |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + /** |
|
| 497 | + * Get a flag URL. |
|
| 498 | + * |
|
| 499 | + * @param string $country_code The country code. |
|
| 500 | + * |
|
| 501 | + * @return string|null The flag url or null if not available. |
|
| 502 | + * @since 3.20.0 |
|
| 503 | + */ |
|
| 504 | + public static function get_flag_url( $country_code ) { |
|
| 505 | + |
|
| 506 | + // Bail out if we don't have the flag. |
|
| 507 | + if ( ! isset( self::$country_flags[ $country_code ] ) |
|
| 508 | + || ( self::$country_flags[ $country_code ] ) === null ) { |
|
| 509 | + return null; |
|
| 510 | + } |
|
| 511 | + |
|
| 512 | + return plugin_dir_url( __DIR__ ) |
|
| 513 | + . 'images/flags/16/' |
|
| 514 | + . self::$country_flags[ $country_code ] |
|
| 515 | + . '.png'; |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + /** |
|
| 519 | + * Get a country name given a country code. |
|
| 520 | + * |
|
| 521 | + * @param string $country_code The 2-letters country code. |
|
| 522 | + * |
|
| 523 | + * @return null|string The country name (in English) or null if not found. |
|
| 524 | + * @since 3.20.0 |
|
| 525 | + */ |
|
| 526 | + public static function get_country_name( $country_code ) { |
|
| 527 | + |
|
| 528 | + /** |
|
| 529 | + * @since 3.27.6 |
|
| 530 | + * |
|
| 531 | + * @see https://github.com/insideout10/wordlift-plugin/issues/1188 |
|
| 532 | + */ |
|
| 533 | + if ( ! isset( self::$country_codes[ $country_code ] ) ) { |
|
| 534 | + return ''; |
|
| 535 | + } |
|
| 536 | + |
|
| 537 | + return self::$country_codes[ $country_code ]; |
|
| 538 | + } |
|
| 539 | 539 | |
| 540 | 540 | } |
@@ -10,7 +10,7 @@ discard block |
||
| 10 | 10 | * @since 3.18.0 |
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 13 | +if ( ! defined('ABSPATH')) { |
|
| 14 | 14 | exit; |
| 15 | 15 | } |
| 16 | 16 | |
@@ -329,13 +329,13 @@ discard block |
||
| 329 | 329 | * |
| 330 | 330 | * @return array An Array having two maps, country_code_language_map and country_code_name_map. |
| 331 | 331 | */ |
| 332 | - public static function parse_country_code_json_file_to_array( $file_name ) { |
|
| 332 | + public static function parse_country_code_json_file_to_array($file_name) { |
|
| 333 | 333 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 334 | - $json_file_contents = file_get_contents( $file_name ); |
|
| 335 | - $decoded_array = json_decode( $json_file_contents, true ); |
|
| 334 | + $json_file_contents = file_get_contents($file_name); |
|
| 335 | + $decoded_array = json_decode($json_file_contents, true); |
|
| 336 | 336 | // decoded array would be null if the json_decode parses |
| 337 | 337 | // invalid content. |
| 338 | - if ( null === $decoded_array ) { |
|
| 338 | + if (null === $decoded_array) { |
|
| 339 | 339 | return array( |
| 340 | 340 | 'country_code_name_map' => array(), |
| 341 | 341 | 'country_code_language_map' => array(), |
@@ -346,9 +346,9 @@ discard block |
||
| 346 | 346 | $country_code_language_map = array(); |
| 347 | 347 | // country_code => country_name map. |
| 348 | 348 | $country_code_name_map = array(); |
| 349 | - foreach ( $decoded_array as $key => $value ) { |
|
| 350 | - $country_code_language_map[ $key ] = $value['supportedLang']; |
|
| 351 | - $country_code_name_map [ $key ] = $value['defaultLoc']['loc_name']; |
|
| 349 | + foreach ($decoded_array as $key => $value) { |
|
| 350 | + $country_code_language_map[$key] = $value['supportedLang']; |
|
| 351 | + $country_code_name_map [$key] = $value['defaultLoc']['loc_name']; |
|
| 352 | 352 | } |
| 353 | 353 | $result['country_code_language_map'] = $country_code_language_map; |
| 354 | 354 | $result['country_code_name_map'] = $country_code_name_map; |
@@ -364,9 +364,9 @@ discard block |
||
| 364 | 364 | * @return array An Array having two maps, country_code_language_map and country_code_name_map. |
| 365 | 365 | * @since 3.22.5.1 |
| 366 | 366 | */ |
| 367 | - public static function get_codes_from_json_file( $file_name ) { |
|
| 368 | - if ( file_exists( $file_name ) ) { |
|
| 369 | - return self::parse_country_code_json_file_to_array( $file_name ); |
|
| 367 | + public static function get_codes_from_json_file($file_name) { |
|
| 368 | + if (file_exists($file_name)) { |
|
| 369 | + return self::parse_country_code_json_file_to_array($file_name); |
|
| 370 | 370 | } |
| 371 | 371 | |
| 372 | 372 | return array( |
@@ -393,13 +393,13 @@ discard block |
||
| 393 | 393 | * @return void |
| 394 | 394 | * @since 3.22.5.1 |
| 395 | 395 | */ |
| 396 | - private static function lazy_populate_codes_and_country_codes_array( $file_name ) { |
|
| 397 | - if ( null === $file_name ) { |
|
| 398 | - $file_name = __DIR__ . '/supported-countries.json'; |
|
| 396 | + private static function lazy_populate_codes_and_country_codes_array($file_name) { |
|
| 397 | + if (null === $file_name) { |
|
| 398 | + $file_name = __DIR__.'/supported-countries.json'; |
|
| 399 | 399 | } |
| 400 | - if ( 0 === count( self::$codes ) || 0 === count( self::$country_codes ) ) { |
|
| 400 | + if (0 === count(self::$codes) || 0 === count(self::$country_codes)) { |
|
| 401 | 401 | // populate the two arrays. |
| 402 | - $result_array = self::get_codes_from_json_file( $file_name ); |
|
| 402 | + $result_array = self::get_codes_from_json_file($file_name); |
|
| 403 | 403 | self::$codes = $result_array['country_code_language_map']; |
| 404 | 404 | self::$country_codes = $result_array['country_code_name_map']; |
| 405 | 405 | } |
@@ -428,44 +428,44 @@ discard block |
||
| 428 | 428 | * @return array An array with country code => country name pairs. |
| 429 | 429 | * @since 3.18.0 |
| 430 | 430 | */ |
| 431 | - public static function get_countries( $lang = false, $file_name = null ) { |
|
| 431 | + public static function get_countries($lang = false, $file_name = null) { |
|
| 432 | 432 | |
| 433 | 433 | // populate the codes and countries array if it is not done before. |
| 434 | - self::lazy_populate_codes_and_country_codes_array( $file_name ); |
|
| 434 | + self::lazy_populate_codes_and_country_codes_array($file_name); |
|
| 435 | 435 | |
| 436 | 436 | // Lazily load the countries. |
| 437 | 437 | // $lang_key = false === $lang ? 'any' : $lang; |
| 438 | 438 | $lang_key = 'any'; |
| 439 | 439 | $lang = ''; |
| 440 | 440 | |
| 441 | - if ( isset( self::$countries[ $lang_key ] ) ) { |
|
| 442 | - return self::$countries[ $lang_key ]; |
|
| 441 | + if (isset(self::$countries[$lang_key])) { |
|
| 442 | + return self::$countries[$lang_key]; |
|
| 443 | 443 | } |
| 444 | 444 | |
| 445 | 445 | // Prepare the array. |
| 446 | - self::$countries[ $lang ] = array(); |
|
| 446 | + self::$countries[$lang] = array(); |
|
| 447 | 447 | |
| 448 | 448 | // Get the country names from WP's own (multisite) function. |
| 449 | - foreach ( self::$codes as $key => $languages ) { |
|
| 449 | + foreach (self::$codes as $key => $languages) { |
|
| 450 | 450 | if ( |
| 451 | 451 | // Process all countries if there is no language specified. |
| 452 | - empty( $lang ) || |
|
| 452 | + empty($lang) || |
|
| 453 | 453 | |
| 454 | 454 | // Or if there are no language limitations for current country. |
| 455 | - empty( self::$codes[ $key ] ) || |
|
| 455 | + empty(self::$codes[$key]) || |
|
| 456 | 456 | |
| 457 | 457 | // Or if the language code exists for current country. |
| 458 | - ! empty( $lang ) && in_array( $lang, self::$codes[ $key ], true ) |
|
| 458 | + ! empty($lang) && in_array($lang, self::$codes[$key], true) |
|
| 459 | 459 | ) { |
| 460 | - self::$countries[ $lang_key ][ $key ] = self::format_country_code( $key ); |
|
| 460 | + self::$countries[$lang_key][$key] = self::format_country_code($key); |
|
| 461 | 461 | } |
| 462 | 462 | } |
| 463 | 463 | |
| 464 | 464 | // Sort by country name. |
| 465 | - asort( self::$countries[ $lang_key ] ); |
|
| 465 | + asort(self::$countries[$lang_key]); |
|
| 466 | 466 | |
| 467 | 467 | // We don't sort here because `asort` returns bool instead of sorted array. |
| 468 | - return self::$countries[ $lang_key ]; |
|
| 468 | + return self::$countries[$lang_key]; |
|
| 469 | 469 | } |
| 470 | 470 | |
| 471 | 471 | /** |
@@ -477,9 +477,9 @@ discard block |
||
| 477 | 477 | * then the first two letters of $code is returned. |
| 478 | 478 | * @since 3.18.0 |
| 479 | 479 | */ |
| 480 | - private static function format_country_code( $code = '' ) { |
|
| 480 | + private static function format_country_code($code = '') { |
|
| 481 | 481 | |
| 482 | - $code = strtolower( substr( $code, 0, 2 ) ); |
|
| 482 | + $code = strtolower(substr($code, 0, 2)); |
|
| 483 | 483 | /** |
| 484 | 484 | * Filters the country codes. |
| 485 | 485 | * |
@@ -488,9 +488,9 @@ discard block |
||
| 488 | 488 | * |
| 489 | 489 | * @since 3.18.0 |
| 490 | 490 | */ |
| 491 | - $country_codes = apply_filters( 'country_code', self::$country_codes, $code ); |
|
| 491 | + $country_codes = apply_filters('country_code', self::$country_codes, $code); |
|
| 492 | 492 | |
| 493 | - return strtr( $code, $country_codes ); |
|
| 493 | + return strtr($code, $country_codes); |
|
| 494 | 494 | } |
| 495 | 495 | |
| 496 | 496 | /** |
@@ -501,17 +501,17 @@ discard block |
||
| 501 | 501 | * @return string|null The flag url or null if not available. |
| 502 | 502 | * @since 3.20.0 |
| 503 | 503 | */ |
| 504 | - public static function get_flag_url( $country_code ) { |
|
| 504 | + public static function get_flag_url($country_code) { |
|
| 505 | 505 | |
| 506 | 506 | // Bail out if we don't have the flag. |
| 507 | - if ( ! isset( self::$country_flags[ $country_code ] ) |
|
| 508 | - || ( self::$country_flags[ $country_code ] ) === null ) { |
|
| 507 | + if ( ! isset(self::$country_flags[$country_code]) |
|
| 508 | + || (self::$country_flags[$country_code]) === null) { |
|
| 509 | 509 | return null; |
| 510 | 510 | } |
| 511 | 511 | |
| 512 | - return plugin_dir_url( __DIR__ ) |
|
| 512 | + return plugin_dir_url(__DIR__) |
|
| 513 | 513 | . 'images/flags/16/' |
| 514 | - . self::$country_flags[ $country_code ] |
|
| 514 | + . self::$country_flags[$country_code] |
|
| 515 | 515 | . '.png'; |
| 516 | 516 | } |
| 517 | 517 | |
@@ -523,18 +523,18 @@ discard block |
||
| 523 | 523 | * @return null|string The country name (in English) or null if not found. |
| 524 | 524 | * @since 3.20.0 |
| 525 | 525 | */ |
| 526 | - public static function get_country_name( $country_code ) { |
|
| 526 | + public static function get_country_name($country_code) { |
|
| 527 | 527 | |
| 528 | 528 | /** |
| 529 | 529 | * @since 3.27.6 |
| 530 | 530 | * |
| 531 | 531 | * @see https://github.com/insideout10/wordlift-plugin/issues/1188 |
| 532 | 532 | */ |
| 533 | - if ( ! isset( self::$country_codes[ $country_code ] ) ) { |
|
| 533 | + if ( ! isset(self::$country_codes[$country_code])) { |
|
| 534 | 534 | return ''; |
| 535 | 535 | } |
| 536 | 536 | |
| 537 | - return self::$country_codes[ $country_code ]; |
|
| 537 | + return self::$country_codes[$country_code]; |
|
| 538 | 538 | } |
| 539 | 539 | |
| 540 | 540 | } |
@@ -19,88 +19,88 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class Wordlift_Image_Service { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * The image ratios and sizes. |
|
| 24 | - * |
|
| 25 | - * @since 3.19.4 |
|
| 26 | - * @access public |
|
| 27 | - * @var array $sizes The image ratios and sizes. |
|
| 28 | - */ |
|
| 29 | - public static $sizes = array( |
|
| 30 | - '16x9' => array( 1200, 675 ), |
|
| 31 | - '4x3' => array( 1200, 900 ), |
|
| 32 | - '1x1' => array( 1200, 1200 ), |
|
| 33 | - ); |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Create a {@link Wordlift_Image_Service} instance. |
|
| 37 | - * |
|
| 38 | - * @since 3.19.4 |
|
| 39 | - */ |
|
| 40 | - public function __construct() { |
|
| 41 | - |
|
| 42 | - // Add hook to define the image sizes. Since we're a plugin, we cannot use the |
|
| 43 | - // `after_theme_setup` hook. |
|
| 44 | - add_action( 'init', array( $this, 'after_theme_setup' ) ); |
|
| 45 | - |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Hook `after_theme_setup`: add our own image sizes. |
|
| 50 | - * |
|
| 51 | - * @since 3.19.4 |
|
| 52 | - */ |
|
| 53 | - public function after_theme_setup() { |
|
| 54 | - |
|
| 55 | - foreach ( self::$sizes as $ratio => $sizes ) { |
|
| 56 | - add_image_size( "wl-$ratio", $sizes[0], $sizes[1], true ); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Get the sources for the specified attachment. |
|
| 63 | - * |
|
| 64 | - * @since 3.19.4 |
|
| 65 | - * |
|
| 66 | - * @param int $attachment_id The attachment id. |
|
| 67 | - * |
|
| 68 | - * @return array { |
|
| 69 | - * An array of image sources. |
|
| 70 | - * |
|
| 71 | - * @type string $url The attachment URL. |
|
| 72 | - * @type int $width The attachment width. |
|
| 73 | - * @type int $height The attachment height. |
|
| 74 | - * } |
|
| 75 | - */ |
|
| 76 | - public static function get_sources( $attachment_id ) { |
|
| 77 | - |
|
| 78 | - // Get the source for the specified image sizes. |
|
| 79 | - $sources = array_map( |
|
| 80 | - function ( $ratio ) use ( $attachment_id ) { |
|
| 81 | - |
|
| 82 | - // Get the source of the specified ratio. |
|
| 83 | - $source = wp_get_attachment_image_src( $attachment_id, "wl-$ratio" ); |
|
| 84 | - |
|
| 85 | - // Get the size for the specified ratio. |
|
| 86 | - $size = Wordlift_Image_Service::$sizes[ $ratio ]; |
|
| 87 | - |
|
| 88 | - // Check that the source has an image, and the required size. |
|
| 89 | - if ( empty( $source[0] ) || $size[0] !== $source[1] || $size[1] !== $source[2] ) { |
|
| 90 | - return null; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - // Return the source. |
|
| 94 | - return $source; |
|
| 95 | - }, |
|
| 96 | - array_keys( self::$sizes ) |
|
| 97 | - ); |
|
| 98 | - |
|
| 99 | - // Filter unavailable sources. |
|
| 100 | - $sources_1200 = array_filter( $sources ); |
|
| 101 | - |
|
| 102 | - // Make the results unique. |
|
| 103 | - return $sources_1200; |
|
| 104 | - } |
|
| 22 | + /** |
|
| 23 | + * The image ratios and sizes. |
|
| 24 | + * |
|
| 25 | + * @since 3.19.4 |
|
| 26 | + * @access public |
|
| 27 | + * @var array $sizes The image ratios and sizes. |
|
| 28 | + */ |
|
| 29 | + public static $sizes = array( |
|
| 30 | + '16x9' => array( 1200, 675 ), |
|
| 31 | + '4x3' => array( 1200, 900 ), |
|
| 32 | + '1x1' => array( 1200, 1200 ), |
|
| 33 | + ); |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Create a {@link Wordlift_Image_Service} instance. |
|
| 37 | + * |
|
| 38 | + * @since 3.19.4 |
|
| 39 | + */ |
|
| 40 | + public function __construct() { |
|
| 41 | + |
|
| 42 | + // Add hook to define the image sizes. Since we're a plugin, we cannot use the |
|
| 43 | + // `after_theme_setup` hook. |
|
| 44 | + add_action( 'init', array( $this, 'after_theme_setup' ) ); |
|
| 45 | + |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Hook `after_theme_setup`: add our own image sizes. |
|
| 50 | + * |
|
| 51 | + * @since 3.19.4 |
|
| 52 | + */ |
|
| 53 | + public function after_theme_setup() { |
|
| 54 | + |
|
| 55 | + foreach ( self::$sizes as $ratio => $sizes ) { |
|
| 56 | + add_image_size( "wl-$ratio", $sizes[0], $sizes[1], true ); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Get the sources for the specified attachment. |
|
| 63 | + * |
|
| 64 | + * @since 3.19.4 |
|
| 65 | + * |
|
| 66 | + * @param int $attachment_id The attachment id. |
|
| 67 | + * |
|
| 68 | + * @return array { |
|
| 69 | + * An array of image sources. |
|
| 70 | + * |
|
| 71 | + * @type string $url The attachment URL. |
|
| 72 | + * @type int $width The attachment width. |
|
| 73 | + * @type int $height The attachment height. |
|
| 74 | + * } |
|
| 75 | + */ |
|
| 76 | + public static function get_sources( $attachment_id ) { |
|
| 77 | + |
|
| 78 | + // Get the source for the specified image sizes. |
|
| 79 | + $sources = array_map( |
|
| 80 | + function ( $ratio ) use ( $attachment_id ) { |
|
| 81 | + |
|
| 82 | + // Get the source of the specified ratio. |
|
| 83 | + $source = wp_get_attachment_image_src( $attachment_id, "wl-$ratio" ); |
|
| 84 | + |
|
| 85 | + // Get the size for the specified ratio. |
|
| 86 | + $size = Wordlift_Image_Service::$sizes[ $ratio ]; |
|
| 87 | + |
|
| 88 | + // Check that the source has an image, and the required size. |
|
| 89 | + if ( empty( $source[0] ) || $size[0] !== $source[1] || $size[1] !== $source[2] ) { |
|
| 90 | + return null; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + // Return the source. |
|
| 94 | + return $source; |
|
| 95 | + }, |
|
| 96 | + array_keys( self::$sizes ) |
|
| 97 | + ); |
|
| 98 | + |
|
| 99 | + // Filter unavailable sources. |
|
| 100 | + $sources_1200 = array_filter( $sources ); |
|
| 101 | + |
|
| 102 | + // Make the results unique. |
|
| 103 | + return $sources_1200; |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | 106 | } |
@@ -27,9 +27,9 @@ discard block |
||
| 27 | 27 | * @var array $sizes The image ratios and sizes. |
| 28 | 28 | */ |
| 29 | 29 | public static $sizes = array( |
| 30 | - '16x9' => array( 1200, 675 ), |
|
| 31 | - '4x3' => array( 1200, 900 ), |
|
| 32 | - '1x1' => array( 1200, 1200 ), |
|
| 30 | + '16x9' => array(1200, 675), |
|
| 31 | + '4x3' => array(1200, 900), |
|
| 32 | + '1x1' => array(1200, 1200), |
|
| 33 | 33 | ); |
| 34 | 34 | |
| 35 | 35 | /** |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | |
| 42 | 42 | // Add hook to define the image sizes. Since we're a plugin, we cannot use the |
| 43 | 43 | // `after_theme_setup` hook. |
| 44 | - add_action( 'init', array( $this, 'after_theme_setup' ) ); |
|
| 44 | + add_action('init', array($this, 'after_theme_setup')); |
|
| 45 | 45 | |
| 46 | 46 | } |
| 47 | 47 | |
@@ -52,8 +52,8 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | public function after_theme_setup() { |
| 54 | 54 | |
| 55 | - foreach ( self::$sizes as $ratio => $sizes ) { |
|
| 56 | - add_image_size( "wl-$ratio", $sizes[0], $sizes[1], true ); |
|
| 55 | + foreach (self::$sizes as $ratio => $sizes) { |
|
| 56 | + add_image_size("wl-$ratio", $sizes[0], $sizes[1], true); |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | } |
@@ -73,31 +73,31 @@ discard block |
||
| 73 | 73 | * @type int $height The attachment height. |
| 74 | 74 | * } |
| 75 | 75 | */ |
| 76 | - public static function get_sources( $attachment_id ) { |
|
| 76 | + public static function get_sources($attachment_id) { |
|
| 77 | 77 | |
| 78 | 78 | // Get the source for the specified image sizes. |
| 79 | 79 | $sources = array_map( |
| 80 | - function ( $ratio ) use ( $attachment_id ) { |
|
| 80 | + function($ratio) use ($attachment_id) { |
|
| 81 | 81 | |
| 82 | 82 | // Get the source of the specified ratio. |
| 83 | - $source = wp_get_attachment_image_src( $attachment_id, "wl-$ratio" ); |
|
| 83 | + $source = wp_get_attachment_image_src($attachment_id, "wl-$ratio"); |
|
| 84 | 84 | |
| 85 | 85 | // Get the size for the specified ratio. |
| 86 | - $size = Wordlift_Image_Service::$sizes[ $ratio ]; |
|
| 86 | + $size = Wordlift_Image_Service::$sizes[$ratio]; |
|
| 87 | 87 | |
| 88 | 88 | // Check that the source has an image, and the required size. |
| 89 | - if ( empty( $source[0] ) || $size[0] !== $source[1] || $size[1] !== $source[2] ) { |
|
| 89 | + if (empty($source[0]) || $size[0] !== $source[1] || $size[1] !== $source[2]) { |
|
| 90 | 90 | return null; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | // Return the source. |
| 94 | 94 | return $source; |
| 95 | 95 | }, |
| 96 | - array_keys( self::$sizes ) |
|
| 96 | + array_keys(self::$sizes) |
|
| 97 | 97 | ); |
| 98 | 98 | |
| 99 | 99 | // Filter unavailable sources. |
| 100 | - $sources_1200 = array_filter( $sources ); |
|
| 100 | + $sources_1200 = array_filter($sources); |
|
| 101 | 101 | |
| 102 | 102 | // Make the results unique. |
| 103 | 103 | return $sources_1200; |
@@ -16,187 +16,187 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Attachment_Service { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Create a {@link Wordlift_Attachment_Service} instance. |
|
| 21 | - * |
|
| 22 | - * @since 3.20.0 |
|
| 23 | - */ |
|
| 24 | - protected function __construct() { |
|
| 25 | - |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - private static $instance = null; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Get the singleton instance. |
|
| 32 | - * |
|
| 33 | - * @return \Wordlift_Attachment_Service The singleton instance. |
|
| 34 | - * @since 3.20.0 |
|
| 35 | - */ |
|
| 36 | - public static function get_instance() { |
|
| 37 | - |
|
| 38 | - if ( ! isset( self::$instance ) ) { |
|
| 39 | - self::$instance = new self(); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - return self::$instance; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Get an attachment ID given a URL. |
|
| 47 | - * |
|
| 48 | - * Inspired from https://wpscholar.com/blog/get-attachment-id-from-wp-image-url/ |
|
| 49 | - * |
|
| 50 | - * @param string $url The attachment URL. |
|
| 51 | - * |
|
| 52 | - * @return int|false Attachment ID on success, false on failure |
|
| 53 | - * @since 3.10.0 |
|
| 54 | - */ |
|
| 55 | - public function get_attachment_id( $url ) { |
|
| 56 | - |
|
| 57 | - // Get the upload directory data, we need the base URL to check whether |
|
| 58 | - // the URL we received is within WP. |
|
| 59 | - $dir = wp_upload_dir(); |
|
| 60 | - |
|
| 61 | - // Get the filename, the extension is kept. |
|
| 62 | - if ( 1 !== preg_match( "@^{$dir['baseurl']}/(.+?)(?:-\d+x\d+)?(\.\w+)$@", $url, $matches ) ) { |
|
| 63 | - return false; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - $filename = $matches[1] . $matches[2]; |
|
| 67 | - |
|
| 68 | - // The following query is CPU-intensive, we need to review it. |
|
| 69 | - // |
|
| 70 | - // See https://github.com/insideout10/wordlift-plugin/issues/689. |
|
| 71 | - // |
|
| 72 | - // Query for attachments with the specified filename. |
|
| 73 | - $query = new WP_Query( |
|
| 74 | - array( |
|
| 75 | - 'post_type' => 'attachment', |
|
| 76 | - 'post_status' => 'inherit', |
|
| 77 | - 'fields' => 'ids', |
|
| 78 | - 'meta_query' => array( |
|
| 79 | - array( |
|
| 80 | - 'value' => $filename, |
|
| 81 | - 'compare' => '=', |
|
| 82 | - 'key' => '_wp_attached_file', |
|
| 83 | - ), |
|
| 84 | - ), |
|
| 85 | - 'posts_per_page' => 1, |
|
| 86 | - 'ignore_sticky_posts' => true, |
|
| 87 | - ) |
|
| 88 | - ); |
|
| 89 | - |
|
| 90 | - // If there are no posts, return. |
|
| 91 | - if ( $query->have_posts() ) { |
|
| 92 | - return $query->posts[0]; |
|
| 93 | - // foreach ( $query->posts as $attachment_id ) { |
|
| 94 | - // |
|
| 95 | - // Get the attachment metadata, we need the filename. |
|
| 96 | - // $metadata = wp_get_attachment_metadata( $attachment_id ); |
|
| 97 | - // $original_filename = basename( $metadata['file'] ); |
|
| 98 | - // |
|
| 99 | - // Get the cropped filenames, or an empty array in case there are no files. |
|
| 100 | - // $sizes_filenames = isset( $metadata['sizes'] ) ? wp_list_pluck( $metadata['sizes'], 'file' ) : array(); |
|
| 101 | - // |
|
| 102 | - // If the provided filename matches the attachment filename (or one of its resized images), return the id. |
|
| 103 | - // if ( $original_filename === $filename || in_array( $filename, $sizes_filenames ) ) { |
|
| 104 | - // return $attachment_id; |
|
| 105 | - // } |
|
| 106 | - // } |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - // If we got here, we couldn't find any attachment. |
|
| 110 | - return false; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Get images embedded in the post content. |
|
| 115 | - * |
|
| 116 | - * @param string $content The post content. |
|
| 117 | - * |
|
| 118 | - * @return array An array of attachment ids. |
|
| 119 | - * @since 3.10.0 |
|
| 120 | - */ |
|
| 121 | - public function get_image_embeds( $content ) { |
|
| 122 | - |
|
| 123 | - // Go over all the images included in the post content, check if they are |
|
| 124 | - // in the DB, and if so include them. |
|
| 125 | - $images = array(); |
|
| 126 | - if ( false === preg_match_all( '#<img [^>]*src="([^\\">]*)"[^>]*>#', $content, $images ) ) { |
|
| 127 | - return array(); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - // Map the image URLs to attachment ids. |
|
| 131 | - $that = $this; |
|
| 132 | - $ids = array_map( |
|
| 133 | - function ( $url ) use ( $that ) { |
|
| 134 | - return $that->get_attachment_id( $url ); |
|
| 135 | - }, |
|
| 136 | - $images[1] |
|
| 137 | - ); |
|
| 138 | - |
|
| 139 | - // Filter out not found ids (i.e. id is false). |
|
| 140 | - return array_filter( |
|
| 141 | - $ids, |
|
| 142 | - function ( $item ) { |
|
| 143 | - return false !== $item; |
|
| 144 | - } |
|
| 145 | - ); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Get images linked via the `gallery` shortcode. |
|
| 150 | - * |
|
| 151 | - * @param \WP_Post $post A {@link WP_Post} instance. |
|
| 152 | - * |
|
| 153 | - * @return array An array of attachment ids. |
|
| 154 | - * @since 3.10.0 |
|
| 155 | - */ |
|
| 156 | - public function get_gallery( $post ) { |
|
| 157 | - |
|
| 158 | - // @todo: the `gallery` shortcode has an `exclude` attribute which isn't |
|
| 159 | - // checked at the moment. |
|
| 160 | - |
|
| 161 | - // Prepare the return value. |
|
| 162 | - $ids = array(); |
|
| 163 | - |
|
| 164 | - // As the above for images in galleries. |
|
| 165 | - // Code inspired by http://wordpress.stackexchange.com/questions/80408/how-to-get-page-post-gallery-attachment-images-in-order-they-are-set-in-backend |
|
| 166 | - $pattern = get_shortcode_regex(); |
|
| 167 | - |
|
| 168 | - if ( preg_match_all( '/' . $pattern . '/s', $post->post_content, $matches ) |
|
| 169 | - && array_key_exists( 2, $matches ) |
|
| 170 | - && in_array( 'gallery', $matches[2], true ) |
|
| 171 | - ) { |
|
| 172 | - |
|
| 173 | - $keys = array_keys( $matches[2], 'gallery', true ); |
|
| 174 | - |
|
| 175 | - foreach ( $keys as $key ) { |
|
| 176 | - $atts = shortcode_parse_atts( $matches[3][ $key ] ); |
|
| 177 | - |
|
| 178 | - if ( is_array( $atts ) && array_key_exists( 'ids', $atts ) ) { |
|
| 179 | - // gallery images insert explicitly by their ids. |
|
| 180 | - |
|
| 181 | - foreach ( explode( ',', $atts['ids'] ) as $attachment_id ) { |
|
| 182 | - // Since we do not check for actual image existence |
|
| 183 | - // when generating the json content, check it now. |
|
| 184 | - if ( wp_get_attachment_image_src( $attachment_id, 'full' ) ) { |
|
| 185 | - $ids[ $attachment_id ] = true; |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - } else { |
|
| 189 | - // gallery shortcode with no ids uses all the images |
|
| 190 | - // attached to the post. |
|
| 191 | - $images = get_attached_media( 'image', $post->ID ); |
|
| 192 | - foreach ( $images as $attachment ) { |
|
| 193 | - $ids[ $attachment->ID ] = true; |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - return array_keys( $ids ); |
|
| 200 | - } |
|
| 19 | + /** |
|
| 20 | + * Create a {@link Wordlift_Attachment_Service} instance. |
|
| 21 | + * |
|
| 22 | + * @since 3.20.0 |
|
| 23 | + */ |
|
| 24 | + protected function __construct() { |
|
| 25 | + |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + private static $instance = null; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Get the singleton instance. |
|
| 32 | + * |
|
| 33 | + * @return \Wordlift_Attachment_Service The singleton instance. |
|
| 34 | + * @since 3.20.0 |
|
| 35 | + */ |
|
| 36 | + public static function get_instance() { |
|
| 37 | + |
|
| 38 | + if ( ! isset( self::$instance ) ) { |
|
| 39 | + self::$instance = new self(); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + return self::$instance; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Get an attachment ID given a URL. |
|
| 47 | + * |
|
| 48 | + * Inspired from https://wpscholar.com/blog/get-attachment-id-from-wp-image-url/ |
|
| 49 | + * |
|
| 50 | + * @param string $url The attachment URL. |
|
| 51 | + * |
|
| 52 | + * @return int|false Attachment ID on success, false on failure |
|
| 53 | + * @since 3.10.0 |
|
| 54 | + */ |
|
| 55 | + public function get_attachment_id( $url ) { |
|
| 56 | + |
|
| 57 | + // Get the upload directory data, we need the base URL to check whether |
|
| 58 | + // the URL we received is within WP. |
|
| 59 | + $dir = wp_upload_dir(); |
|
| 60 | + |
|
| 61 | + // Get the filename, the extension is kept. |
|
| 62 | + if ( 1 !== preg_match( "@^{$dir['baseurl']}/(.+?)(?:-\d+x\d+)?(\.\w+)$@", $url, $matches ) ) { |
|
| 63 | + return false; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + $filename = $matches[1] . $matches[2]; |
|
| 67 | + |
|
| 68 | + // The following query is CPU-intensive, we need to review it. |
|
| 69 | + // |
|
| 70 | + // See https://github.com/insideout10/wordlift-plugin/issues/689. |
|
| 71 | + // |
|
| 72 | + // Query for attachments with the specified filename. |
|
| 73 | + $query = new WP_Query( |
|
| 74 | + array( |
|
| 75 | + 'post_type' => 'attachment', |
|
| 76 | + 'post_status' => 'inherit', |
|
| 77 | + 'fields' => 'ids', |
|
| 78 | + 'meta_query' => array( |
|
| 79 | + array( |
|
| 80 | + 'value' => $filename, |
|
| 81 | + 'compare' => '=', |
|
| 82 | + 'key' => '_wp_attached_file', |
|
| 83 | + ), |
|
| 84 | + ), |
|
| 85 | + 'posts_per_page' => 1, |
|
| 86 | + 'ignore_sticky_posts' => true, |
|
| 87 | + ) |
|
| 88 | + ); |
|
| 89 | + |
|
| 90 | + // If there are no posts, return. |
|
| 91 | + if ( $query->have_posts() ) { |
|
| 92 | + return $query->posts[0]; |
|
| 93 | + // foreach ( $query->posts as $attachment_id ) { |
|
| 94 | + // |
|
| 95 | + // Get the attachment metadata, we need the filename. |
|
| 96 | + // $metadata = wp_get_attachment_metadata( $attachment_id ); |
|
| 97 | + // $original_filename = basename( $metadata['file'] ); |
|
| 98 | + // |
|
| 99 | + // Get the cropped filenames, or an empty array in case there are no files. |
|
| 100 | + // $sizes_filenames = isset( $metadata['sizes'] ) ? wp_list_pluck( $metadata['sizes'], 'file' ) : array(); |
|
| 101 | + // |
|
| 102 | + // If the provided filename matches the attachment filename (or one of its resized images), return the id. |
|
| 103 | + // if ( $original_filename === $filename || in_array( $filename, $sizes_filenames ) ) { |
|
| 104 | + // return $attachment_id; |
|
| 105 | + // } |
|
| 106 | + // } |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + // If we got here, we couldn't find any attachment. |
|
| 110 | + return false; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Get images embedded in the post content. |
|
| 115 | + * |
|
| 116 | + * @param string $content The post content. |
|
| 117 | + * |
|
| 118 | + * @return array An array of attachment ids. |
|
| 119 | + * @since 3.10.0 |
|
| 120 | + */ |
|
| 121 | + public function get_image_embeds( $content ) { |
|
| 122 | + |
|
| 123 | + // Go over all the images included in the post content, check if they are |
|
| 124 | + // in the DB, and if so include them. |
|
| 125 | + $images = array(); |
|
| 126 | + if ( false === preg_match_all( '#<img [^>]*src="([^\\">]*)"[^>]*>#', $content, $images ) ) { |
|
| 127 | + return array(); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + // Map the image URLs to attachment ids. |
|
| 131 | + $that = $this; |
|
| 132 | + $ids = array_map( |
|
| 133 | + function ( $url ) use ( $that ) { |
|
| 134 | + return $that->get_attachment_id( $url ); |
|
| 135 | + }, |
|
| 136 | + $images[1] |
|
| 137 | + ); |
|
| 138 | + |
|
| 139 | + // Filter out not found ids (i.e. id is false). |
|
| 140 | + return array_filter( |
|
| 141 | + $ids, |
|
| 142 | + function ( $item ) { |
|
| 143 | + return false !== $item; |
|
| 144 | + } |
|
| 145 | + ); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Get images linked via the `gallery` shortcode. |
|
| 150 | + * |
|
| 151 | + * @param \WP_Post $post A {@link WP_Post} instance. |
|
| 152 | + * |
|
| 153 | + * @return array An array of attachment ids. |
|
| 154 | + * @since 3.10.0 |
|
| 155 | + */ |
|
| 156 | + public function get_gallery( $post ) { |
|
| 157 | + |
|
| 158 | + // @todo: the `gallery` shortcode has an `exclude` attribute which isn't |
|
| 159 | + // checked at the moment. |
|
| 160 | + |
|
| 161 | + // Prepare the return value. |
|
| 162 | + $ids = array(); |
|
| 163 | + |
|
| 164 | + // As the above for images in galleries. |
|
| 165 | + // Code inspired by http://wordpress.stackexchange.com/questions/80408/how-to-get-page-post-gallery-attachment-images-in-order-they-are-set-in-backend |
|
| 166 | + $pattern = get_shortcode_regex(); |
|
| 167 | + |
|
| 168 | + if ( preg_match_all( '/' . $pattern . '/s', $post->post_content, $matches ) |
|
| 169 | + && array_key_exists( 2, $matches ) |
|
| 170 | + && in_array( 'gallery', $matches[2], true ) |
|
| 171 | + ) { |
|
| 172 | + |
|
| 173 | + $keys = array_keys( $matches[2], 'gallery', true ); |
|
| 174 | + |
|
| 175 | + foreach ( $keys as $key ) { |
|
| 176 | + $atts = shortcode_parse_atts( $matches[3][ $key ] ); |
|
| 177 | + |
|
| 178 | + if ( is_array( $atts ) && array_key_exists( 'ids', $atts ) ) { |
|
| 179 | + // gallery images insert explicitly by their ids. |
|
| 180 | + |
|
| 181 | + foreach ( explode( ',', $atts['ids'] ) as $attachment_id ) { |
|
| 182 | + // Since we do not check for actual image existence |
|
| 183 | + // when generating the json content, check it now. |
|
| 184 | + if ( wp_get_attachment_image_src( $attachment_id, 'full' ) ) { |
|
| 185 | + $ids[ $attachment_id ] = true; |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + } else { |
|
| 189 | + // gallery shortcode with no ids uses all the images |
|
| 190 | + // attached to the post. |
|
| 191 | + $images = get_attached_media( 'image', $post->ID ); |
|
| 192 | + foreach ( $images as $attachment ) { |
|
| 193 | + $ids[ $attachment->ID ] = true; |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + return array_keys( $ids ); |
|
| 200 | + } |
|
| 201 | 201 | |
| 202 | 202 | } |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | */ |
| 36 | 36 | public static function get_instance() { |
| 37 | 37 | |
| 38 | - if ( ! isset( self::$instance ) ) { |
|
| 38 | + if ( ! isset(self::$instance)) { |
|
| 39 | 39 | self::$instance = new self(); |
| 40 | 40 | } |
| 41 | 41 | |
@@ -52,18 +52,18 @@ discard block |
||
| 52 | 52 | * @return int|false Attachment ID on success, false on failure |
| 53 | 53 | * @since 3.10.0 |
| 54 | 54 | */ |
| 55 | - public function get_attachment_id( $url ) { |
|
| 55 | + public function get_attachment_id($url) { |
|
| 56 | 56 | |
| 57 | 57 | // Get the upload directory data, we need the base URL to check whether |
| 58 | 58 | // the URL we received is within WP. |
| 59 | 59 | $dir = wp_upload_dir(); |
| 60 | 60 | |
| 61 | 61 | // Get the filename, the extension is kept. |
| 62 | - if ( 1 !== preg_match( "@^{$dir['baseurl']}/(.+?)(?:-\d+x\d+)?(\.\w+)$@", $url, $matches ) ) { |
|
| 62 | + if (1 !== preg_match("@^{$dir['baseurl']}/(.+?)(?:-\d+x\d+)?(\.\w+)$@", $url, $matches)) { |
|
| 63 | 63 | return false; |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | - $filename = $matches[1] . $matches[2]; |
|
| 66 | + $filename = $matches[1].$matches[2]; |
|
| 67 | 67 | |
| 68 | 68 | // The following query is CPU-intensive, we need to review it. |
| 69 | 69 | // |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | ); |
| 89 | 89 | |
| 90 | 90 | // If there are no posts, return. |
| 91 | - if ( $query->have_posts() ) { |
|
| 91 | + if ($query->have_posts()) { |
|
| 92 | 92 | return $query->posts[0]; |
| 93 | 93 | // foreach ( $query->posts as $attachment_id ) { |
| 94 | 94 | // |
@@ -118,20 +118,20 @@ discard block |
||
| 118 | 118 | * @return array An array of attachment ids. |
| 119 | 119 | * @since 3.10.0 |
| 120 | 120 | */ |
| 121 | - public function get_image_embeds( $content ) { |
|
| 121 | + public function get_image_embeds($content) { |
|
| 122 | 122 | |
| 123 | 123 | // Go over all the images included in the post content, check if they are |
| 124 | 124 | // in the DB, and if so include them. |
| 125 | 125 | $images = array(); |
| 126 | - if ( false === preg_match_all( '#<img [^>]*src="([^\\">]*)"[^>]*>#', $content, $images ) ) { |
|
| 126 | + if (false === preg_match_all('#<img [^>]*src="([^\\">]*)"[^>]*>#', $content, $images)) { |
|
| 127 | 127 | return array(); |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | // Map the image URLs to attachment ids. |
| 131 | 131 | $that = $this; |
| 132 | 132 | $ids = array_map( |
| 133 | - function ( $url ) use ( $that ) { |
|
| 134 | - return $that->get_attachment_id( $url ); |
|
| 133 | + function($url) use ($that) { |
|
| 134 | + return $that->get_attachment_id($url); |
|
| 135 | 135 | }, |
| 136 | 136 | $images[1] |
| 137 | 137 | ); |
@@ -139,7 +139,7 @@ discard block |
||
| 139 | 139 | // Filter out not found ids (i.e. id is false). |
| 140 | 140 | return array_filter( |
| 141 | 141 | $ids, |
| 142 | - function ( $item ) { |
|
| 142 | + function($item) { |
|
| 143 | 143 | return false !== $item; |
| 144 | 144 | } |
| 145 | 145 | ); |
@@ -153,7 +153,7 @@ discard block |
||
| 153 | 153 | * @return array An array of attachment ids. |
| 154 | 154 | * @since 3.10.0 |
| 155 | 155 | */ |
| 156 | - public function get_gallery( $post ) { |
|
| 156 | + public function get_gallery($post) { |
|
| 157 | 157 | |
| 158 | 158 | // @todo: the `gallery` shortcode has an `exclude` attribute which isn't |
| 159 | 159 | // checked at the moment. |
@@ -165,38 +165,38 @@ discard block |
||
| 165 | 165 | // Code inspired by http://wordpress.stackexchange.com/questions/80408/how-to-get-page-post-gallery-attachment-images-in-order-they-are-set-in-backend |
| 166 | 166 | $pattern = get_shortcode_regex(); |
| 167 | 167 | |
| 168 | - if ( preg_match_all( '/' . $pattern . '/s', $post->post_content, $matches ) |
|
| 169 | - && array_key_exists( 2, $matches ) |
|
| 170 | - && in_array( 'gallery', $matches[2], true ) |
|
| 168 | + if (preg_match_all('/'.$pattern.'/s', $post->post_content, $matches) |
|
| 169 | + && array_key_exists(2, $matches) |
|
| 170 | + && in_array('gallery', $matches[2], true) |
|
| 171 | 171 | ) { |
| 172 | 172 | |
| 173 | - $keys = array_keys( $matches[2], 'gallery', true ); |
|
| 173 | + $keys = array_keys($matches[2], 'gallery', true); |
|
| 174 | 174 | |
| 175 | - foreach ( $keys as $key ) { |
|
| 176 | - $atts = shortcode_parse_atts( $matches[3][ $key ] ); |
|
| 175 | + foreach ($keys as $key) { |
|
| 176 | + $atts = shortcode_parse_atts($matches[3][$key]); |
|
| 177 | 177 | |
| 178 | - if ( is_array( $atts ) && array_key_exists( 'ids', $atts ) ) { |
|
| 178 | + if (is_array($atts) && array_key_exists('ids', $atts)) { |
|
| 179 | 179 | // gallery images insert explicitly by their ids. |
| 180 | 180 | |
| 181 | - foreach ( explode( ',', $atts['ids'] ) as $attachment_id ) { |
|
| 181 | + foreach (explode(',', $atts['ids']) as $attachment_id) { |
|
| 182 | 182 | // Since we do not check for actual image existence |
| 183 | 183 | // when generating the json content, check it now. |
| 184 | - if ( wp_get_attachment_image_src( $attachment_id, 'full' ) ) { |
|
| 185 | - $ids[ $attachment_id ] = true; |
|
| 184 | + if (wp_get_attachment_image_src($attachment_id, 'full')) { |
|
| 185 | + $ids[$attachment_id] = true; |
|
| 186 | 186 | } |
| 187 | 187 | } |
| 188 | 188 | } else { |
| 189 | 189 | // gallery shortcode with no ids uses all the images |
| 190 | 190 | // attached to the post. |
| 191 | - $images = get_attached_media( 'image', $post->ID ); |
|
| 192 | - foreach ( $images as $attachment ) { |
|
| 193 | - $ids[ $attachment->ID ] = true; |
|
| 191 | + $images = get_attached_media('image', $post->ID); |
|
| 192 | + foreach ($images as $attachment) { |
|
| 193 | + $ids[$attachment->ID] = true; |
|
| 194 | 194 | } |
| 195 | 195 | } |
| 196 | 196 | } |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | - return array_keys( $ids ); |
|
| 199 | + return array_keys($ids); |
|
| 200 | 200 | } |
| 201 | 201 | |
| 202 | 202 | } |
@@ -18,75 +18,75 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Category_Taxonomy_Service { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The {@link Wordlift_Entity_Post_Type_Service} instance. |
|
| 23 | - * |
|
| 24 | - * @since 3.11.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var \Wordlift_Entity_Post_Type_Service $entity_post_type_service The {@link Wordlift_Entity_Post_Type_Service} instance. |
|
| 27 | - */ |
|
| 28 | - private $entity_post_type_service; |
|
| 21 | + /** |
|
| 22 | + * The {@link Wordlift_Entity_Post_Type_Service} instance. |
|
| 23 | + * |
|
| 24 | + * @since 3.11.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var \Wordlift_Entity_Post_Type_Service $entity_post_type_service The {@link Wordlift_Entity_Post_Type_Service} instance. |
|
| 27 | + */ |
|
| 28 | + private $entity_post_type_service; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Create a {@link Wordlift_Category_Taxonomy_Service} instance. |
|
| 32 | - * |
|
| 33 | - * @since 3.11.0 |
|
| 34 | - * |
|
| 35 | - * @param \Wordlift_Entity_Post_Type_Service $entity_post_type_service The {@link Wordlift_Entity_Post_Type_Service} instance. |
|
| 36 | - */ |
|
| 37 | - public function __construct( $entity_post_type_service ) { |
|
| 30 | + /** |
|
| 31 | + * Create a {@link Wordlift_Category_Taxonomy_Service} instance. |
|
| 32 | + * |
|
| 33 | + * @since 3.11.0 |
|
| 34 | + * |
|
| 35 | + * @param \Wordlift_Entity_Post_Type_Service $entity_post_type_service The {@link Wordlift_Entity_Post_Type_Service} instance. |
|
| 36 | + */ |
|
| 37 | + public function __construct( $entity_post_type_service ) { |
|
| 38 | 38 | |
| 39 | - $this->entity_post_type_service = $entity_post_type_service; |
|
| 39 | + $this->entity_post_type_service = $entity_post_type_service; |
|
| 40 | 40 | |
| 41 | - } |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Set the entity post types as one to be included in archive pages. |
|
| 45 | - * |
|
| 46 | - * In order to have entities show up in standard WP categories (Posts categories) |
|
| 47 | - * we configure the `entity` post type, but we also need to alter the main |
|
| 48 | - * WP query (which by default queries posts only) to include the `entities`. |
|
| 49 | - * |
|
| 50 | - * @since 3.11.0 |
|
| 51 | - * |
|
| 52 | - * @param WP_Query $query WP's {@link WP_Query} instance. |
|
| 53 | - */ |
|
| 54 | - public function pre_get_posts( $query ) { |
|
| 43 | + /** |
|
| 44 | + * Set the entity post types as one to be included in archive pages. |
|
| 45 | + * |
|
| 46 | + * In order to have entities show up in standard WP categories (Posts categories) |
|
| 47 | + * we configure the `entity` post type, but we also need to alter the main |
|
| 48 | + * WP query (which by default queries posts only) to include the `entities`. |
|
| 49 | + * |
|
| 50 | + * @since 3.11.0 |
|
| 51 | + * |
|
| 52 | + * @param WP_Query $query WP's {@link WP_Query} instance. |
|
| 53 | + */ |
|
| 54 | + public function pre_get_posts( $query ) { |
|
| 55 | 55 | |
| 56 | - // Only for the main query, avoid problems with widgets and what not. |
|
| 57 | - if ( ! $query->is_main_query() ) { |
|
| 58 | - return; |
|
| 59 | - } |
|
| 56 | + // Only for the main query, avoid problems with widgets and what not. |
|
| 57 | + if ( ! $query->is_main_query() ) { |
|
| 58 | + return; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - // We don't want to alter the query if we're in the admin UI, if this is |
|
| 62 | - // not a category query, or if the `suppress_filters` is set. |
|
| 63 | - // |
|
| 64 | - // Note that it is unlikely for `suppress_filter` to be set on the front |
|
| 65 | - // end, but let's be safe if it is set the calling code assumes no |
|
| 66 | - // modifications of queries. |
|
| 67 | - // |
|
| 68 | - // is_admin is needed, otherwise category based post filters will show |
|
| 69 | - // both types and at the current release (4.7) it causes PHP errors. |
|
| 70 | - if ( is_admin() || ! is_category() || ! empty( $query->query_vars['suppress_filters'] ) ) { |
|
| 71 | - return; |
|
| 72 | - } |
|
| 61 | + // We don't want to alter the query if we're in the admin UI, if this is |
|
| 62 | + // not a category query, or if the `suppress_filters` is set. |
|
| 63 | + // |
|
| 64 | + // Note that it is unlikely for `suppress_filter` to be set on the front |
|
| 65 | + // end, but let's be safe if it is set the calling code assumes no |
|
| 66 | + // modifications of queries. |
|
| 67 | + // |
|
| 68 | + // is_admin is needed, otherwise category based post filters will show |
|
| 69 | + // both types and at the current release (4.7) it causes PHP errors. |
|
| 70 | + if ( is_admin() || ! is_category() || ! empty( $query->query_vars['suppress_filters'] ) ) { |
|
| 71 | + return; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - // Check the current post types, maybe the category archive pages |
|
| 75 | - // are already associated with other post types. |
|
| 76 | - // |
|
| 77 | - // If `post_type` isn't set, WP assumes `post` by default. |
|
| 78 | - $post_types = $query->get( 'post_type' ); |
|
| 79 | - $post_types = (array) ( $post_types ? $post_types : 'post' ); |
|
| 74 | + // Check the current post types, maybe the category archive pages |
|
| 75 | + // are already associated with other post types. |
|
| 76 | + // |
|
| 77 | + // If `post_type` isn't set, WP assumes `post` by default. |
|
| 78 | + $post_types = $query->get( 'post_type' ); |
|
| 79 | + $post_types = (array) ( $post_types ? $post_types : 'post' ); |
|
| 80 | 80 | |
| 81 | - // Add the entities post type only if the post post type is used in the query |
|
| 82 | - // since we only want `entities` to appear alongside posts. |
|
| 83 | - if ( in_array( 'post', $post_types, true ) ) { |
|
| 84 | - $post_types[] = $this->entity_post_type_service->get_post_type(); |
|
| 85 | - } |
|
| 81 | + // Add the entities post type only if the post post type is used in the query |
|
| 82 | + // since we only want `entities` to appear alongside posts. |
|
| 83 | + if ( in_array( 'post', $post_types, true ) ) { |
|
| 84 | + $post_types[] = $this->entity_post_type_service->get_post_type(); |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - // Update the query post types. |
|
| 88 | - $query->set( 'post_type', $post_types ); |
|
| 87 | + // Update the query post types. |
|
| 88 | + $query->set( 'post_type', $post_types ); |
|
| 89 | 89 | |
| 90 | - } |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | 92 | } |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | * |
| 35 | 35 | * @param \Wordlift_Entity_Post_Type_Service $entity_post_type_service The {@link Wordlift_Entity_Post_Type_Service} instance. |
| 36 | 36 | */ |
| 37 | - public function __construct( $entity_post_type_service ) { |
|
| 37 | + public function __construct($entity_post_type_service) { |
|
| 38 | 38 | |
| 39 | 39 | $this->entity_post_type_service = $entity_post_type_service; |
| 40 | 40 | |
@@ -51,10 +51,10 @@ discard block |
||
| 51 | 51 | * |
| 52 | 52 | * @param WP_Query $query WP's {@link WP_Query} instance. |
| 53 | 53 | */ |
| 54 | - public function pre_get_posts( $query ) { |
|
| 54 | + public function pre_get_posts($query) { |
|
| 55 | 55 | |
| 56 | 56 | // Only for the main query, avoid problems with widgets and what not. |
| 57 | - if ( ! $query->is_main_query() ) { |
|
| 57 | + if ( ! $query->is_main_query()) { |
|
| 58 | 58 | return; |
| 59 | 59 | } |
| 60 | 60 | |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | // |
| 68 | 68 | // is_admin is needed, otherwise category based post filters will show |
| 69 | 69 | // both types and at the current release (4.7) it causes PHP errors. |
| 70 | - if ( is_admin() || ! is_category() || ! empty( $query->query_vars['suppress_filters'] ) ) { |
|
| 70 | + if (is_admin() || ! is_category() || ! empty($query->query_vars['suppress_filters'])) { |
|
| 71 | 71 | return; |
| 72 | 72 | } |
| 73 | 73 | |
@@ -75,17 +75,17 @@ discard block |
||
| 75 | 75 | // are already associated with other post types. |
| 76 | 76 | // |
| 77 | 77 | // If `post_type` isn't set, WP assumes `post` by default. |
| 78 | - $post_types = $query->get( 'post_type' ); |
|
| 79 | - $post_types = (array) ( $post_types ? $post_types : 'post' ); |
|
| 78 | + $post_types = $query->get('post_type'); |
|
| 79 | + $post_types = (array) ($post_types ? $post_types : 'post'); |
|
| 80 | 80 | |
| 81 | 81 | // Add the entities post type only if the post post type is used in the query |
| 82 | 82 | // since we only want `entities` to appear alongside posts. |
| 83 | - if ( in_array( 'post', $post_types, true ) ) { |
|
| 83 | + if (in_array('post', $post_types, true)) { |
|
| 84 | 84 | $post_types[] = $this->entity_post_type_service->get_post_type(); |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | // Update the query post types. |
| 88 | - $query->set( 'post_type', $post_types ); |
|
| 88 | + $query->set('post_type', $post_types); |
|
| 89 | 89 | |
| 90 | 90 | } |
| 91 | 91 | |
@@ -20,146 +20,146 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | class Wordlift_Entity_Type_Adapter { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * A {@link Wordlift_Log_Service} instance. |
|
| 25 | - * |
|
| 26 | - * @since 3.15.0 |
|
| 27 | - * @access private |
|
| 28 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 29 | - */ |
|
| 30 | - private $log; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * The {@link Wordlift_Entity_Type_Service} instance. |
|
| 34 | - * |
|
| 35 | - * @since 3.15.0 |
|
| 36 | - * @access private |
|
| 37 | - * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 38 | - */ |
|
| 39 | - private $entity_type_service; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Create a {@link Wordlift_Entity_Type_Adapter} instance. |
|
| 43 | - * |
|
| 44 | - * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 45 | - * |
|
| 46 | - * @since 3.15.0 |
|
| 47 | - */ |
|
| 48 | - public function __construct( $entity_type_service ) { |
|
| 49 | - |
|
| 50 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Adapter' ); |
|
| 51 | - |
|
| 52 | - $this->entity_type_service = $entity_type_service; |
|
| 53 | - |
|
| 54 | - add_filter( |
|
| 55 | - 'wl_default_entity_type_for_post_type', |
|
| 56 | - array( |
|
| 57 | - $this, |
|
| 58 | - 'default_entity_type_callback', |
|
| 59 | - ), |
|
| 60 | - 10, |
|
| 61 | - 2 |
|
| 62 | - ); |
|
| 63 | - |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @param $entity_type string Entity type |
|
| 68 | - * @param $post_type string The post type |
|
| 69 | - * |
|
| 70 | - * @return string The default entity type depending on the post type. |
|
| 71 | - */ |
|
| 72 | - public function default_entity_type_callback( $entity_type, $post_type ) { |
|
| 73 | - if ( 'product' === $post_type ) { |
|
| 74 | - return 'http://schema.org/Product'; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - return $entity_type; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Hook to `save_post`. |
|
| 82 | - * |
|
| 83 | - * This function is called when the `save_post` action is run. The |
|
| 84 | - * function checks whether an Entity Type term (from the Entity Types |
|
| 85 | - * taxonomy) is already assigned and, if not, it calls {@link Wordlift_Entity_Type_Service} |
|
| 86 | - * to set the default taxonomy term. |
|
| 87 | - * |
|
| 88 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 89 | - * @param WP_Post $post The {@link WP_Post} instance. |
|
| 90 | - * |
|
| 91 | - * @since 3.15.0 |
|
| 92 | - */ |
|
| 93 | - public function save_post( $post_id, $post ) { |
|
| 94 | - |
|
| 95 | - if ( ! Wordlift_Entity_Type_Service::is_valid_entity_post_type( $post->post_type ) ) { |
|
| 96 | - $this->log->debug( "Ignoring `{$post->post_type}` post type." ); |
|
| 97 | - |
|
| 98 | - // Bail out if the post can not be an entity. |
|
| 99 | - return; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - // Bail out if the post already has an entity type. |
|
| 103 | - if ( apply_filters( 'wl_entity_type_adapter__save_post__has_entity_type', $this->entity_type_service->has_entity_type( $post_id ) ) ) { |
|
| 104 | - $this->log->debug( "Post $post_id has already an entity type." ); |
|
| 105 | - |
|
| 106 | - return; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - $this->log->debug( "Setting `Article` entity type for post $post_id." ); |
|
| 110 | - |
|
| 111 | - // Finally set the default entity type. |
|
| 112 | - |
|
| 113 | - // For entities use a Thing |
|
| 114 | - if ( 'entity' === $post->post_type ) { |
|
| 115 | - $this->entity_type_service->set( $post_id, 'http://schema.org/Thing' ); |
|
| 116 | - } else { |
|
| 117 | - // Get the entity types. |
|
| 118 | - $entity_types = self::get_entity_types( $post->post_type ); |
|
| 119 | - |
|
| 120 | - // Set the entity type. |
|
| 121 | - foreach ( $entity_types as $entity_type ) { |
|
| 122 | - $this->entity_type_service->set( $post_id, $entity_type, false ); |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Get the entity types for a post type. |
|
| 130 | - * |
|
| 131 | - * @param string $post_type The post type. |
|
| 132 | - * |
|
| 133 | - * @return array An array of entity types. |
|
| 134 | - * @since 3.20.0 |
|
| 135 | - */ |
|
| 136 | - public static function get_entity_types( $post_type ) { |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Get the default entity type. |
|
| 140 | - * |
|
| 141 | - * @param string $entity_type The preset entity type. |
|
| 142 | - * @param string $post_type The post type. |
|
| 143 | - * |
|
| 144 | - * @since 3.20.0 |
|
| 145 | - */ |
|
| 146 | - $default_entity_type = apply_filters( 'wl_default_entity_type_for_post_type', 'http://schema.org/Article', $post_type ); |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Get the default entity types. |
|
| 150 | - * |
|
| 151 | - * Adding support to assign more than one entity type. |
|
| 152 | - * |
|
| 153 | - * @param array $entity_types The default entity types. |
|
| 154 | - * @param string $post_type The post type. |
|
| 155 | - * |
|
| 156 | - * @since 3.20.0 |
|
| 157 | - * |
|
| 158 | - * @see Wordlift_Mapping_Service |
|
| 159 | - */ |
|
| 160 | - $entity_types = apply_filters( 'wl_default_entity_types_for_post_type', array( $default_entity_type ), $post_type ); |
|
| 161 | - |
|
| 162 | - return $entity_types; |
|
| 163 | - } |
|
| 23 | + /** |
|
| 24 | + * A {@link Wordlift_Log_Service} instance. |
|
| 25 | + * |
|
| 26 | + * @since 3.15.0 |
|
| 27 | + * @access private |
|
| 28 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 29 | + */ |
|
| 30 | + private $log; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * The {@link Wordlift_Entity_Type_Service} instance. |
|
| 34 | + * |
|
| 35 | + * @since 3.15.0 |
|
| 36 | + * @access private |
|
| 37 | + * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 38 | + */ |
|
| 39 | + private $entity_type_service; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Create a {@link Wordlift_Entity_Type_Adapter} instance. |
|
| 43 | + * |
|
| 44 | + * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 45 | + * |
|
| 46 | + * @since 3.15.0 |
|
| 47 | + */ |
|
| 48 | + public function __construct( $entity_type_service ) { |
|
| 49 | + |
|
| 50 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Adapter' ); |
|
| 51 | + |
|
| 52 | + $this->entity_type_service = $entity_type_service; |
|
| 53 | + |
|
| 54 | + add_filter( |
|
| 55 | + 'wl_default_entity_type_for_post_type', |
|
| 56 | + array( |
|
| 57 | + $this, |
|
| 58 | + 'default_entity_type_callback', |
|
| 59 | + ), |
|
| 60 | + 10, |
|
| 61 | + 2 |
|
| 62 | + ); |
|
| 63 | + |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @param $entity_type string Entity type |
|
| 68 | + * @param $post_type string The post type |
|
| 69 | + * |
|
| 70 | + * @return string The default entity type depending on the post type. |
|
| 71 | + */ |
|
| 72 | + public function default_entity_type_callback( $entity_type, $post_type ) { |
|
| 73 | + if ( 'product' === $post_type ) { |
|
| 74 | + return 'http://schema.org/Product'; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + return $entity_type; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Hook to `save_post`. |
|
| 82 | + * |
|
| 83 | + * This function is called when the `save_post` action is run. The |
|
| 84 | + * function checks whether an Entity Type term (from the Entity Types |
|
| 85 | + * taxonomy) is already assigned and, if not, it calls {@link Wordlift_Entity_Type_Service} |
|
| 86 | + * to set the default taxonomy term. |
|
| 87 | + * |
|
| 88 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 89 | + * @param WP_Post $post The {@link WP_Post} instance. |
|
| 90 | + * |
|
| 91 | + * @since 3.15.0 |
|
| 92 | + */ |
|
| 93 | + public function save_post( $post_id, $post ) { |
|
| 94 | + |
|
| 95 | + if ( ! Wordlift_Entity_Type_Service::is_valid_entity_post_type( $post->post_type ) ) { |
|
| 96 | + $this->log->debug( "Ignoring `{$post->post_type}` post type." ); |
|
| 97 | + |
|
| 98 | + // Bail out if the post can not be an entity. |
|
| 99 | + return; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + // Bail out if the post already has an entity type. |
|
| 103 | + if ( apply_filters( 'wl_entity_type_adapter__save_post__has_entity_type', $this->entity_type_service->has_entity_type( $post_id ) ) ) { |
|
| 104 | + $this->log->debug( "Post $post_id has already an entity type." ); |
|
| 105 | + |
|
| 106 | + return; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + $this->log->debug( "Setting `Article` entity type for post $post_id." ); |
|
| 110 | + |
|
| 111 | + // Finally set the default entity type. |
|
| 112 | + |
|
| 113 | + // For entities use a Thing |
|
| 114 | + if ( 'entity' === $post->post_type ) { |
|
| 115 | + $this->entity_type_service->set( $post_id, 'http://schema.org/Thing' ); |
|
| 116 | + } else { |
|
| 117 | + // Get the entity types. |
|
| 118 | + $entity_types = self::get_entity_types( $post->post_type ); |
|
| 119 | + |
|
| 120 | + // Set the entity type. |
|
| 121 | + foreach ( $entity_types as $entity_type ) { |
|
| 122 | + $this->entity_type_service->set( $post_id, $entity_type, false ); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Get the entity types for a post type. |
|
| 130 | + * |
|
| 131 | + * @param string $post_type The post type. |
|
| 132 | + * |
|
| 133 | + * @return array An array of entity types. |
|
| 134 | + * @since 3.20.0 |
|
| 135 | + */ |
|
| 136 | + public static function get_entity_types( $post_type ) { |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Get the default entity type. |
|
| 140 | + * |
|
| 141 | + * @param string $entity_type The preset entity type. |
|
| 142 | + * @param string $post_type The post type. |
|
| 143 | + * |
|
| 144 | + * @since 3.20.0 |
|
| 145 | + */ |
|
| 146 | + $default_entity_type = apply_filters( 'wl_default_entity_type_for_post_type', 'http://schema.org/Article', $post_type ); |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Get the default entity types. |
|
| 150 | + * |
|
| 151 | + * Adding support to assign more than one entity type. |
|
| 152 | + * |
|
| 153 | + * @param array $entity_types The default entity types. |
|
| 154 | + * @param string $post_type The post type. |
|
| 155 | + * |
|
| 156 | + * @since 3.20.0 |
|
| 157 | + * |
|
| 158 | + * @see Wordlift_Mapping_Service |
|
| 159 | + */ |
|
| 160 | + $entity_types = apply_filters( 'wl_default_entity_types_for_post_type', array( $default_entity_type ), $post_type ); |
|
| 161 | + |
|
| 162 | + return $entity_types; |
|
| 163 | + } |
|
| 164 | 164 | |
| 165 | 165 | } |
@@ -45,9 +45,9 @@ discard block |
||
| 45 | 45 | * |
| 46 | 46 | * @since 3.15.0 |
| 47 | 47 | */ |
| 48 | - public function __construct( $entity_type_service ) { |
|
| 48 | + public function __construct($entity_type_service) { |
|
| 49 | 49 | |
| 50 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Adapter' ); |
|
| 50 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Type_Adapter'); |
|
| 51 | 51 | |
| 52 | 52 | $this->entity_type_service = $entity_type_service; |
| 53 | 53 | |
@@ -69,8 +69,8 @@ discard block |
||
| 69 | 69 | * |
| 70 | 70 | * @return string The default entity type depending on the post type. |
| 71 | 71 | */ |
| 72 | - public function default_entity_type_callback( $entity_type, $post_type ) { |
|
| 73 | - if ( 'product' === $post_type ) { |
|
| 72 | + public function default_entity_type_callback($entity_type, $post_type) { |
|
| 73 | + if ('product' === $post_type) { |
|
| 74 | 74 | return 'http://schema.org/Product'; |
| 75 | 75 | } |
| 76 | 76 | |
@@ -90,36 +90,36 @@ discard block |
||
| 90 | 90 | * |
| 91 | 91 | * @since 3.15.0 |
| 92 | 92 | */ |
| 93 | - public function save_post( $post_id, $post ) { |
|
| 93 | + public function save_post($post_id, $post) { |
|
| 94 | 94 | |
| 95 | - if ( ! Wordlift_Entity_Type_Service::is_valid_entity_post_type( $post->post_type ) ) { |
|
| 96 | - $this->log->debug( "Ignoring `{$post->post_type}` post type." ); |
|
| 95 | + if ( ! Wordlift_Entity_Type_Service::is_valid_entity_post_type($post->post_type)) { |
|
| 96 | + $this->log->debug("Ignoring `{$post->post_type}` post type."); |
|
| 97 | 97 | |
| 98 | 98 | // Bail out if the post can not be an entity. |
| 99 | 99 | return; |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | // Bail out if the post already has an entity type. |
| 103 | - if ( apply_filters( 'wl_entity_type_adapter__save_post__has_entity_type', $this->entity_type_service->has_entity_type( $post_id ) ) ) { |
|
| 104 | - $this->log->debug( "Post $post_id has already an entity type." ); |
|
| 103 | + if (apply_filters('wl_entity_type_adapter__save_post__has_entity_type', $this->entity_type_service->has_entity_type($post_id))) { |
|
| 104 | + $this->log->debug("Post $post_id has already an entity type."); |
|
| 105 | 105 | |
| 106 | 106 | return; |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - $this->log->debug( "Setting `Article` entity type for post $post_id." ); |
|
| 109 | + $this->log->debug("Setting `Article` entity type for post $post_id."); |
|
| 110 | 110 | |
| 111 | 111 | // Finally set the default entity type. |
| 112 | 112 | |
| 113 | 113 | // For entities use a Thing |
| 114 | - if ( 'entity' === $post->post_type ) { |
|
| 115 | - $this->entity_type_service->set( $post_id, 'http://schema.org/Thing' ); |
|
| 114 | + if ('entity' === $post->post_type) { |
|
| 115 | + $this->entity_type_service->set($post_id, 'http://schema.org/Thing'); |
|
| 116 | 116 | } else { |
| 117 | 117 | // Get the entity types. |
| 118 | - $entity_types = self::get_entity_types( $post->post_type ); |
|
| 118 | + $entity_types = self::get_entity_types($post->post_type); |
|
| 119 | 119 | |
| 120 | 120 | // Set the entity type. |
| 121 | - foreach ( $entity_types as $entity_type ) { |
|
| 122 | - $this->entity_type_service->set( $post_id, $entity_type, false ); |
|
| 121 | + foreach ($entity_types as $entity_type) { |
|
| 122 | + $this->entity_type_service->set($post_id, $entity_type, false); |
|
| 123 | 123 | } |
| 124 | 124 | } |
| 125 | 125 | |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | * @return array An array of entity types. |
| 134 | 134 | * @since 3.20.0 |
| 135 | 135 | */ |
| 136 | - public static function get_entity_types( $post_type ) { |
|
| 136 | + public static function get_entity_types($post_type) { |
|
| 137 | 137 | |
| 138 | 138 | /** |
| 139 | 139 | * Get the default entity type. |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | * |
| 144 | 144 | * @since 3.20.0 |
| 145 | 145 | */ |
| 146 | - $default_entity_type = apply_filters( 'wl_default_entity_type_for_post_type', 'http://schema.org/Article', $post_type ); |
|
| 146 | + $default_entity_type = apply_filters('wl_default_entity_type_for_post_type', 'http://schema.org/Article', $post_type); |
|
| 147 | 147 | |
| 148 | 148 | /** |
| 149 | 149 | * Get the default entity types. |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | * |
| 158 | 158 | * @see Wordlift_Mapping_Service |
| 159 | 159 | */ |
| 160 | - $entity_types = apply_filters( 'wl_default_entity_types_for_post_type', array( $default_entity_type ), $post_type ); |
|
| 160 | + $entity_types = apply_filters('wl_default_entity_types_for_post_type', array($default_entity_type), $post_type); |
|
| 161 | 161 | |
| 162 | 162 | return $entity_types; |
| 163 | 163 | } |
@@ -21,145 +21,145 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_Deactivator_Feedback { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * A {@link Wordlift_Log_Service} instance. |
|
| 26 | - * |
|
| 27 | - * @since 3.19.0 |
|
| 28 | - * @access private |
|
| 29 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 30 | - */ |
|
| 31 | - private $log; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Wordlift_Deactivator_Feedback constructor. |
|
| 35 | - * |
|
| 36 | - * @since 3.19.0 |
|
| 37 | - */ |
|
| 38 | - public function __construct() { |
|
| 39 | - |
|
| 40 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Deactivator_Feedback' ); |
|
| 41 | - |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Checks whether we have permissions to show the popup. |
|
| 46 | - * |
|
| 47 | - * @return bool `true` if we have permissions, false otherwise. |
|
| 48 | - * @version 3.19.0 |
|
| 49 | - */ |
|
| 50 | - private function has_permission_to_show_popup() { |
|
| 51 | - // Get the current page. |
|
| 52 | - global $pagenow; |
|
| 53 | - |
|
| 54 | - // Bail if the user doesn't have permissions |
|
| 55 | - // or if it's not the plugins page. |
|
| 56 | - if ( 'plugins.php' !== $pagenow ) { |
|
| 57 | - return false; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - // Get the user preferences. We shouldn't show the feedback popup |
|
| 61 | - // if we don't have permissions for that. |
|
| 62 | - $user_preferences = Wordlift_Configuration_Service::get_instance()->get_diagnostic_preferences(); |
|
| 63 | - |
|
| 64 | - // Bail. We don't have preferences to show the popup. |
|
| 65 | - if ( 'yes' !== $user_preferences ) { |
|
| 66 | - return false; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - return true; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Render the feedback popup in the footer. |
|
| 74 | - * |
|
| 75 | - * @return void |
|
| 76 | - * @version 3.19.0 |
|
| 77 | - */ |
|
| 78 | - public function render_feedback_popup() { |
|
| 79 | - // Bail if we don't have permissions to show the popup. |
|
| 80 | - if ( ! $this->has_permission_to_show_popup() ) { |
|
| 81 | - return; |
|
| 82 | - } |
|
| 83 | - // Include the partial. |
|
| 84 | - include plugin_dir_path( __FILE__ ) . '../admin/partials/wordlift-admin-deactivation-feedback-popup.php'; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Enqueue required popup scripts and styles. |
|
| 89 | - * |
|
| 90 | - * @return void |
|
| 91 | - * @version 3.19.0 |
|
| 92 | - */ |
|
| 93 | - public function enqueue_popup_scripts() { |
|
| 94 | - // Bail if we don't have permissions to show the popup. |
|
| 95 | - if ( ! $this->has_permission_to_show_popup() ) { |
|
| 96 | - return; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - wp_enqueue_style( 'wordlift-admin-feedback-popup', plugin_dir_url( __DIR__ ) . 'admin/css/wordlift-admin-feedback-popup.css', array(), WORDLIFT_VERSION ); |
|
| 100 | - wp_enqueue_script( 'wordlift-admin-feedback-popup', plugin_dir_url( __DIR__ ) . 'admin/js/wordlift-admin-feedback-popup.js', array( 'jquery' ), WORDLIFT_VERSION, false ); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Handle the deactivation ajax call |
|
| 105 | - * and perform a request to external server. |
|
| 106 | - * |
|
| 107 | - * @return void |
|
| 108 | - * @version 3.19.0 |
|
| 109 | - */ |
|
| 110 | - public function wl_deactivation_feedback() { |
|
| 111 | - // Bail if the nonce is not valid. |
|
| 112 | - if ( |
|
| 113 | - empty( $_POST['wl_deactivation_feedback_nonce'] ) || // The nonce doens't exists. |
|
| 114 | - ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wl_deactivation_feedback_nonce'] ) ), 'wl_deactivation_feedback_nonce' ) // The nonce is invalid. |
|
| 115 | - ) { |
|
| 116 | - wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - // We allow user to deactivate without providing a reason |
|
| 120 | - // so bail and send success response. |
|
| 121 | - if ( empty( $_POST['code'] ) ) { |
|
| 122 | - wp_send_json_success(); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - $plugin_data = get_plugin_data( plugin_dir_path( __DIR__ ) . 'wordlift.php', false, false ); |
|
| 126 | - |
|
| 127 | - // Prepare the options. |
|
| 128 | - $options = array( |
|
| 129 | - // The deactivation reason. |
|
| 130 | - 'code' => sanitize_text_field( wp_unslash( (string) $_POST['code'] ) ), |
|
| 131 | - // Additional information if provided. |
|
| 132 | - 'details' => ( ! empty( $_POST['details'] ) ) ? sanitize_text_field( wp_unslash( (string) $_POST['details'] ) ) : '', |
|
| 133 | - // The website url. |
|
| 134 | - 'url' => get_bloginfo( 'url' ), |
|
| 135 | - // WP version. |
|
| 136 | - 'wordpressVersion' => get_bloginfo( 'version' ), |
|
| 137 | - // WL version. |
|
| 138 | - 'wordliftVersion' => $plugin_data['Version'], |
|
| 139 | - // The admin email. |
|
| 140 | - 'email' => get_bloginfo( 'admin_email' ), |
|
| 141 | - ); |
|
| 142 | - |
|
| 143 | - $response = wp_remote_post( |
|
| 144 | - Wordlift_Configuration_Service::get_instance()->get_deactivation_feedback_url(), |
|
| 145 | - array( |
|
| 146 | - 'method' => 'POST', |
|
| 147 | - 'body' => wp_json_encode( $options ), |
|
| 148 | - 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), |
|
| 149 | - ) |
|
| 150 | - ); |
|
| 151 | - |
|
| 152 | - $code = wp_remote_retrieve_response_code( $response ); |
|
| 153 | - $message = wp_remote_retrieve_response_message( $response ); |
|
| 154 | - |
|
| 155 | - // Add message to the error log if the response code is not 200. |
|
| 156 | - if ( 201 !== $code ) { |
|
| 157 | - // Write the error in the logs. |
|
| 158 | - $this->log->error( 'An error occurred while requesting a feedback endpoint error_code: ' . $code . ' message: ' . $message ); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - // We should send success message even when the feedback is not |
|
| 162 | - // send, because otherwise the plugin cannot be deactivated. |
|
| 163 | - wp_send_json_success(); |
|
| 164 | - } |
|
| 24 | + /** |
|
| 25 | + * A {@link Wordlift_Log_Service} instance. |
|
| 26 | + * |
|
| 27 | + * @since 3.19.0 |
|
| 28 | + * @access private |
|
| 29 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 30 | + */ |
|
| 31 | + private $log; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Wordlift_Deactivator_Feedback constructor. |
|
| 35 | + * |
|
| 36 | + * @since 3.19.0 |
|
| 37 | + */ |
|
| 38 | + public function __construct() { |
|
| 39 | + |
|
| 40 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Deactivator_Feedback' ); |
|
| 41 | + |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Checks whether we have permissions to show the popup. |
|
| 46 | + * |
|
| 47 | + * @return bool `true` if we have permissions, false otherwise. |
|
| 48 | + * @version 3.19.0 |
|
| 49 | + */ |
|
| 50 | + private function has_permission_to_show_popup() { |
|
| 51 | + // Get the current page. |
|
| 52 | + global $pagenow; |
|
| 53 | + |
|
| 54 | + // Bail if the user doesn't have permissions |
|
| 55 | + // or if it's not the plugins page. |
|
| 56 | + if ( 'plugins.php' !== $pagenow ) { |
|
| 57 | + return false; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + // Get the user preferences. We shouldn't show the feedback popup |
|
| 61 | + // if we don't have permissions for that. |
|
| 62 | + $user_preferences = Wordlift_Configuration_Service::get_instance()->get_diagnostic_preferences(); |
|
| 63 | + |
|
| 64 | + // Bail. We don't have preferences to show the popup. |
|
| 65 | + if ( 'yes' !== $user_preferences ) { |
|
| 66 | + return false; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + return true; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Render the feedback popup in the footer. |
|
| 74 | + * |
|
| 75 | + * @return void |
|
| 76 | + * @version 3.19.0 |
|
| 77 | + */ |
|
| 78 | + public function render_feedback_popup() { |
|
| 79 | + // Bail if we don't have permissions to show the popup. |
|
| 80 | + if ( ! $this->has_permission_to_show_popup() ) { |
|
| 81 | + return; |
|
| 82 | + } |
|
| 83 | + // Include the partial. |
|
| 84 | + include plugin_dir_path( __FILE__ ) . '../admin/partials/wordlift-admin-deactivation-feedback-popup.php'; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Enqueue required popup scripts and styles. |
|
| 89 | + * |
|
| 90 | + * @return void |
|
| 91 | + * @version 3.19.0 |
|
| 92 | + */ |
|
| 93 | + public function enqueue_popup_scripts() { |
|
| 94 | + // Bail if we don't have permissions to show the popup. |
|
| 95 | + if ( ! $this->has_permission_to_show_popup() ) { |
|
| 96 | + return; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + wp_enqueue_style( 'wordlift-admin-feedback-popup', plugin_dir_url( __DIR__ ) . 'admin/css/wordlift-admin-feedback-popup.css', array(), WORDLIFT_VERSION ); |
|
| 100 | + wp_enqueue_script( 'wordlift-admin-feedback-popup', plugin_dir_url( __DIR__ ) . 'admin/js/wordlift-admin-feedback-popup.js', array( 'jquery' ), WORDLIFT_VERSION, false ); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Handle the deactivation ajax call |
|
| 105 | + * and perform a request to external server. |
|
| 106 | + * |
|
| 107 | + * @return void |
|
| 108 | + * @version 3.19.0 |
|
| 109 | + */ |
|
| 110 | + public function wl_deactivation_feedback() { |
|
| 111 | + // Bail if the nonce is not valid. |
|
| 112 | + if ( |
|
| 113 | + empty( $_POST['wl_deactivation_feedback_nonce'] ) || // The nonce doens't exists. |
|
| 114 | + ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wl_deactivation_feedback_nonce'] ) ), 'wl_deactivation_feedback_nonce' ) // The nonce is invalid. |
|
| 115 | + ) { |
|
| 116 | + wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + // We allow user to deactivate without providing a reason |
|
| 120 | + // so bail and send success response. |
|
| 121 | + if ( empty( $_POST['code'] ) ) { |
|
| 122 | + wp_send_json_success(); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + $plugin_data = get_plugin_data( plugin_dir_path( __DIR__ ) . 'wordlift.php', false, false ); |
|
| 126 | + |
|
| 127 | + // Prepare the options. |
|
| 128 | + $options = array( |
|
| 129 | + // The deactivation reason. |
|
| 130 | + 'code' => sanitize_text_field( wp_unslash( (string) $_POST['code'] ) ), |
|
| 131 | + // Additional information if provided. |
|
| 132 | + 'details' => ( ! empty( $_POST['details'] ) ) ? sanitize_text_field( wp_unslash( (string) $_POST['details'] ) ) : '', |
|
| 133 | + // The website url. |
|
| 134 | + 'url' => get_bloginfo( 'url' ), |
|
| 135 | + // WP version. |
|
| 136 | + 'wordpressVersion' => get_bloginfo( 'version' ), |
|
| 137 | + // WL version. |
|
| 138 | + 'wordliftVersion' => $plugin_data['Version'], |
|
| 139 | + // The admin email. |
|
| 140 | + 'email' => get_bloginfo( 'admin_email' ), |
|
| 141 | + ); |
|
| 142 | + |
|
| 143 | + $response = wp_remote_post( |
|
| 144 | + Wordlift_Configuration_Service::get_instance()->get_deactivation_feedback_url(), |
|
| 145 | + array( |
|
| 146 | + 'method' => 'POST', |
|
| 147 | + 'body' => wp_json_encode( $options ), |
|
| 148 | + 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), |
|
| 149 | + ) |
|
| 150 | + ); |
|
| 151 | + |
|
| 152 | + $code = wp_remote_retrieve_response_code( $response ); |
|
| 153 | + $message = wp_remote_retrieve_response_message( $response ); |
|
| 154 | + |
|
| 155 | + // Add message to the error log if the response code is not 200. |
|
| 156 | + if ( 201 !== $code ) { |
|
| 157 | + // Write the error in the logs. |
|
| 158 | + $this->log->error( 'An error occurred while requesting a feedback endpoint error_code: ' . $code . ' message: ' . $message ); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + // We should send success message even when the feedback is not |
|
| 162 | + // send, because otherwise the plugin cannot be deactivated. |
|
| 163 | + wp_send_json_success(); |
|
| 164 | + } |
|
| 165 | 165 | } |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | */ |
| 38 | 38 | public function __construct() { |
| 39 | 39 | |
| 40 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Deactivator_Feedback' ); |
|
| 40 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Deactivator_Feedback'); |
|
| 41 | 41 | |
| 42 | 42 | } |
| 43 | 43 | |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | |
| 54 | 54 | // Bail if the user doesn't have permissions |
| 55 | 55 | // or if it's not the plugins page. |
| 56 | - if ( 'plugins.php' !== $pagenow ) { |
|
| 56 | + if ('plugins.php' !== $pagenow) { |
|
| 57 | 57 | return false; |
| 58 | 58 | } |
| 59 | 59 | |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | $user_preferences = Wordlift_Configuration_Service::get_instance()->get_diagnostic_preferences(); |
| 63 | 63 | |
| 64 | 64 | // Bail. We don't have preferences to show the popup. |
| 65 | - if ( 'yes' !== $user_preferences ) { |
|
| 65 | + if ('yes' !== $user_preferences) { |
|
| 66 | 66 | return false; |
| 67 | 67 | } |
| 68 | 68 | |
@@ -77,11 +77,11 @@ discard block |
||
| 77 | 77 | */ |
| 78 | 78 | public function render_feedback_popup() { |
| 79 | 79 | // Bail if we don't have permissions to show the popup. |
| 80 | - if ( ! $this->has_permission_to_show_popup() ) { |
|
| 80 | + if ( ! $this->has_permission_to_show_popup()) { |
|
| 81 | 81 | return; |
| 82 | 82 | } |
| 83 | 83 | // Include the partial. |
| 84 | - include plugin_dir_path( __FILE__ ) . '../admin/partials/wordlift-admin-deactivation-feedback-popup.php'; |
|
| 84 | + include plugin_dir_path(__FILE__).'../admin/partials/wordlift-admin-deactivation-feedback-popup.php'; |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | /** |
@@ -92,12 +92,12 @@ discard block |
||
| 92 | 92 | */ |
| 93 | 93 | public function enqueue_popup_scripts() { |
| 94 | 94 | // Bail if we don't have permissions to show the popup. |
| 95 | - if ( ! $this->has_permission_to_show_popup() ) { |
|
| 95 | + if ( ! $this->has_permission_to_show_popup()) { |
|
| 96 | 96 | return; |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | - wp_enqueue_style( 'wordlift-admin-feedback-popup', plugin_dir_url( __DIR__ ) . 'admin/css/wordlift-admin-feedback-popup.css', array(), WORDLIFT_VERSION ); |
|
| 100 | - wp_enqueue_script( 'wordlift-admin-feedback-popup', plugin_dir_url( __DIR__ ) . 'admin/js/wordlift-admin-feedback-popup.js', array( 'jquery' ), WORDLIFT_VERSION, false ); |
|
| 99 | + wp_enqueue_style('wordlift-admin-feedback-popup', plugin_dir_url(__DIR__).'admin/css/wordlift-admin-feedback-popup.css', array(), WORDLIFT_VERSION); |
|
| 100 | + wp_enqueue_script('wordlift-admin-feedback-popup', plugin_dir_url(__DIR__).'admin/js/wordlift-admin-feedback-popup.js', array('jquery'), WORDLIFT_VERSION, false); |
|
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | /** |
@@ -110,52 +110,52 @@ discard block |
||
| 110 | 110 | public function wl_deactivation_feedback() { |
| 111 | 111 | // Bail if the nonce is not valid. |
| 112 | 112 | if ( |
| 113 | - empty( $_POST['wl_deactivation_feedback_nonce'] ) || // The nonce doens't exists. |
|
| 114 | - ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wl_deactivation_feedback_nonce'] ) ), 'wl_deactivation_feedback_nonce' ) // The nonce is invalid. |
|
| 113 | + empty($_POST['wl_deactivation_feedback_nonce']) || // The nonce doens't exists. |
|
| 114 | + ! wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['wl_deactivation_feedback_nonce'])), 'wl_deactivation_feedback_nonce') // The nonce is invalid. |
|
| 115 | 115 | ) { |
| 116 | - wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
| 116 | + wp_send_json_error(__('Nonce Security Check Failed!', 'wordlift')); |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | // We allow user to deactivate without providing a reason |
| 120 | 120 | // so bail and send success response. |
| 121 | - if ( empty( $_POST['code'] ) ) { |
|
| 121 | + if (empty($_POST['code'])) { |
|
| 122 | 122 | wp_send_json_success(); |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | - $plugin_data = get_plugin_data( plugin_dir_path( __DIR__ ) . 'wordlift.php', false, false ); |
|
| 125 | + $plugin_data = get_plugin_data(plugin_dir_path(__DIR__).'wordlift.php', false, false); |
|
| 126 | 126 | |
| 127 | 127 | // Prepare the options. |
| 128 | 128 | $options = array( |
| 129 | 129 | // The deactivation reason. |
| 130 | - 'code' => sanitize_text_field( wp_unslash( (string) $_POST['code'] ) ), |
|
| 130 | + 'code' => sanitize_text_field(wp_unslash((string) $_POST['code'])), |
|
| 131 | 131 | // Additional information if provided. |
| 132 | - 'details' => ( ! empty( $_POST['details'] ) ) ? sanitize_text_field( wp_unslash( (string) $_POST['details'] ) ) : '', |
|
| 132 | + 'details' => ( ! empty($_POST['details'])) ? sanitize_text_field(wp_unslash((string) $_POST['details'])) : '', |
|
| 133 | 133 | // The website url. |
| 134 | - 'url' => get_bloginfo( 'url' ), |
|
| 134 | + 'url' => get_bloginfo('url'), |
|
| 135 | 135 | // WP version. |
| 136 | - 'wordpressVersion' => get_bloginfo( 'version' ), |
|
| 136 | + 'wordpressVersion' => get_bloginfo('version'), |
|
| 137 | 137 | // WL version. |
| 138 | 138 | 'wordliftVersion' => $plugin_data['Version'], |
| 139 | 139 | // The admin email. |
| 140 | - 'email' => get_bloginfo( 'admin_email' ), |
|
| 140 | + 'email' => get_bloginfo('admin_email'), |
|
| 141 | 141 | ); |
| 142 | 142 | |
| 143 | 143 | $response = wp_remote_post( |
| 144 | 144 | Wordlift_Configuration_Service::get_instance()->get_deactivation_feedback_url(), |
| 145 | 145 | array( |
| 146 | 146 | 'method' => 'POST', |
| 147 | - 'body' => wp_json_encode( $options ), |
|
| 148 | - 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), |
|
| 147 | + 'body' => wp_json_encode($options), |
|
| 148 | + 'headers' => array('Content-Type' => 'application/json; charset=utf-8'), |
|
| 149 | 149 | ) |
| 150 | 150 | ); |
| 151 | 151 | |
| 152 | - $code = wp_remote_retrieve_response_code( $response ); |
|
| 153 | - $message = wp_remote_retrieve_response_message( $response ); |
|
| 152 | + $code = wp_remote_retrieve_response_code($response); |
|
| 153 | + $message = wp_remote_retrieve_response_message($response); |
|
| 154 | 154 | |
| 155 | 155 | // Add message to the error log if the response code is not 200. |
| 156 | - if ( 201 !== $code ) { |
|
| 156 | + if (201 !== $code) { |
|
| 157 | 157 | // Write the error in the logs. |
| 158 | - $this->log->error( 'An error occurred while requesting a feedback endpoint error_code: ' . $code . ' message: ' . $message ); |
|
| 158 | + $this->log->error('An error occurred while requesting a feedback endpoint error_code: '.$code.' message: '.$message); |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | // We should send success message even when the feedback is not |
@@ -23,107 +23,107 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class Wordlift_Loader { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * The array of actions registered with WordPress. |
|
| 28 | - * |
|
| 29 | - * @since 1.0.0 |
|
| 30 | - * @access protected |
|
| 31 | - * @var array $actions The actions registered with WordPress to fire when the plugin loads. |
|
| 32 | - */ |
|
| 33 | - protected $actions; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * The array of filters registered with WordPress. |
|
| 37 | - * |
|
| 38 | - * @since 1.0.0 |
|
| 39 | - * @access protected |
|
| 40 | - * @var array $filters The filters registered with WordPress to fire when the plugin loads. |
|
| 41 | - */ |
|
| 42 | - protected $filters; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Initialize the collections used to maintain the actions and filters. |
|
| 46 | - * |
|
| 47 | - * @since 1.0.0 |
|
| 48 | - */ |
|
| 49 | - public function __construct() { |
|
| 50 | - |
|
| 51 | - $this->actions = array(); |
|
| 52 | - $this->filters = array(); |
|
| 53 | - |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Add a new action to the collection to be registered with WordPress. |
|
| 58 | - * |
|
| 59 | - * @since 1.0.0 |
|
| 60 | - * @param string $hook The name of the WordPress action that is being registered. |
|
| 61 | - * @param object $component A reference to the instance of the object on which the action is defined. |
|
| 62 | - * @param string $callback The name of the function definition on the $component. |
|
| 63 | - * @param int $priority Optional. he priority at which the function should be fired. Default is 10. |
|
| 64 | - * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. |
|
| 65 | - */ |
|
| 66 | - public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
|
| 67 | - add_action( $hook, array( $component, $callback ), $priority, $accepted_args ); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Add a new filter to the collection to be registered with WordPress. |
|
| 72 | - * |
|
| 73 | - * @since 1.0.0 |
|
| 74 | - * @param string $hook The name of the WordPress filter that is being registered. |
|
| 75 | - * @param object $component A reference to the instance of the object on which the filter is defined. |
|
| 76 | - * @param string $callback The name of the function definition on the $component. |
|
| 77 | - * @param int $priority Optional. he priority at which the function should be fired. Default is 10. |
|
| 78 | - * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1 |
|
| 79 | - */ |
|
| 80 | - public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
|
| 81 | - add_filter( $hook, array( $component, $callback ), $priority, $accepted_args ); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * A utility function that is used to register the actions and hooks into a single |
|
| 86 | - * collection. |
|
| 87 | - * |
|
| 88 | - * @since 1.0.0 |
|
| 89 | - * @access private |
|
| 90 | - * @param array $hooks The collection of hooks that is being registered (that is, actions or filters). |
|
| 91 | - * @param string $hook The name of the WordPress filter that is being registered. |
|
| 92 | - * @param object $component A reference to the instance of the object on which the filter is defined. |
|
| 93 | - * @param string $callback The name of the function definition on the $component. |
|
| 94 | - * @param int $priority The priority at which the function should be fired. |
|
| 95 | - * @param int $accepted_args The number of arguments that should be passed to the $callback. |
|
| 96 | - * @return array The collection of actions and filters registered with WordPress. |
|
| 97 | - */ |
|
| 98 | - private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) { |
|
| 99 | - |
|
| 100 | - $hooks[] = array( |
|
| 101 | - 'hook' => $hook, |
|
| 102 | - 'component' => $component, |
|
| 103 | - 'callback' => $callback, |
|
| 104 | - 'priority' => $priority, |
|
| 105 | - 'accepted_args' => $accepted_args, |
|
| 106 | - ); |
|
| 107 | - |
|
| 108 | - return $hooks; |
|
| 109 | - |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Register the filters and actions with WordPress. |
|
| 114 | - * |
|
| 115 | - * @since 1.0.0 |
|
| 116 | - */ |
|
| 117 | - public function run() { |
|
| 118 | - |
|
| 119 | - foreach ( $this->filters as $hook ) { |
|
| 120 | - add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - foreach ( $this->actions as $hook ) { |
|
| 124 | - add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - } |
|
| 26 | + /** |
|
| 27 | + * The array of actions registered with WordPress. |
|
| 28 | + * |
|
| 29 | + * @since 1.0.0 |
|
| 30 | + * @access protected |
|
| 31 | + * @var array $actions The actions registered with WordPress to fire when the plugin loads. |
|
| 32 | + */ |
|
| 33 | + protected $actions; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * The array of filters registered with WordPress. |
|
| 37 | + * |
|
| 38 | + * @since 1.0.0 |
|
| 39 | + * @access protected |
|
| 40 | + * @var array $filters The filters registered with WordPress to fire when the plugin loads. |
|
| 41 | + */ |
|
| 42 | + protected $filters; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Initialize the collections used to maintain the actions and filters. |
|
| 46 | + * |
|
| 47 | + * @since 1.0.0 |
|
| 48 | + */ |
|
| 49 | + public function __construct() { |
|
| 50 | + |
|
| 51 | + $this->actions = array(); |
|
| 52 | + $this->filters = array(); |
|
| 53 | + |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Add a new action to the collection to be registered with WordPress. |
|
| 58 | + * |
|
| 59 | + * @since 1.0.0 |
|
| 60 | + * @param string $hook The name of the WordPress action that is being registered. |
|
| 61 | + * @param object $component A reference to the instance of the object on which the action is defined. |
|
| 62 | + * @param string $callback The name of the function definition on the $component. |
|
| 63 | + * @param int $priority Optional. he priority at which the function should be fired. Default is 10. |
|
| 64 | + * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. |
|
| 65 | + */ |
|
| 66 | + public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
|
| 67 | + add_action( $hook, array( $component, $callback ), $priority, $accepted_args ); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Add a new filter to the collection to be registered with WordPress. |
|
| 72 | + * |
|
| 73 | + * @since 1.0.0 |
|
| 74 | + * @param string $hook The name of the WordPress filter that is being registered. |
|
| 75 | + * @param object $component A reference to the instance of the object on which the filter is defined. |
|
| 76 | + * @param string $callback The name of the function definition on the $component. |
|
| 77 | + * @param int $priority Optional. he priority at which the function should be fired. Default is 10. |
|
| 78 | + * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1 |
|
| 79 | + */ |
|
| 80 | + public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
|
| 81 | + add_filter( $hook, array( $component, $callback ), $priority, $accepted_args ); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * A utility function that is used to register the actions and hooks into a single |
|
| 86 | + * collection. |
|
| 87 | + * |
|
| 88 | + * @since 1.0.0 |
|
| 89 | + * @access private |
|
| 90 | + * @param array $hooks The collection of hooks that is being registered (that is, actions or filters). |
|
| 91 | + * @param string $hook The name of the WordPress filter that is being registered. |
|
| 92 | + * @param object $component A reference to the instance of the object on which the filter is defined. |
|
| 93 | + * @param string $callback The name of the function definition on the $component. |
|
| 94 | + * @param int $priority The priority at which the function should be fired. |
|
| 95 | + * @param int $accepted_args The number of arguments that should be passed to the $callback. |
|
| 96 | + * @return array The collection of actions and filters registered with WordPress. |
|
| 97 | + */ |
|
| 98 | + private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) { |
|
| 99 | + |
|
| 100 | + $hooks[] = array( |
|
| 101 | + 'hook' => $hook, |
|
| 102 | + 'component' => $component, |
|
| 103 | + 'callback' => $callback, |
|
| 104 | + 'priority' => $priority, |
|
| 105 | + 'accepted_args' => $accepted_args, |
|
| 106 | + ); |
|
| 107 | + |
|
| 108 | + return $hooks; |
|
| 109 | + |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Register the filters and actions with WordPress. |
|
| 114 | + * |
|
| 115 | + * @since 1.0.0 |
|
| 116 | + */ |
|
| 117 | + public function run() { |
|
| 118 | + |
|
| 119 | + foreach ( $this->filters as $hook ) { |
|
| 120 | + add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + foreach ( $this->actions as $hook ) { |
|
| 124 | + add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | 129 | } |
@@ -63,8 +63,8 @@ discard block |
||
| 63 | 63 | * @param int $priority Optional. he priority at which the function should be fired. Default is 10. |
| 64 | 64 | * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. |
| 65 | 65 | */ |
| 66 | - public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
|
| 67 | - add_action( $hook, array( $component, $callback ), $priority, $accepted_args ); |
|
| 66 | + public function add_action($hook, $component, $callback, $priority = 10, $accepted_args = 1) { |
|
| 67 | + add_action($hook, array($component, $callback), $priority, $accepted_args); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | /** |
@@ -77,8 +77,8 @@ discard block |
||
| 77 | 77 | * @param int $priority Optional. he priority at which the function should be fired. Default is 10. |
| 78 | 78 | * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1 |
| 79 | 79 | */ |
| 80 | - public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
|
| 81 | - add_filter( $hook, array( $component, $callback ), $priority, $accepted_args ); |
|
| 80 | + public function add_filter($hook, $component, $callback, $priority = 10, $accepted_args = 1) { |
|
| 81 | + add_filter($hook, array($component, $callback), $priority, $accepted_args); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | /** |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | * @param int $accepted_args The number of arguments that should be passed to the $callback. |
| 96 | 96 | * @return array The collection of actions and filters registered with WordPress. |
| 97 | 97 | */ |
| 98 | - private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) { |
|
| 98 | + private function add($hooks, $hook, $component, $callback, $priority, $accepted_args) { |
|
| 99 | 99 | |
| 100 | 100 | $hooks[] = array( |
| 101 | 101 | 'hook' => $hook, |
@@ -116,12 +116,12 @@ discard block |
||
| 116 | 116 | */ |
| 117 | 117 | public function run() { |
| 118 | 118 | |
| 119 | - foreach ( $this->filters as $hook ) { |
|
| 120 | - add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
|
| 119 | + foreach ($this->filters as $hook) { |
|
| 120 | + add_filter($hook['hook'], array($hook['component'], $hook['callback']), $hook['priority'], $hook['accepted_args']); |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | - foreach ( $this->actions as $hook ) { |
|
| 124 | - add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
|
| 123 | + foreach ($this->actions as $hook) { |
|
| 124 | + add_action($hook['hook'], array($hook['component'], $hook['callback']), $hook['priority'], $hook['accepted_args']); |
|
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | } |