@@ -6,79 +6,79 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Import_Page extends Wordlift_Admin_Page { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * {@inheritdoc} |
|
| 11 | - */ |
|
| 12 | - public function get_page_title() { |
|
| 13 | - |
|
| 14 | - return __( 'Google Addon Import', 'wordlift' ); |
|
| 15 | - } |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * {@inheritdoc} |
|
| 19 | - */ |
|
| 20 | - public function get_menu_title() { |
|
| 21 | - |
|
| 22 | - return __( 'Google Addon Import', 'wordlift' ); |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - protected function get_parent_slug() { |
|
| 26 | - return null; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - public function render() { |
|
| 30 | - |
|
| 31 | - wp_enqueue_script( |
|
| 32 | - 'wl-gaddon-import-page', |
|
| 33 | - plugin_dir_url( __FILE__ ) . 'assets/gaddon-import-page.js', |
|
| 34 | - array(), |
|
| 35 | - WORDLIFT_VERSION, |
|
| 36 | - false |
|
| 37 | - ); |
|
| 38 | - |
|
| 39 | - wp_localize_script( |
|
| 40 | - 'wl-gaddon-import-page', |
|
| 41 | - '_wlGaddonImportSettings', |
|
| 42 | - array( |
|
| 43 | - 'restUrl' => get_rest_url(), |
|
| 44 | - 'nonce' => wp_create_nonce( 'wp_rest' ), |
|
| 45 | - 'entityUrls' => $this->get_entity_urls(), |
|
| 46 | - ) |
|
| 47 | - ); |
|
| 48 | - |
|
| 49 | - parent::render(); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * {@inheritdoc} |
|
| 54 | - */ |
|
| 55 | - public function get_menu_slug() { |
|
| 56 | - |
|
| 57 | - return 'wl_google_addon_import'; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * {@inheritdoc} |
|
| 62 | - */ |
|
| 63 | - public function get_partial_name() { |
|
| 64 | - return 'wordlift-admin-google-addon-import.php'; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - private function get_entity_urls() { |
|
| 68 | - if ( ! isset( $_GET['e'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 69 | - return array(); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - $entities = esc_url_raw( wp_unslash( $_GET['e'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 73 | - |
|
| 74 | - if ( empty( $entities ) ) { |
|
| 75 | - return array(); |
|
| 76 | - } |
|
| 77 | - if ( ! is_string( $entities ) ) { |
|
| 78 | - return array(); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - return explode( ',', $entities ); |
|
| 82 | - } |
|
| 9 | + /** |
|
| 10 | + * {@inheritdoc} |
|
| 11 | + */ |
|
| 12 | + public function get_page_title() { |
|
| 13 | + |
|
| 14 | + return __( 'Google Addon Import', 'wordlift' ); |
|
| 15 | + } |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * {@inheritdoc} |
|
| 19 | + */ |
|
| 20 | + public function get_menu_title() { |
|
| 21 | + |
|
| 22 | + return __( 'Google Addon Import', 'wordlift' ); |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + protected function get_parent_slug() { |
|
| 26 | + return null; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + public function render() { |
|
| 30 | + |
|
| 31 | + wp_enqueue_script( |
|
| 32 | + 'wl-gaddon-import-page', |
|
| 33 | + plugin_dir_url( __FILE__ ) . 'assets/gaddon-import-page.js', |
|
| 34 | + array(), |
|
| 35 | + WORDLIFT_VERSION, |
|
| 36 | + false |
|
| 37 | + ); |
|
| 38 | + |
|
| 39 | + wp_localize_script( |
|
| 40 | + 'wl-gaddon-import-page', |
|
| 41 | + '_wlGaddonImportSettings', |
|
| 42 | + array( |
|
| 43 | + 'restUrl' => get_rest_url(), |
|
| 44 | + 'nonce' => wp_create_nonce( 'wp_rest' ), |
|
| 45 | + 'entityUrls' => $this->get_entity_urls(), |
|
| 46 | + ) |
|
| 47 | + ); |
|
| 48 | + |
|
| 49 | + parent::render(); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * {@inheritdoc} |
|
| 54 | + */ |
|
| 55 | + public function get_menu_slug() { |
|
| 56 | + |
|
| 57 | + return 'wl_google_addon_import'; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * {@inheritdoc} |
|
| 62 | + */ |
|
| 63 | + public function get_partial_name() { |
|
| 64 | + return 'wordlift-admin-google-addon-import.php'; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + private function get_entity_urls() { |
|
| 68 | + if ( ! isset( $_GET['e'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 69 | + return array(); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + $entities = esc_url_raw( wp_unslash( $_GET['e'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 73 | + |
|
| 74 | + if ( empty( $entities ) ) { |
|
| 75 | + return array(); |
|
| 76 | + } |
|
| 77 | + if ( ! is_string( $entities ) ) { |
|
| 78 | + return array(); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + return explode( ',', $entities ); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | 84 | } |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | */ |
| 12 | 12 | public function get_page_title() { |
| 13 | 13 | |
| 14 | - return __( 'Google Addon Import', 'wordlift' ); |
|
| 14 | + return __('Google Addon Import', 'wordlift'); |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | /** |
@@ -19,7 +19,7 @@ discard block |
||
| 19 | 19 | */ |
| 20 | 20 | public function get_menu_title() { |
| 21 | 21 | |
| 22 | - return __( 'Google Addon Import', 'wordlift' ); |
|
| 22 | + return __('Google Addon Import', 'wordlift'); |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | protected function get_parent_slug() { |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | |
| 31 | 31 | wp_enqueue_script( |
| 32 | 32 | 'wl-gaddon-import-page', |
| 33 | - plugin_dir_url( __FILE__ ) . 'assets/gaddon-import-page.js', |
|
| 33 | + plugin_dir_url(__FILE__).'assets/gaddon-import-page.js', |
|
| 34 | 34 | array(), |
| 35 | 35 | WORDLIFT_VERSION, |
| 36 | 36 | false |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | '_wlGaddonImportSettings', |
| 42 | 42 | array( |
| 43 | 43 | 'restUrl' => get_rest_url(), |
| 44 | - 'nonce' => wp_create_nonce( 'wp_rest' ), |
|
| 44 | + 'nonce' => wp_create_nonce('wp_rest'), |
|
| 45 | 45 | 'entityUrls' => $this->get_entity_urls(), |
| 46 | 46 | ) |
| 47 | 47 | ); |
@@ -65,20 +65,20 @@ discard block |
||
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | private function get_entity_urls() { |
| 68 | - if ( ! isset( $_GET['e'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 68 | + if ( ! isset($_GET['e'])) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 69 | 69 | return array(); |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | - $entities = esc_url_raw( wp_unslash( $_GET['e'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 72 | + $entities = esc_url_raw(wp_unslash($_GET['e'])); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 73 | 73 | |
| 74 | - if ( empty( $entities ) ) { |
|
| 74 | + if (empty($entities)) { |
|
| 75 | 75 | return array(); |
| 76 | 76 | } |
| 77 | - if ( ! is_string( $entities ) ) { |
|
| 77 | + if ( ! is_string($entities)) { |
|
| 78 | 78 | return array(); |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | - return explode( ',', $entities ); |
|
| 81 | + return explode(',', $entities); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | } |
@@ -17,16 +17,16 @@ discard block |
||
| 17 | 17 | use Wordlift\Modules\Pods\WlEntityField\Filters; |
| 18 | 18 | |
| 19 | 19 | if ( ! defined( 'ABSPATH' ) ) { |
| 20 | - exit; |
|
| 20 | + exit; |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | if ( ! apply_filters( 'wl_feature__enable__pods-integration', false ) ) { //phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 24 | - return; |
|
| 24 | + return; |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | // Autoloader for plugin itself. |
| 28 | 28 | if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) { |
| 29 | - require __DIR__ . '/vendor/autoload.php'; |
|
| 29 | + require __DIR__ . '/vendor/autoload.php'; |
|
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | $container_builder = new ContainerBuilder(); |
@@ -35,32 +35,32 @@ discard block |
||
| 35 | 35 | $container_builder->compile(); |
| 36 | 36 | |
| 37 | 37 | add_action( |
| 38 | - 'plugins_loaded', |
|
| 39 | - function () use ( $container_builder ) { |
|
| 38 | + 'plugins_loaded', |
|
| 39 | + function () use ( $container_builder ) { |
|
| 40 | 40 | |
| 41 | - if ( ! defined( 'PODS_VERSION' ) ) { |
|
| 42 | - return; |
|
| 43 | - } |
|
| 41 | + if ( ! defined( 'PODS_VERSION' ) ) { |
|
| 42 | + return; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - $factory = $container_builder->get( FieldDefinitionFactory::class ); |
|
| 46 | - $field_definition = $factory->get_field_definition(); |
|
| 47 | - $field_definition->register(); |
|
| 45 | + $factory = $container_builder->get( FieldDefinitionFactory::class ); |
|
| 46 | + $field_definition = $factory->get_field_definition(); |
|
| 47 | + $field_definition->register(); |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * @var $installer \Wordlift\Modules\Pods\Installer |
|
| 51 | - */ |
|
| 52 | - $installer = $container_builder->get( Installer::class ); |
|
| 53 | - $installer->register_hooks(); |
|
| 49 | + /** |
|
| 50 | + * @var $installer \Wordlift\Modules\Pods\Installer |
|
| 51 | + */ |
|
| 52 | + $installer = $container_builder->get( Installer::class ); |
|
| 53 | + $installer->register_hooks(); |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * @var $notices \Wordlift\Modules\Pods\Notices |
|
| 57 | - */ |
|
| 58 | - $notices = $container_builder->get( Notices::class ); |
|
| 59 | - $notices->register_hooks(); |
|
| 55 | + /** |
|
| 56 | + * @var $notices \Wordlift\Modules\Pods\Notices |
|
| 57 | + */ |
|
| 58 | + $notices = $container_builder->get( Notices::class ); |
|
| 59 | + $notices->register_hooks(); |
|
| 60 | 60 | |
| 61 | - $container_builder->get( Filters::class ); |
|
| 61 | + $container_builder->get( Filters::class ); |
|
| 62 | 62 | |
| 63 | - } |
|
| 63 | + } |
|
| 64 | 64 | ); |
| 65 | 65 | |
| 66 | 66 | |
@@ -16,49 +16,49 @@ |
||
| 16 | 16 | use Wordlift\Modules\Pods\Notices; |
| 17 | 17 | use Wordlift\Modules\Pods\WlEntityField\Filters; |
| 18 | 18 | |
| 19 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 19 | +if ( ! defined('ABSPATH')) { |
|
| 20 | 20 | exit; |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | -if ( ! apply_filters( 'wl_feature__enable__pods-integration', false ) ) { //phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 23 | +if ( ! apply_filters('wl_feature__enable__pods-integration', false)) { //phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 24 | 24 | return; |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | // Autoloader for plugin itself. |
| 28 | -if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) { |
|
| 29 | - require __DIR__ . '/vendor/autoload.php'; |
|
| 28 | +if (file_exists(__DIR__.'/vendor/autoload.php')) { |
|
| 29 | + require __DIR__.'/vendor/autoload.php'; |
|
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | $container_builder = new ContainerBuilder(); |
| 33 | -$loader = new YamlFileLoader( $container_builder, new FileLocator( __DIR__ ) ); |
|
| 34 | -$loader->load( 'services.yml' ); |
|
| 33 | +$loader = new YamlFileLoader($container_builder, new FileLocator(__DIR__)); |
|
| 34 | +$loader->load('services.yml'); |
|
| 35 | 35 | $container_builder->compile(); |
| 36 | 36 | |
| 37 | 37 | add_action( |
| 38 | 38 | 'plugins_loaded', |
| 39 | - function () use ( $container_builder ) { |
|
| 39 | + function() use ($container_builder) { |
|
| 40 | 40 | |
| 41 | - if ( ! defined( 'PODS_VERSION' ) ) { |
|
| 41 | + if ( ! defined('PODS_VERSION')) { |
|
| 42 | 42 | return; |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | - $factory = $container_builder->get( FieldDefinitionFactory::class ); |
|
| 45 | + $factory = $container_builder->get(FieldDefinitionFactory::class); |
|
| 46 | 46 | $field_definition = $factory->get_field_definition(); |
| 47 | 47 | $field_definition->register(); |
| 48 | 48 | |
| 49 | 49 | /** |
| 50 | 50 | * @var $installer \Wordlift\Modules\Pods\Installer |
| 51 | 51 | */ |
| 52 | - $installer = $container_builder->get( Installer::class ); |
|
| 52 | + $installer = $container_builder->get(Installer::class); |
|
| 53 | 53 | $installer->register_hooks(); |
| 54 | 54 | |
| 55 | 55 | /** |
| 56 | 56 | * @var $notices \Wordlift\Modules\Pods\Notices |
| 57 | 57 | */ |
| 58 | - $notices = $container_builder->get( Notices::class ); |
|
| 58 | + $notices = $container_builder->get(Notices::class); |
|
| 59 | 59 | $notices->register_hooks(); |
| 60 | 60 | |
| 61 | - $container_builder->get( Filters::class ); |
|
| 61 | + $container_builder->get(Filters::class); |
|
| 62 | 62 | |
| 63 | 63 | } |
| 64 | 64 | ); |
@@ -21,45 +21,45 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class Jsonld_Adapter { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @var Wordlift_Jsonld_Service |
|
| 26 | - */ |
|
| 27 | - private $jsonld_service; |
|
| 24 | + /** |
|
| 25 | + * @var Wordlift_Jsonld_Service |
|
| 26 | + */ |
|
| 27 | + private $jsonld_service; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Jsonld_Adapter constructor. |
|
| 31 | - * |
|
| 32 | - * @param \Wordlift_Jsonld_Service $jsonld_service |
|
| 33 | - */ |
|
| 34 | - public function __construct( $jsonld_service ) { |
|
| 29 | + /** |
|
| 30 | + * Jsonld_Adapter constructor. |
|
| 31 | + * |
|
| 32 | + * @param \Wordlift_Jsonld_Service $jsonld_service |
|
| 33 | + */ |
|
| 34 | + public function __construct( $jsonld_service ) { |
|
| 35 | 35 | |
| 36 | - $this->jsonld_service = $jsonld_service; |
|
| 36 | + $this->jsonld_service = $jsonld_service; |
|
| 37 | 37 | |
| 38 | - add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
| 39 | - add_action( 'amp_post_template_head', array( $this, 'wp_head' ) ); |
|
| 38 | + add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
| 39 | + add_action( 'amp_post_template_head', array( $this, 'wp_head' ) ); |
|
| 40 | 40 | |
| 41 | - } |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - public function wp_head() { |
|
| 43 | + public function wp_head() { |
|
| 44 | 44 | |
| 45 | - // Bail out if `wl_jsonld_enabled` isn't enabled. |
|
| 46 | - if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) { |
|
| 47 | - return; |
|
| 48 | - } |
|
| 45 | + // Bail out if `wl_jsonld_enabled` isn't enabled. |
|
| 46 | + if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) { |
|
| 47 | + return; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - // Determine whether this is the home page or whether we're displaying a single post. |
|
| 51 | - $is_homepage = is_home() || is_front_page(); |
|
| 52 | - $post_id = is_singular() ? get_the_ID() : null; |
|
| 50 | + // Determine whether this is the home page or whether we're displaying a single post. |
|
| 51 | + $is_homepage = is_home() || is_front_page(); |
|
| 52 | + $post_id = is_singular() ? get_the_ID() : null; |
|
| 53 | 53 | |
| 54 | - // Get the JSON-LD. |
|
| 55 | - $jsonld = wp_json_encode( $this->jsonld_service->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) ); |
|
| 56 | - // Finally print the JSON-LD out. |
|
| 57 | - $jsonld_post_html_output = '<script type="application/ld+json" id="wl-jsonld">' . $jsonld . '</script>'; |
|
| 58 | - $jsonld_post_html_output = apply_filters( 'wl_jsonld_post_html_output', $jsonld_post_html_output, $post_id ); |
|
| 54 | + // Get the JSON-LD. |
|
| 55 | + $jsonld = wp_json_encode( $this->jsonld_service->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) ); |
|
| 56 | + // Finally print the JSON-LD out. |
|
| 57 | + $jsonld_post_html_output = '<script type="application/ld+json" id="wl-jsonld">' . $jsonld . '</script>'; |
|
| 58 | + $jsonld_post_html_output = apply_filters( 'wl_jsonld_post_html_output', $jsonld_post_html_output, $post_id ); |
|
| 59 | 59 | |
| 60 | - // We don't need to escape this one since it's a JSON-LD output. |
|
| 61 | - echo $jsonld_post_html_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 60 | + // We don't need to escape this one since it's a JSON-LD output. |
|
| 61 | + echo $jsonld_post_html_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 62 | 62 | |
| 63 | - } |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | 65 | } |
@@ -31,19 +31,19 @@ discard block |
||
| 31 | 31 | * |
| 32 | 32 | * @param \Wordlift_Jsonld_Service $jsonld_service |
| 33 | 33 | */ |
| 34 | - public function __construct( $jsonld_service ) { |
|
| 34 | + public function __construct($jsonld_service) { |
|
| 35 | 35 | |
| 36 | 36 | $this->jsonld_service = $jsonld_service; |
| 37 | 37 | |
| 38 | - add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
| 39 | - add_action( 'amp_post_template_head', array( $this, 'wp_head' ) ); |
|
| 38 | + add_action('wp_head', array($this, 'wp_head')); |
|
| 39 | + add_action('amp_post_template_head', array($this, 'wp_head')); |
|
| 40 | 40 | |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | public function wp_head() { |
| 44 | 44 | |
| 45 | 45 | // Bail out if `wl_jsonld_enabled` isn't enabled. |
| 46 | - if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) { |
|
| 46 | + if ( ! apply_filters('wl_jsonld_enabled', true)) { |
|
| 47 | 47 | return; |
| 48 | 48 | } |
| 49 | 49 | |
@@ -52,10 +52,10 @@ discard block |
||
| 52 | 52 | $post_id = is_singular() ? get_the_ID() : null; |
| 53 | 53 | |
| 54 | 54 | // Get the JSON-LD. |
| 55 | - $jsonld = wp_json_encode( $this->jsonld_service->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) ); |
|
| 55 | + $jsonld = wp_json_encode($this->jsonld_service->get_jsonld($is_homepage, $post_id, Jsonld_Context_Enum::PAGE)); |
|
| 56 | 56 | // Finally print the JSON-LD out. |
| 57 | - $jsonld_post_html_output = '<script type="application/ld+json" id="wl-jsonld">' . $jsonld . '</script>'; |
|
| 58 | - $jsonld_post_html_output = apply_filters( 'wl_jsonld_post_html_output', $jsonld_post_html_output, $post_id ); |
|
| 57 | + $jsonld_post_html_output = '<script type="application/ld+json" id="wl-jsonld">'.$jsonld.'</script>'; |
|
| 58 | + $jsonld_post_html_output = apply_filters('wl_jsonld_post_html_output', $jsonld_post_html_output, $post_id); |
|
| 59 | 59 | |
| 60 | 60 | // We don't need to escape this one since it's a JSON-LD output. |
| 61 | 61 | echo $jsonld_post_html_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
@@ -20,274 +20,274 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | class Wordlift_Term_JsonLd_Adapter { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 25 | - * |
|
| 26 | - * @since 3.20.0 |
|
| 27 | - * @access private |
|
| 28 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 29 | - */ |
|
| 30 | - private $entity_uri_service; |
|
| 31 | - |
|
| 32 | - private static $instance; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @var Wordlift_Post_Converter |
|
| 36 | - */ |
|
| 37 | - private $post_id_to_jsonld_converter; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Wordlift_Term_JsonLd_Adapter constructor. |
|
| 41 | - * |
|
| 42 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 43 | - * @param \Wordlift_Post_Converter $post_id_to_jsonld_converter The {@link Wordlift_Post_Converter} instance. |
|
| 44 | - * |
|
| 45 | - * @since 3.20.0 |
|
| 46 | - */ |
|
| 47 | - public function __construct( $entity_uri_service, $post_id_to_jsonld_converter ) { |
|
| 48 | - |
|
| 49 | - add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
| 50 | - |
|
| 51 | - $this->entity_uri_service = $entity_uri_service; |
|
| 52 | - $this->post_id_to_jsonld_converter = $post_id_to_jsonld_converter; |
|
| 53 | - |
|
| 54 | - self::$instance = $this; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - public static function get_instance() { |
|
| 58 | - |
|
| 59 | - return self::$instance; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Adds carousel json ld data to term page if the conditions match |
|
| 64 | - * |
|
| 65 | - * @return array|boolean |
|
| 66 | - */ |
|
| 67 | - public function get_carousel_jsonld( $id = null ) { |
|
| 68 | - $posts = $this->get_posts( $id ); |
|
| 69 | - $post_jsonld = array(); |
|
| 70 | - if ( ! is_array( $posts ) || count( $posts ) < 2 ) { |
|
| 71 | - // Bail out if no posts are present. |
|
| 72 | - return false; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - if ( $id !== null ) { |
|
| 76 | - $term = get_term( $id ); |
|
| 77 | - $post_jsonld['description'] = wp_strip_all_tags( strip_shortcodes( $term->description ) ); |
|
| 78 | - $thumbnail_id = get_term_meta( $id, 'thumbnail_id', true ); |
|
| 79 | - if ( ! empty( $thumbnail_id ) ) { |
|
| 80 | - $post_jsonld['image'] = wp_get_attachment_url( $thumbnail_id ); |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - // More than 2 items are present, so construct the post_jsonld data |
|
| 85 | - $post_jsonld['@context'] = 'https://schema.org'; |
|
| 86 | - $post_jsonld['@type'] = 'ItemList'; |
|
| 87 | - $post_jsonld['url'] = $this->get_term_url( $id ); |
|
| 88 | - $post_jsonld['itemListElement'] = array(); |
|
| 89 | - $position = 1; |
|
| 90 | - |
|
| 91 | - foreach ( $posts as $post_id ) { |
|
| 92 | - $result = array( |
|
| 93 | - '@type' => 'ListItem', |
|
| 94 | - 'position' => $position, |
|
| 95 | - /** |
|
| 96 | - * We can't use `item` here unless we change the URL for the item to point to the current page. |
|
| 97 | - * |
|
| 98 | - * See https://developers.google.com/search/docs/data-types/carousel |
|
| 99 | - */ |
|
| 100 | - 'url' => apply_filters( 'wl_carousel_post_list_item_url', get_permalink( $post_id ), $post_id ), |
|
| 101 | - ); |
|
| 102 | - array_push( $post_jsonld['itemListElement'], $result ); |
|
| 103 | - ++ $position; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - return $post_jsonld; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - private function get_posts( $id ) { |
|
| 110 | - global $wp_query; |
|
| 111 | - |
|
| 112 | - if ( $wp_query->posts !== null ) { |
|
| 113 | - return array_map( |
|
| 114 | - function ( $post ) { |
|
| 115 | - return $post->ID; |
|
| 116 | - }, |
|
| 117 | - $wp_query->posts |
|
| 118 | - ); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - if ( $id === null ) { |
|
| 122 | - return null; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - $term = get_term( $id ); |
|
| 126 | - |
|
| 127 | - return get_objects_in_term( $id, $term->taxonomy ); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Hook to `wp_head` to print the JSON-LD. |
|
| 132 | - * |
|
| 133 | - * @since 3.20.0 |
|
| 134 | - */ |
|
| 135 | - public function wp_head() { |
|
| 136 | - $query_object = get_queried_object(); |
|
| 137 | - |
|
| 138 | - // Check if it is a term page. |
|
| 139 | - if ( ! $query_object instanceof WP_Term ) { |
|
| 140 | - return; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - // Bail out if `wl_jsonld_enabled` isn't enabled. |
|
| 144 | - if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) { |
|
| 145 | - return; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - $term_id = $query_object->term_id; |
|
| 149 | - |
|
| 150 | - $jsonld = $this->get( $term_id, Jsonld_Context_Enum::PAGE ); |
|
| 151 | - |
|
| 152 | - // Bail out if the JSON-LD is empty. |
|
| 153 | - if ( empty( $jsonld ) ) { |
|
| 154 | - return; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - $jsonld_string = wp_json_encode( $jsonld ); |
|
| 158 | - |
|
| 159 | - $jsonld_term_html_output = '<script type="application/ld+json" id="wl-jsonld-term">' . $jsonld_string . '</script>'; |
|
| 160 | - $jsonld_term_html_output = apply_filters( 'wl_jsonld_term_html_output', $jsonld_term_html_output, $term_id ); |
|
| 161 | - |
|
| 162 | - echo $jsonld_term_html_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- It's an application/ld+json output. |
|
| 163 | - |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - public function get( $id, $context, $is_recursive_call = false ) { |
|
| 167 | - /** |
|
| 168 | - * Support for carousel rich snippet, get jsonld data present |
|
| 169 | - * for all the posts shown in the term page, and add the jsonld data |
|
| 170 | - * to list |
|
| 171 | - * |
|
| 172 | - * see here: https://developers.google.com/search/docs/data-types/carousel |
|
| 173 | - * |
|
| 174 | - * @since 3.26.0 |
|
| 175 | - */ |
|
| 176 | - $jsonld_array = array(); |
|
| 177 | - |
|
| 178 | - if ( Jsonld_Context_Enum::PAGE === $context ) { |
|
| 179 | - $carousel_data = $this->get_carousel_jsonld( $id ); |
|
| 180 | - if ( $carousel_data ) { |
|
| 181 | - $jsonld_array[] = $carousel_data; |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - $entities_jsonld_array = $this->get_entity_jsonld( $id, $context ); |
|
| 186 | - |
|
| 187 | - $result = array( |
|
| 188 | - 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
| 189 | - 'references' => array(), |
|
| 190 | - ); |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * @since 3.26.3 |
|
| 194 | - * Filter: wl_term_jsonld_array |
|
| 195 | - * @var $id int Term id |
|
| 196 | - * @var $jsonld_array array An array containing jsonld for term and entities. |
|
| 197 | - */ |
|
| 198 | - $arr = apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
| 199 | - |
|
| 200 | - $references = array(); |
|
| 201 | - |
|
| 202 | - // Don't expand nested references, it will lead to an infinite loop. |
|
| 203 | - if ( ! $is_recursive_call ) { |
|
| 204 | - /** |
|
| 205 | - * @since 3.32.0 |
|
| 206 | - * Expand the references returned by this filter. |
|
| 207 | - */ |
|
| 208 | - $references = $this->expand_references( $arr['references'] ); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - $jsonld_array = array_merge( $arr['jsonld'], $references ); |
|
| 212 | - |
|
| 213 | - return $jsonld_array; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - private function get_term_url( $id ) { |
|
| 217 | - if ( null === $id ) { |
|
| 218 | - return isset( $_SERVER['REQUEST_URI'] ) ? filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL ) : ''; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true ); |
|
| 222 | - if ( ! empty( $maybe_url ) ) { |
|
| 223 | - return $maybe_url; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - return get_term_link( $id ); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * Return jsonld for entities bound to terms. |
|
| 231 | - * |
|
| 232 | - * @param int $term_id Term ID. |
|
| 233 | - * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
| 234 | - * |
|
| 235 | - * @return array |
|
| 236 | - */ |
|
| 237 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 238 | - private function get_entity_jsonld( $term_id, $context ) { |
|
| 239 | - |
|
| 240 | - // The `_wl_entity_id` are URIs. |
|
| 241 | - $entity_ids = get_term_meta( $term_id, '_wl_entity_id' ); |
|
| 242 | - $entity_uri_service = $this->entity_uri_service; |
|
| 243 | - |
|
| 244 | - $wordlift_jsonld_service = Wordlift_Jsonld_Service::get_instance(); |
|
| 245 | - |
|
| 246 | - $local_entity_ids = array_filter( |
|
| 247 | - $entity_ids, |
|
| 248 | - function ( $uri ) use ( $entity_uri_service ) { |
|
| 249 | - return $entity_uri_service->is_internal( $uri ); |
|
| 250 | - } |
|
| 251 | - ); |
|
| 252 | - |
|
| 253 | - // Bail out if there are no entities. |
|
| 254 | - if ( empty( $local_entity_ids ) ) { |
|
| 255 | - return array(); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - $post = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) ); |
|
| 259 | - $entities_jsonld = $wordlift_jsonld_service->get_jsonld( false, $post->ID ); |
|
| 260 | - // Reset the `url` to the term page. |
|
| 261 | - $entities_jsonld[0]['url'] = get_term_link( $term_id ); |
|
| 262 | - |
|
| 263 | - return $entities_jsonld; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * @param $references |
|
| 268 | - * |
|
| 269 | - * @return array |
|
| 270 | - */ |
|
| 271 | - private function expand_references( $references ) { |
|
| 272 | - if ( ! is_array( $references ) ) { |
|
| 273 | - return array(); |
|
| 274 | - } |
|
| 275 | - $references_jsonld = array(); |
|
| 276 | - // Expand the references. |
|
| 277 | - foreach ( $references as $reference ) { |
|
| 278 | - if ( $reference instanceof Post_Reference ) { |
|
| 279 | - $post_id = $reference->get_id(); |
|
| 280 | - $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $post_id ); |
|
| 281 | - } elseif ( $reference instanceof Term_Reference ) { |
|
| 282 | - // Second level references won't be expanded. |
|
| 283 | - $references_jsonld[] = current( $this->get( $reference->get_id(), Jsonld_Context_Enum::UNKNOWN, true ) ); |
|
| 284 | - } elseif ( is_numeric( $reference ) ) { |
|
| 285 | - $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference ); |
|
| 286 | - } |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - return $references_jsonld; |
|
| 290 | - |
|
| 291 | - } |
|
| 23 | + /** |
|
| 24 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 25 | + * |
|
| 26 | + * @since 3.20.0 |
|
| 27 | + * @access private |
|
| 28 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 29 | + */ |
|
| 30 | + private $entity_uri_service; |
|
| 31 | + |
|
| 32 | + private static $instance; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @var Wordlift_Post_Converter |
|
| 36 | + */ |
|
| 37 | + private $post_id_to_jsonld_converter; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Wordlift_Term_JsonLd_Adapter constructor. |
|
| 41 | + * |
|
| 42 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 43 | + * @param \Wordlift_Post_Converter $post_id_to_jsonld_converter The {@link Wordlift_Post_Converter} instance. |
|
| 44 | + * |
|
| 45 | + * @since 3.20.0 |
|
| 46 | + */ |
|
| 47 | + public function __construct( $entity_uri_service, $post_id_to_jsonld_converter ) { |
|
| 48 | + |
|
| 49 | + add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
| 50 | + |
|
| 51 | + $this->entity_uri_service = $entity_uri_service; |
|
| 52 | + $this->post_id_to_jsonld_converter = $post_id_to_jsonld_converter; |
|
| 53 | + |
|
| 54 | + self::$instance = $this; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + public static function get_instance() { |
|
| 58 | + |
|
| 59 | + return self::$instance; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Adds carousel json ld data to term page if the conditions match |
|
| 64 | + * |
|
| 65 | + * @return array|boolean |
|
| 66 | + */ |
|
| 67 | + public function get_carousel_jsonld( $id = null ) { |
|
| 68 | + $posts = $this->get_posts( $id ); |
|
| 69 | + $post_jsonld = array(); |
|
| 70 | + if ( ! is_array( $posts ) || count( $posts ) < 2 ) { |
|
| 71 | + // Bail out if no posts are present. |
|
| 72 | + return false; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + if ( $id !== null ) { |
|
| 76 | + $term = get_term( $id ); |
|
| 77 | + $post_jsonld['description'] = wp_strip_all_tags( strip_shortcodes( $term->description ) ); |
|
| 78 | + $thumbnail_id = get_term_meta( $id, 'thumbnail_id', true ); |
|
| 79 | + if ( ! empty( $thumbnail_id ) ) { |
|
| 80 | + $post_jsonld['image'] = wp_get_attachment_url( $thumbnail_id ); |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + // More than 2 items are present, so construct the post_jsonld data |
|
| 85 | + $post_jsonld['@context'] = 'https://schema.org'; |
|
| 86 | + $post_jsonld['@type'] = 'ItemList'; |
|
| 87 | + $post_jsonld['url'] = $this->get_term_url( $id ); |
|
| 88 | + $post_jsonld['itemListElement'] = array(); |
|
| 89 | + $position = 1; |
|
| 90 | + |
|
| 91 | + foreach ( $posts as $post_id ) { |
|
| 92 | + $result = array( |
|
| 93 | + '@type' => 'ListItem', |
|
| 94 | + 'position' => $position, |
|
| 95 | + /** |
|
| 96 | + * We can't use `item` here unless we change the URL for the item to point to the current page. |
|
| 97 | + * |
|
| 98 | + * See https://developers.google.com/search/docs/data-types/carousel |
|
| 99 | + */ |
|
| 100 | + 'url' => apply_filters( 'wl_carousel_post_list_item_url', get_permalink( $post_id ), $post_id ), |
|
| 101 | + ); |
|
| 102 | + array_push( $post_jsonld['itemListElement'], $result ); |
|
| 103 | + ++ $position; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + return $post_jsonld; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + private function get_posts( $id ) { |
|
| 110 | + global $wp_query; |
|
| 111 | + |
|
| 112 | + if ( $wp_query->posts !== null ) { |
|
| 113 | + return array_map( |
|
| 114 | + function ( $post ) { |
|
| 115 | + return $post->ID; |
|
| 116 | + }, |
|
| 117 | + $wp_query->posts |
|
| 118 | + ); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + if ( $id === null ) { |
|
| 122 | + return null; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + $term = get_term( $id ); |
|
| 126 | + |
|
| 127 | + return get_objects_in_term( $id, $term->taxonomy ); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Hook to `wp_head` to print the JSON-LD. |
|
| 132 | + * |
|
| 133 | + * @since 3.20.0 |
|
| 134 | + */ |
|
| 135 | + public function wp_head() { |
|
| 136 | + $query_object = get_queried_object(); |
|
| 137 | + |
|
| 138 | + // Check if it is a term page. |
|
| 139 | + if ( ! $query_object instanceof WP_Term ) { |
|
| 140 | + return; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + // Bail out if `wl_jsonld_enabled` isn't enabled. |
|
| 144 | + if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) { |
|
| 145 | + return; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + $term_id = $query_object->term_id; |
|
| 149 | + |
|
| 150 | + $jsonld = $this->get( $term_id, Jsonld_Context_Enum::PAGE ); |
|
| 151 | + |
|
| 152 | + // Bail out if the JSON-LD is empty. |
|
| 153 | + if ( empty( $jsonld ) ) { |
|
| 154 | + return; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + $jsonld_string = wp_json_encode( $jsonld ); |
|
| 158 | + |
|
| 159 | + $jsonld_term_html_output = '<script type="application/ld+json" id="wl-jsonld-term">' . $jsonld_string . '</script>'; |
|
| 160 | + $jsonld_term_html_output = apply_filters( 'wl_jsonld_term_html_output', $jsonld_term_html_output, $term_id ); |
|
| 161 | + |
|
| 162 | + echo $jsonld_term_html_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- It's an application/ld+json output. |
|
| 163 | + |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + public function get( $id, $context, $is_recursive_call = false ) { |
|
| 167 | + /** |
|
| 168 | + * Support for carousel rich snippet, get jsonld data present |
|
| 169 | + * for all the posts shown in the term page, and add the jsonld data |
|
| 170 | + * to list |
|
| 171 | + * |
|
| 172 | + * see here: https://developers.google.com/search/docs/data-types/carousel |
|
| 173 | + * |
|
| 174 | + * @since 3.26.0 |
|
| 175 | + */ |
|
| 176 | + $jsonld_array = array(); |
|
| 177 | + |
|
| 178 | + if ( Jsonld_Context_Enum::PAGE === $context ) { |
|
| 179 | + $carousel_data = $this->get_carousel_jsonld( $id ); |
|
| 180 | + if ( $carousel_data ) { |
|
| 181 | + $jsonld_array[] = $carousel_data; |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + $entities_jsonld_array = $this->get_entity_jsonld( $id, $context ); |
|
| 186 | + |
|
| 187 | + $result = array( |
|
| 188 | + 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
| 189 | + 'references' => array(), |
|
| 190 | + ); |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * @since 3.26.3 |
|
| 194 | + * Filter: wl_term_jsonld_array |
|
| 195 | + * @var $id int Term id |
|
| 196 | + * @var $jsonld_array array An array containing jsonld for term and entities. |
|
| 197 | + */ |
|
| 198 | + $arr = apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
| 199 | + |
|
| 200 | + $references = array(); |
|
| 201 | + |
|
| 202 | + // Don't expand nested references, it will lead to an infinite loop. |
|
| 203 | + if ( ! $is_recursive_call ) { |
|
| 204 | + /** |
|
| 205 | + * @since 3.32.0 |
|
| 206 | + * Expand the references returned by this filter. |
|
| 207 | + */ |
|
| 208 | + $references = $this->expand_references( $arr['references'] ); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + $jsonld_array = array_merge( $arr['jsonld'], $references ); |
|
| 212 | + |
|
| 213 | + return $jsonld_array; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + private function get_term_url( $id ) { |
|
| 217 | + if ( null === $id ) { |
|
| 218 | + return isset( $_SERVER['REQUEST_URI'] ) ? filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL ) : ''; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true ); |
|
| 222 | + if ( ! empty( $maybe_url ) ) { |
|
| 223 | + return $maybe_url; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + return get_term_link( $id ); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * Return jsonld for entities bound to terms. |
|
| 231 | + * |
|
| 232 | + * @param int $term_id Term ID. |
|
| 233 | + * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
| 234 | + * |
|
| 235 | + * @return array |
|
| 236 | + */ |
|
| 237 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 238 | + private function get_entity_jsonld( $term_id, $context ) { |
|
| 239 | + |
|
| 240 | + // The `_wl_entity_id` are URIs. |
|
| 241 | + $entity_ids = get_term_meta( $term_id, '_wl_entity_id' ); |
|
| 242 | + $entity_uri_service = $this->entity_uri_service; |
|
| 243 | + |
|
| 244 | + $wordlift_jsonld_service = Wordlift_Jsonld_Service::get_instance(); |
|
| 245 | + |
|
| 246 | + $local_entity_ids = array_filter( |
|
| 247 | + $entity_ids, |
|
| 248 | + function ( $uri ) use ( $entity_uri_service ) { |
|
| 249 | + return $entity_uri_service->is_internal( $uri ); |
|
| 250 | + } |
|
| 251 | + ); |
|
| 252 | + |
|
| 253 | + // Bail out if there are no entities. |
|
| 254 | + if ( empty( $local_entity_ids ) ) { |
|
| 255 | + return array(); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + $post = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) ); |
|
| 259 | + $entities_jsonld = $wordlift_jsonld_service->get_jsonld( false, $post->ID ); |
|
| 260 | + // Reset the `url` to the term page. |
|
| 261 | + $entities_jsonld[0]['url'] = get_term_link( $term_id ); |
|
| 262 | + |
|
| 263 | + return $entities_jsonld; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * @param $references |
|
| 268 | + * |
|
| 269 | + * @return array |
|
| 270 | + */ |
|
| 271 | + private function expand_references( $references ) { |
|
| 272 | + if ( ! is_array( $references ) ) { |
|
| 273 | + return array(); |
|
| 274 | + } |
|
| 275 | + $references_jsonld = array(); |
|
| 276 | + // Expand the references. |
|
| 277 | + foreach ( $references as $reference ) { |
|
| 278 | + if ( $reference instanceof Post_Reference ) { |
|
| 279 | + $post_id = $reference->get_id(); |
|
| 280 | + $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $post_id ); |
|
| 281 | + } elseif ( $reference instanceof Term_Reference ) { |
|
| 282 | + // Second level references won't be expanded. |
|
| 283 | + $references_jsonld[] = current( $this->get( $reference->get_id(), Jsonld_Context_Enum::UNKNOWN, true ) ); |
|
| 284 | + } elseif ( is_numeric( $reference ) ) { |
|
| 285 | + $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference ); |
|
| 286 | + } |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + return $references_jsonld; |
|
| 290 | + |
|
| 291 | + } |
|
| 292 | 292 | |
| 293 | 293 | } |
@@ -44,9 +44,9 @@ discard block |
||
| 44 | 44 | * |
| 45 | 45 | * @since 3.20.0 |
| 46 | 46 | */ |
| 47 | - public function __construct( $entity_uri_service, $post_id_to_jsonld_converter ) { |
|
| 47 | + public function __construct($entity_uri_service, $post_id_to_jsonld_converter) { |
|
| 48 | 48 | |
| 49 | - add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
| 49 | + add_action('wp_head', array($this, 'wp_head')); |
|
| 50 | 50 | |
| 51 | 51 | $this->entity_uri_service = $entity_uri_service; |
| 52 | 52 | $this->post_id_to_jsonld_converter = $post_id_to_jsonld_converter; |
@@ -64,31 +64,31 @@ discard block |
||
| 64 | 64 | * |
| 65 | 65 | * @return array|boolean |
| 66 | 66 | */ |
| 67 | - public function get_carousel_jsonld( $id = null ) { |
|
| 68 | - $posts = $this->get_posts( $id ); |
|
| 67 | + public function get_carousel_jsonld($id = null) { |
|
| 68 | + $posts = $this->get_posts($id); |
|
| 69 | 69 | $post_jsonld = array(); |
| 70 | - if ( ! is_array( $posts ) || count( $posts ) < 2 ) { |
|
| 70 | + if ( ! is_array($posts) || count($posts) < 2) { |
|
| 71 | 71 | // Bail out if no posts are present. |
| 72 | 72 | return false; |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - if ( $id !== null ) { |
|
| 76 | - $term = get_term( $id ); |
|
| 77 | - $post_jsonld['description'] = wp_strip_all_tags( strip_shortcodes( $term->description ) ); |
|
| 78 | - $thumbnail_id = get_term_meta( $id, 'thumbnail_id', true ); |
|
| 79 | - if ( ! empty( $thumbnail_id ) ) { |
|
| 80 | - $post_jsonld['image'] = wp_get_attachment_url( $thumbnail_id ); |
|
| 75 | + if ($id !== null) { |
|
| 76 | + $term = get_term($id); |
|
| 77 | + $post_jsonld['description'] = wp_strip_all_tags(strip_shortcodes($term->description)); |
|
| 78 | + $thumbnail_id = get_term_meta($id, 'thumbnail_id', true); |
|
| 79 | + if ( ! empty($thumbnail_id)) { |
|
| 80 | + $post_jsonld['image'] = wp_get_attachment_url($thumbnail_id); |
|
| 81 | 81 | } |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | // More than 2 items are present, so construct the post_jsonld data |
| 85 | 85 | $post_jsonld['@context'] = 'https://schema.org'; |
| 86 | 86 | $post_jsonld['@type'] = 'ItemList'; |
| 87 | - $post_jsonld['url'] = $this->get_term_url( $id ); |
|
| 87 | + $post_jsonld['url'] = $this->get_term_url($id); |
|
| 88 | 88 | $post_jsonld['itemListElement'] = array(); |
| 89 | 89 | $position = 1; |
| 90 | 90 | |
| 91 | - foreach ( $posts as $post_id ) { |
|
| 91 | + foreach ($posts as $post_id) { |
|
| 92 | 92 | $result = array( |
| 93 | 93 | '@type' => 'ListItem', |
| 94 | 94 | 'position' => $position, |
@@ -97,34 +97,34 @@ discard block |
||
| 97 | 97 | * |
| 98 | 98 | * See https://developers.google.com/search/docs/data-types/carousel |
| 99 | 99 | */ |
| 100 | - 'url' => apply_filters( 'wl_carousel_post_list_item_url', get_permalink( $post_id ), $post_id ), |
|
| 100 | + 'url' => apply_filters('wl_carousel_post_list_item_url', get_permalink($post_id), $post_id), |
|
| 101 | 101 | ); |
| 102 | - array_push( $post_jsonld['itemListElement'], $result ); |
|
| 103 | - ++ $position; |
|
| 102 | + array_push($post_jsonld['itemListElement'], $result); |
|
| 103 | + ++$position; |
|
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | return $post_jsonld; |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - private function get_posts( $id ) { |
|
| 109 | + private function get_posts($id) { |
|
| 110 | 110 | global $wp_query; |
| 111 | 111 | |
| 112 | - if ( $wp_query->posts !== null ) { |
|
| 112 | + if ($wp_query->posts !== null) { |
|
| 113 | 113 | return array_map( |
| 114 | - function ( $post ) { |
|
| 114 | + function($post) { |
|
| 115 | 115 | return $post->ID; |
| 116 | 116 | }, |
| 117 | 117 | $wp_query->posts |
| 118 | 118 | ); |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - if ( $id === null ) { |
|
| 121 | + if ($id === null) { |
|
| 122 | 122 | return null; |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | - $term = get_term( $id ); |
|
| 125 | + $term = get_term($id); |
|
| 126 | 126 | |
| 127 | - return get_objects_in_term( $id, $term->taxonomy ); |
|
| 127 | + return get_objects_in_term($id, $term->taxonomy); |
|
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | /** |
@@ -136,34 +136,34 @@ discard block |
||
| 136 | 136 | $query_object = get_queried_object(); |
| 137 | 137 | |
| 138 | 138 | // Check if it is a term page. |
| 139 | - if ( ! $query_object instanceof WP_Term ) { |
|
| 139 | + if ( ! $query_object instanceof WP_Term) { |
|
| 140 | 140 | return; |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | // Bail out if `wl_jsonld_enabled` isn't enabled. |
| 144 | - if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) { |
|
| 144 | + if ( ! apply_filters('wl_jsonld_enabled', true)) { |
|
| 145 | 145 | return; |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | $term_id = $query_object->term_id; |
| 149 | 149 | |
| 150 | - $jsonld = $this->get( $term_id, Jsonld_Context_Enum::PAGE ); |
|
| 150 | + $jsonld = $this->get($term_id, Jsonld_Context_Enum::PAGE); |
|
| 151 | 151 | |
| 152 | 152 | // Bail out if the JSON-LD is empty. |
| 153 | - if ( empty( $jsonld ) ) { |
|
| 153 | + if (empty($jsonld)) { |
|
| 154 | 154 | return; |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | - $jsonld_string = wp_json_encode( $jsonld ); |
|
| 157 | + $jsonld_string = wp_json_encode($jsonld); |
|
| 158 | 158 | |
| 159 | - $jsonld_term_html_output = '<script type="application/ld+json" id="wl-jsonld-term">' . $jsonld_string . '</script>'; |
|
| 160 | - $jsonld_term_html_output = apply_filters( 'wl_jsonld_term_html_output', $jsonld_term_html_output, $term_id ); |
|
| 159 | + $jsonld_term_html_output = '<script type="application/ld+json" id="wl-jsonld-term">'.$jsonld_string.'</script>'; |
|
| 160 | + $jsonld_term_html_output = apply_filters('wl_jsonld_term_html_output', $jsonld_term_html_output, $term_id); |
|
| 161 | 161 | |
| 162 | 162 | echo $jsonld_term_html_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- It's an application/ld+json output. |
| 163 | 163 | |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | - public function get( $id, $context, $is_recursive_call = false ) { |
|
| 166 | + public function get($id, $context, $is_recursive_call = false) { |
|
| 167 | 167 | /** |
| 168 | 168 | * Support for carousel rich snippet, get jsonld data present |
| 169 | 169 | * for all the posts shown in the term page, and add the jsonld data |
@@ -175,17 +175,17 @@ discard block |
||
| 175 | 175 | */ |
| 176 | 176 | $jsonld_array = array(); |
| 177 | 177 | |
| 178 | - if ( Jsonld_Context_Enum::PAGE === $context ) { |
|
| 179 | - $carousel_data = $this->get_carousel_jsonld( $id ); |
|
| 180 | - if ( $carousel_data ) { |
|
| 178 | + if (Jsonld_Context_Enum::PAGE === $context) { |
|
| 179 | + $carousel_data = $this->get_carousel_jsonld($id); |
|
| 180 | + if ($carousel_data) { |
|
| 181 | 181 | $jsonld_array[] = $carousel_data; |
| 182 | 182 | } |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | - $entities_jsonld_array = $this->get_entity_jsonld( $id, $context ); |
|
| 185 | + $entities_jsonld_array = $this->get_entity_jsonld($id, $context); |
|
| 186 | 186 | |
| 187 | 187 | $result = array( |
| 188 | - 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
| 188 | + 'jsonld' => array_merge($jsonld_array, $entities_jsonld_array), |
|
| 189 | 189 | 'references' => array(), |
| 190 | 190 | ); |
| 191 | 191 | |
@@ -195,35 +195,35 @@ discard block |
||
| 195 | 195 | * @var $id int Term id |
| 196 | 196 | * @var $jsonld_array array An array containing jsonld for term and entities. |
| 197 | 197 | */ |
| 198 | - $arr = apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
| 198 | + $arr = apply_filters('wl_term_jsonld_array', $result, $id); |
|
| 199 | 199 | |
| 200 | 200 | $references = array(); |
| 201 | 201 | |
| 202 | 202 | // Don't expand nested references, it will lead to an infinite loop. |
| 203 | - if ( ! $is_recursive_call ) { |
|
| 203 | + if ( ! $is_recursive_call) { |
|
| 204 | 204 | /** |
| 205 | 205 | * @since 3.32.0 |
| 206 | 206 | * Expand the references returned by this filter. |
| 207 | 207 | */ |
| 208 | - $references = $this->expand_references( $arr['references'] ); |
|
| 208 | + $references = $this->expand_references($arr['references']); |
|
| 209 | 209 | } |
| 210 | 210 | |
| 211 | - $jsonld_array = array_merge( $arr['jsonld'], $references ); |
|
| 211 | + $jsonld_array = array_merge($arr['jsonld'], $references); |
|
| 212 | 212 | |
| 213 | 213 | return $jsonld_array; |
| 214 | 214 | } |
| 215 | 215 | |
| 216 | - private function get_term_url( $id ) { |
|
| 217 | - if ( null === $id ) { |
|
| 218 | - return isset( $_SERVER['REQUEST_URI'] ) ? filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL ) : ''; |
|
| 216 | + private function get_term_url($id) { |
|
| 217 | + if (null === $id) { |
|
| 218 | + return isset($_SERVER['REQUEST_URI']) ? filter_var(wp_unslash($_SERVER['REQUEST_URI']), FILTER_SANITIZE_URL) : ''; |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | - $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true ); |
|
| 222 | - if ( ! empty( $maybe_url ) ) { |
|
| 221 | + $maybe_url = get_term_meta($id, Wordlift_Url_Property_Service::META_KEY, true); |
|
| 222 | + if ( ! empty($maybe_url)) { |
|
| 223 | 223 | return $maybe_url; |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | - return get_term_link( $id ); |
|
| 226 | + return get_term_link($id); |
|
| 227 | 227 | } |
| 228 | 228 | |
| 229 | 229 | /** |
@@ -235,30 +235,30 @@ discard block |
||
| 235 | 235 | * @return array |
| 236 | 236 | */ |
| 237 | 237 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 238 | - private function get_entity_jsonld( $term_id, $context ) { |
|
| 238 | + private function get_entity_jsonld($term_id, $context) { |
|
| 239 | 239 | |
| 240 | 240 | // The `_wl_entity_id` are URIs. |
| 241 | - $entity_ids = get_term_meta( $term_id, '_wl_entity_id' ); |
|
| 241 | + $entity_ids = get_term_meta($term_id, '_wl_entity_id'); |
|
| 242 | 242 | $entity_uri_service = $this->entity_uri_service; |
| 243 | 243 | |
| 244 | 244 | $wordlift_jsonld_service = Wordlift_Jsonld_Service::get_instance(); |
| 245 | 245 | |
| 246 | 246 | $local_entity_ids = array_filter( |
| 247 | 247 | $entity_ids, |
| 248 | - function ( $uri ) use ( $entity_uri_service ) { |
|
| 249 | - return $entity_uri_service->is_internal( $uri ); |
|
| 248 | + function($uri) use ($entity_uri_service) { |
|
| 249 | + return $entity_uri_service->is_internal($uri); |
|
| 250 | 250 | } |
| 251 | 251 | ); |
| 252 | 252 | |
| 253 | 253 | // Bail out if there are no entities. |
| 254 | - if ( empty( $local_entity_ids ) ) { |
|
| 254 | + if (empty($local_entity_ids)) { |
|
| 255 | 255 | return array(); |
| 256 | 256 | } |
| 257 | 257 | |
| 258 | - $post = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) ); |
|
| 259 | - $entities_jsonld = $wordlift_jsonld_service->get_jsonld( false, $post->ID ); |
|
| 258 | + $post = $this->entity_uri_service->get_entity(array_shift($local_entity_ids)); |
|
| 259 | + $entities_jsonld = $wordlift_jsonld_service->get_jsonld(false, $post->ID); |
|
| 260 | 260 | // Reset the `url` to the term page. |
| 261 | - $entities_jsonld[0]['url'] = get_term_link( $term_id ); |
|
| 261 | + $entities_jsonld[0]['url'] = get_term_link($term_id); |
|
| 262 | 262 | |
| 263 | 263 | return $entities_jsonld; |
| 264 | 264 | } |
@@ -268,21 +268,21 @@ discard block |
||
| 268 | 268 | * |
| 269 | 269 | * @return array |
| 270 | 270 | */ |
| 271 | - private function expand_references( $references ) { |
|
| 272 | - if ( ! is_array( $references ) ) { |
|
| 271 | + private function expand_references($references) { |
|
| 272 | + if ( ! is_array($references)) { |
|
| 273 | 273 | return array(); |
| 274 | 274 | } |
| 275 | 275 | $references_jsonld = array(); |
| 276 | 276 | // Expand the references. |
| 277 | - foreach ( $references as $reference ) { |
|
| 278 | - if ( $reference instanceof Post_Reference ) { |
|
| 277 | + foreach ($references as $reference) { |
|
| 278 | + if ($reference instanceof Post_Reference) { |
|
| 279 | 279 | $post_id = $reference->get_id(); |
| 280 | - $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $post_id ); |
|
| 281 | - } elseif ( $reference instanceof Term_Reference ) { |
|
| 280 | + $references_jsonld[] = $this->post_id_to_jsonld_converter->convert($post_id); |
|
| 281 | + } elseif ($reference instanceof Term_Reference) { |
|
| 282 | 282 | // Second level references won't be expanded. |
| 283 | - $references_jsonld[] = current( $this->get( $reference->get_id(), Jsonld_Context_Enum::UNKNOWN, true ) ); |
|
| 284 | - } elseif ( is_numeric( $reference ) ) { |
|
| 285 | - $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference ); |
|
| 283 | + $references_jsonld[] = current($this->get($reference->get_id(), Jsonld_Context_Enum::UNKNOWN, true)); |
|
| 284 | + } elseif (is_numeric($reference)) { |
|
| 285 | + $references_jsonld[] = $this->post_id_to_jsonld_converter->convert($reference); |
|
| 286 | 286 | } |
| 287 | 287 | } |
| 288 | 288 | |
@@ -13,214 +13,214 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Wordlift_Entity_Post_To_Jsonld_Converter extends Wordlift_Abstract_Post_To_Jsonld_Converter { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * The {@link Wordlift_Schemaorg_Property_Service} or null if not provided. |
|
| 18 | - * |
|
| 19 | - * @since 3.20.0 |
|
| 20 | - * @access private |
|
| 21 | - * @var null|Wordlift_Schemaorg_Property_Service The {@link Wordlift_Schemaorg_Property_Service} or null if not provided. |
|
| 22 | - */ |
|
| 23 | - private $schemaorg_property_service; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * The {@link Wordlift_Post_To_Jsonld_Converter} is used to convert entities that also have the `article` term. |
|
| 27 | - * |
|
| 28 | - * @since 3.25.2 |
|
| 29 | - * @var \Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter The {@link Wordlift_Post_To_Jsonld_Converter} instance. |
|
| 30 | - */ |
|
| 31 | - private $post_to_jsonld_converter; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Wordlift_Entity_To_Jsonld_Converter constructor. |
|
| 35 | - * |
|
| 36 | - * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 37 | - * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 38 | - * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 39 | - * @param \Wordlift_Property_Getter $property_getter A {@link Wordlift_Property_Getter} instance. |
|
| 40 | - * @param \Wordlift_Schemaorg_Property_Service $schemaorg_property_service A {@link Wordlift_Schemaorg_Property_Service} instance. |
|
| 41 | - * @param \Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter The {@link Wordlift_Post_To_Jsonld_Converter} instance. |
|
| 42 | - * |
|
| 43 | - * @since 3.8.0 |
|
| 44 | - */ |
|
| 45 | - public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter, $schemaorg_property_service, $post_to_jsonld_converter ) { |
|
| 46 | - parent::__construct( $entity_type_service, $user_service, $attachment_service, $property_getter ); |
|
| 47 | - $this->schemaorg_property_service = $schemaorg_property_service; |
|
| 48 | - $this->post_to_jsonld_converter = $post_to_jsonld_converter; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 53 | - * found while processing the post is set in the $references array. |
|
| 54 | - * |
|
| 55 | - * @param int $post_id The {@link WP_Post} id. |
|
| 56 | - * @param array $references An array of entity references. |
|
| 57 | - * @param array $references_infos |
|
| 58 | - * |
|
| 59 | - * @return array A JSON-LD array. |
|
| 60 | - * @since 3.8.0 |
|
| 61 | - */ |
|
| 62 | - public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
| 63 | - |
|
| 64 | - // Get the post instance. |
|
| 65 | - $post = get_post( $post_id ); |
|
| 66 | - if ( null === $post ) { |
|
| 67 | - // Post not found. |
|
| 68 | - return null; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - // Get the base JSON-LD and the list of entities referenced by this entity. |
|
| 72 | - if ( has_term( 'article', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $post_id ) ) { |
|
| 73 | - $jsonld = $this->post_to_jsonld_converter->convert( $post_id, $references, $references_infos ); |
|
| 74 | - } else { |
|
| 75 | - $jsonld = parent::convert( $post_id, $references, $references_infos ); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - // Get the entity name. |
|
| 79 | - $jsonld['name'] = html_entity_decode( $post->post_title ); |
|
| 80 | - |
|
| 81 | - // 3.13.0, add alternate names. |
|
| 82 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 83 | - if ( 0 < count( $alternative_labels ) ) { |
|
| 84 | - $jsonld['alternateName'] = $alternative_labels; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - // Get the entity `@type` with custom fields set by the Wordlift_Schema_Service. |
|
| 88 | - // |
|
| 89 | - // This allows us to gather the basic properties as defined by the `Thing` entity type. |
|
| 90 | - // Get the configured type custom fields. |
|
| 91 | - $custom_fields = $this->entity_type_service->get_custom_fields_for_post( $post_id ); |
|
| 92 | - |
|
| 93 | - if ( isset( $custom_fields ) ) { |
|
| 94 | - $this->process_type_custom_fields( $jsonld, $custom_fields, $post, $references, $references_infos ); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /* |
|
| 16 | + /** |
|
| 17 | + * The {@link Wordlift_Schemaorg_Property_Service} or null if not provided. |
|
| 18 | + * |
|
| 19 | + * @since 3.20.0 |
|
| 20 | + * @access private |
|
| 21 | + * @var null|Wordlift_Schemaorg_Property_Service The {@link Wordlift_Schemaorg_Property_Service} or null if not provided. |
|
| 22 | + */ |
|
| 23 | + private $schemaorg_property_service; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * The {@link Wordlift_Post_To_Jsonld_Converter} is used to convert entities that also have the `article` term. |
|
| 27 | + * |
|
| 28 | + * @since 3.25.2 |
|
| 29 | + * @var \Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter The {@link Wordlift_Post_To_Jsonld_Converter} instance. |
|
| 30 | + */ |
|
| 31 | + private $post_to_jsonld_converter; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Wordlift_Entity_To_Jsonld_Converter constructor. |
|
| 35 | + * |
|
| 36 | + * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 37 | + * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 38 | + * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 39 | + * @param \Wordlift_Property_Getter $property_getter A {@link Wordlift_Property_Getter} instance. |
|
| 40 | + * @param \Wordlift_Schemaorg_Property_Service $schemaorg_property_service A {@link Wordlift_Schemaorg_Property_Service} instance. |
|
| 41 | + * @param \Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter The {@link Wordlift_Post_To_Jsonld_Converter} instance. |
|
| 42 | + * |
|
| 43 | + * @since 3.8.0 |
|
| 44 | + */ |
|
| 45 | + public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter, $schemaorg_property_service, $post_to_jsonld_converter ) { |
|
| 46 | + parent::__construct( $entity_type_service, $user_service, $attachment_service, $property_getter ); |
|
| 47 | + $this->schemaorg_property_service = $schemaorg_property_service; |
|
| 48 | + $this->post_to_jsonld_converter = $post_to_jsonld_converter; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 53 | + * found while processing the post is set in the $references array. |
|
| 54 | + * |
|
| 55 | + * @param int $post_id The {@link WP_Post} id. |
|
| 56 | + * @param array $references An array of entity references. |
|
| 57 | + * @param array $references_infos |
|
| 58 | + * |
|
| 59 | + * @return array A JSON-LD array. |
|
| 60 | + * @since 3.8.0 |
|
| 61 | + */ |
|
| 62 | + public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
| 63 | + |
|
| 64 | + // Get the post instance. |
|
| 65 | + $post = get_post( $post_id ); |
|
| 66 | + if ( null === $post ) { |
|
| 67 | + // Post not found. |
|
| 68 | + return null; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + // Get the base JSON-LD and the list of entities referenced by this entity. |
|
| 72 | + if ( has_term( 'article', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $post_id ) ) { |
|
| 73 | + $jsonld = $this->post_to_jsonld_converter->convert( $post_id, $references, $references_infos ); |
|
| 74 | + } else { |
|
| 75 | + $jsonld = parent::convert( $post_id, $references, $references_infos ); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + // Get the entity name. |
|
| 79 | + $jsonld['name'] = html_entity_decode( $post->post_title ); |
|
| 80 | + |
|
| 81 | + // 3.13.0, add alternate names. |
|
| 82 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 83 | + if ( 0 < count( $alternative_labels ) ) { |
|
| 84 | + $jsonld['alternateName'] = $alternative_labels; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + // Get the entity `@type` with custom fields set by the Wordlift_Schema_Service. |
|
| 88 | + // |
|
| 89 | + // This allows us to gather the basic properties as defined by the `Thing` entity type. |
|
| 90 | + // Get the configured type custom fields. |
|
| 91 | + $custom_fields = $this->entity_type_service->get_custom_fields_for_post( $post_id ); |
|
| 92 | + |
|
| 93 | + if ( isset( $custom_fields ) ) { |
|
| 94 | + $this->process_type_custom_fields( $jsonld, $custom_fields, $post, $references, $references_infos ); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /* |
|
| 98 | 98 | * Get the properties attached to the post. |
| 99 | 99 | * |
| 100 | 100 | * @since 3.20.0 We attach properties directly to the posts. |
| 101 | 101 | * |
| 102 | 102 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 103 | 103 | */ |
| 104 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 105 | - if ( apply_filters( 'wl_feature__enable__all-entity-types', WL_ALL_ENTITY_TYPES ) ) { |
|
| 106 | - $this->process_post_properties( $jsonld, $post_id ); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references. |
|
| 111 | - * |
|
| 112 | - * @param array $value { |
|
| 113 | - * |
|
| 114 | - * @type array $jsonld The JSON-LD structure. |
|
| 115 | - * @type int[] $references An array of post IDs. |
|
| 116 | - * } |
|
| 117 | - * @since 3.25.0 |
|
| 118 | - * |
|
| 119 | - * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/ |
|
| 120 | - * |
|
| 121 | - * @api |
|
| 122 | - */ |
|
| 123 | - $ret_val = apply_filters( |
|
| 124 | - 'wl_entity_jsonld_array', |
|
| 125 | - array( |
|
| 126 | - 'jsonld' => $jsonld, |
|
| 127 | - 'references' => $references, |
|
| 128 | - ), |
|
| 129 | - $post_id |
|
| 130 | - ); |
|
| 131 | - $jsonld = $ret_val['jsonld']; |
|
| 132 | - $references = $ret_val['references']; |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Call the `wl_entity_jsonld` filter. |
|
| 136 | - * |
|
| 137 | - * @param array $jsonld The JSON-LD structure. |
|
| 138 | - * @param int $post_id The {@link WP_Post} `id`. |
|
| 139 | - * @param array $references The array of referenced entities. |
|
| 140 | - * |
|
| 141 | - * @since 3.20.0 |
|
| 142 | - * |
|
| 143 | - * @api |
|
| 144 | - */ |
|
| 145 | - return apply_filters( 'wl_entity_jsonld', $this->post_process( $jsonld ), $post_id, $references ); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Process the properties attached to the {@link WP_Post}. |
|
| 150 | - * |
|
| 151 | - * @param array $jsonld The JSON-LD array. |
|
| 152 | - * @param int $post_id The target {@link WP_Post} id. |
|
| 153 | - * |
|
| 154 | - * @since 3.20.0 |
|
| 155 | - */ |
|
| 156 | - private function process_post_properties( &$jsonld, $post_id ) { |
|
| 157 | - |
|
| 158 | - // Get all the props. |
|
| 159 | - $props = $this->schemaorg_property_service->get_all( $post_id ); |
|
| 160 | - |
|
| 161 | - // Process all the props. |
|
| 162 | - foreach ( $props as $name => $instances ) { |
|
| 163 | - |
|
| 164 | - // Get the values. |
|
| 165 | - $values = array_map( |
|
| 166 | - function ( $instance ) { |
|
| 167 | - return $instance['value']; |
|
| 168 | - }, |
|
| 169 | - $instances |
|
| 170 | - ); |
|
| 171 | - |
|
| 172 | - // We might receive empty values, remove them. |
|
| 173 | - $non_empty_values = array_filter( |
|
| 174 | - $values, |
|
| 175 | - function ( $value ) { |
|
| 176 | - return ! empty( $value ); |
|
| 177 | - } |
|
| 178 | - ); |
|
| 179 | - |
|
| 180 | - // Skip empty properties. |
|
| 181 | - if ( empty( $non_empty_values ) ) { |
|
| 182 | - continue; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - // @@todo: need to handle maybe Numbers and URLs differently. |
|
| 186 | - // Make an array a single value when possible. |
|
| 187 | - $jsonld[ $name ] = self::make_one( $non_empty_values ); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Post process the generated JSON to reorganize values which are stored as 1st |
|
| 194 | - * level in WP but are really 2nd level. |
|
| 195 | - * |
|
| 196 | - * @param array $jsonld An array of JSON-LD properties and values. |
|
| 197 | - * |
|
| 198 | - * @return array The array remapped. |
|
| 199 | - * @since 3.8.0 |
|
| 200 | - */ |
|
| 201 | - public static function post_process( $jsonld ) { |
|
| 202 | - |
|
| 203 | - foreach ( $jsonld as $key => $value ) { |
|
| 204 | - if ( 'streetAddress' === $key || 'postalCode' === $key || 'addressLocality' === $key || 'addressRegion' === $key || 'addressCountry' === $key || 'postOfficeBoxNumber' === $key ) { |
|
| 205 | - $jsonld['address']['@type'] = 'PostalAddress'; |
|
| 206 | - $jsonld['address'][ $key ] = $value; |
|
| 207 | - unset( $jsonld[ $key ] ); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - if ( 'latitude' === $key || 'longitude' === $key ) { |
|
| 211 | - $jsonld['geo']['@type'] = 'GeoCoordinates'; |
|
| 212 | - $jsonld['geo'][ $key ] = $value; |
|
| 213 | - unset( $jsonld[ $key ] ); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - if ( 'calories' === $key ) { |
|
| 217 | - $jsonld['nutrition']['@type'] = 'NutritionInformation'; |
|
| 218 | - $jsonld['nutrition'][ $key ] = $value; |
|
| 219 | - unset( $jsonld[ $key ] ); |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - return $jsonld; |
|
| 224 | - } |
|
| 104 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 105 | + if ( apply_filters( 'wl_feature__enable__all-entity-types', WL_ALL_ENTITY_TYPES ) ) { |
|
| 106 | + $this->process_post_properties( $jsonld, $post_id ); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references. |
|
| 111 | + * |
|
| 112 | + * @param array $value { |
|
| 113 | + * |
|
| 114 | + * @type array $jsonld The JSON-LD structure. |
|
| 115 | + * @type int[] $references An array of post IDs. |
|
| 116 | + * } |
|
| 117 | + * @since 3.25.0 |
|
| 118 | + * |
|
| 119 | + * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/ |
|
| 120 | + * |
|
| 121 | + * @api |
|
| 122 | + */ |
|
| 123 | + $ret_val = apply_filters( |
|
| 124 | + 'wl_entity_jsonld_array', |
|
| 125 | + array( |
|
| 126 | + 'jsonld' => $jsonld, |
|
| 127 | + 'references' => $references, |
|
| 128 | + ), |
|
| 129 | + $post_id |
|
| 130 | + ); |
|
| 131 | + $jsonld = $ret_val['jsonld']; |
|
| 132 | + $references = $ret_val['references']; |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Call the `wl_entity_jsonld` filter. |
|
| 136 | + * |
|
| 137 | + * @param array $jsonld The JSON-LD structure. |
|
| 138 | + * @param int $post_id The {@link WP_Post} `id`. |
|
| 139 | + * @param array $references The array of referenced entities. |
|
| 140 | + * |
|
| 141 | + * @since 3.20.0 |
|
| 142 | + * |
|
| 143 | + * @api |
|
| 144 | + */ |
|
| 145 | + return apply_filters( 'wl_entity_jsonld', $this->post_process( $jsonld ), $post_id, $references ); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Process the properties attached to the {@link WP_Post}. |
|
| 150 | + * |
|
| 151 | + * @param array $jsonld The JSON-LD array. |
|
| 152 | + * @param int $post_id The target {@link WP_Post} id. |
|
| 153 | + * |
|
| 154 | + * @since 3.20.0 |
|
| 155 | + */ |
|
| 156 | + private function process_post_properties( &$jsonld, $post_id ) { |
|
| 157 | + |
|
| 158 | + // Get all the props. |
|
| 159 | + $props = $this->schemaorg_property_service->get_all( $post_id ); |
|
| 160 | + |
|
| 161 | + // Process all the props. |
|
| 162 | + foreach ( $props as $name => $instances ) { |
|
| 163 | + |
|
| 164 | + // Get the values. |
|
| 165 | + $values = array_map( |
|
| 166 | + function ( $instance ) { |
|
| 167 | + return $instance['value']; |
|
| 168 | + }, |
|
| 169 | + $instances |
|
| 170 | + ); |
|
| 171 | + |
|
| 172 | + // We might receive empty values, remove them. |
|
| 173 | + $non_empty_values = array_filter( |
|
| 174 | + $values, |
|
| 175 | + function ( $value ) { |
|
| 176 | + return ! empty( $value ); |
|
| 177 | + } |
|
| 178 | + ); |
|
| 179 | + |
|
| 180 | + // Skip empty properties. |
|
| 181 | + if ( empty( $non_empty_values ) ) { |
|
| 182 | + continue; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + // @@todo: need to handle maybe Numbers and URLs differently. |
|
| 186 | + // Make an array a single value when possible. |
|
| 187 | + $jsonld[ $name ] = self::make_one( $non_empty_values ); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Post process the generated JSON to reorganize values which are stored as 1st |
|
| 194 | + * level in WP but are really 2nd level. |
|
| 195 | + * |
|
| 196 | + * @param array $jsonld An array of JSON-LD properties and values. |
|
| 197 | + * |
|
| 198 | + * @return array The array remapped. |
|
| 199 | + * @since 3.8.0 |
|
| 200 | + */ |
|
| 201 | + public static function post_process( $jsonld ) { |
|
| 202 | + |
|
| 203 | + foreach ( $jsonld as $key => $value ) { |
|
| 204 | + if ( 'streetAddress' === $key || 'postalCode' === $key || 'addressLocality' === $key || 'addressRegion' === $key || 'addressCountry' === $key || 'postOfficeBoxNumber' === $key ) { |
|
| 205 | + $jsonld['address']['@type'] = 'PostalAddress'; |
|
| 206 | + $jsonld['address'][ $key ] = $value; |
|
| 207 | + unset( $jsonld[ $key ] ); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + if ( 'latitude' === $key || 'longitude' === $key ) { |
|
| 211 | + $jsonld['geo']['@type'] = 'GeoCoordinates'; |
|
| 212 | + $jsonld['geo'][ $key ] = $value; |
|
| 213 | + unset( $jsonld[ $key ] ); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + if ( 'calories' === $key ) { |
|
| 217 | + $jsonld['nutrition']['@type'] = 'NutritionInformation'; |
|
| 218 | + $jsonld['nutrition'][ $key ] = $value; |
|
| 219 | + unset( $jsonld[ $key ] ); |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + return $jsonld; |
|
| 224 | + } |
|
| 225 | 225 | |
| 226 | 226 | } |
@@ -42,8 +42,8 @@ discard block |
||
| 42 | 42 | * |
| 43 | 43 | * @since 3.8.0 |
| 44 | 44 | */ |
| 45 | - public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter, $schemaorg_property_service, $post_to_jsonld_converter ) { |
|
| 46 | - parent::__construct( $entity_type_service, $user_service, $attachment_service, $property_getter ); |
|
| 45 | + public function __construct($entity_type_service, $user_service, $attachment_service, $property_getter, $schemaorg_property_service, $post_to_jsonld_converter) { |
|
| 46 | + parent::__construct($entity_type_service, $user_service, $attachment_service, $property_getter); |
|
| 47 | 47 | $this->schemaorg_property_service = $schemaorg_property_service; |
| 48 | 48 | $this->post_to_jsonld_converter = $post_to_jsonld_converter; |
| 49 | 49 | } |
@@ -59,28 +59,28 @@ discard block |
||
| 59 | 59 | * @return array A JSON-LD array. |
| 60 | 60 | * @since 3.8.0 |
| 61 | 61 | */ |
| 62 | - public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
| 62 | + public function convert($post_id, &$references = array(), &$references_infos = array()) { |
|
| 63 | 63 | |
| 64 | 64 | // Get the post instance. |
| 65 | - $post = get_post( $post_id ); |
|
| 66 | - if ( null === $post ) { |
|
| 65 | + $post = get_post($post_id); |
|
| 66 | + if (null === $post) { |
|
| 67 | 67 | // Post not found. |
| 68 | 68 | return null; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | // Get the base JSON-LD and the list of entities referenced by this entity. |
| 72 | - if ( has_term( 'article', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $post_id ) ) { |
|
| 73 | - $jsonld = $this->post_to_jsonld_converter->convert( $post_id, $references, $references_infos ); |
|
| 72 | + if (has_term('article', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $post_id)) { |
|
| 73 | + $jsonld = $this->post_to_jsonld_converter->convert($post_id, $references, $references_infos); |
|
| 74 | 74 | } else { |
| 75 | - $jsonld = parent::convert( $post_id, $references, $references_infos ); |
|
| 75 | + $jsonld = parent::convert($post_id, $references, $references_infos); |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | // Get the entity name. |
| 79 | - $jsonld['name'] = html_entity_decode( $post->post_title ); |
|
| 79 | + $jsonld['name'] = html_entity_decode($post->post_title); |
|
| 80 | 80 | |
| 81 | 81 | // 3.13.0, add alternate names. |
| 82 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 83 | - if ( 0 < count( $alternative_labels ) ) { |
|
| 82 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels($post_id); |
|
| 83 | + if (0 < count($alternative_labels)) { |
|
| 84 | 84 | $jsonld['alternateName'] = $alternative_labels; |
| 85 | 85 | } |
| 86 | 86 | |
@@ -88,10 +88,10 @@ discard block |
||
| 88 | 88 | // |
| 89 | 89 | // This allows us to gather the basic properties as defined by the `Thing` entity type. |
| 90 | 90 | // Get the configured type custom fields. |
| 91 | - $custom_fields = $this->entity_type_service->get_custom_fields_for_post( $post_id ); |
|
| 91 | + $custom_fields = $this->entity_type_service->get_custom_fields_for_post($post_id); |
|
| 92 | 92 | |
| 93 | - if ( isset( $custom_fields ) ) { |
|
| 94 | - $this->process_type_custom_fields( $jsonld, $custom_fields, $post, $references, $references_infos ); |
|
| 93 | + if (isset($custom_fields)) { |
|
| 94 | + $this->process_type_custom_fields($jsonld, $custom_fields, $post, $references, $references_infos); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | /* |
@@ -102,8 +102,8 @@ discard block |
||
| 102 | 102 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 103 | 103 | */ |
| 104 | 104 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 105 | - if ( apply_filters( 'wl_feature__enable__all-entity-types', WL_ALL_ENTITY_TYPES ) ) { |
|
| 106 | - $this->process_post_properties( $jsonld, $post_id ); |
|
| 105 | + if (apply_filters('wl_feature__enable__all-entity-types', WL_ALL_ENTITY_TYPES)) { |
|
| 106 | + $this->process_post_properties($jsonld, $post_id); |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | /** |
@@ -120,7 +120,7 @@ discard block |
||
| 120 | 120 | * |
| 121 | 121 | * @api |
| 122 | 122 | */ |
| 123 | - $ret_val = apply_filters( |
|
| 123 | + $ret_val = apply_filters( |
|
| 124 | 124 | 'wl_entity_jsonld_array', |
| 125 | 125 | array( |
| 126 | 126 | 'jsonld' => $jsonld, |
@@ -142,7 +142,7 @@ discard block |
||
| 142 | 142 | * |
| 143 | 143 | * @api |
| 144 | 144 | */ |
| 145 | - return apply_filters( 'wl_entity_jsonld', $this->post_process( $jsonld ), $post_id, $references ); |
|
| 145 | + return apply_filters('wl_entity_jsonld', $this->post_process($jsonld), $post_id, $references); |
|
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | /** |
@@ -153,17 +153,17 @@ discard block |
||
| 153 | 153 | * |
| 154 | 154 | * @since 3.20.0 |
| 155 | 155 | */ |
| 156 | - private function process_post_properties( &$jsonld, $post_id ) { |
|
| 156 | + private function process_post_properties(&$jsonld, $post_id) { |
|
| 157 | 157 | |
| 158 | 158 | // Get all the props. |
| 159 | - $props = $this->schemaorg_property_service->get_all( $post_id ); |
|
| 159 | + $props = $this->schemaorg_property_service->get_all($post_id); |
|
| 160 | 160 | |
| 161 | 161 | // Process all the props. |
| 162 | - foreach ( $props as $name => $instances ) { |
|
| 162 | + foreach ($props as $name => $instances) { |
|
| 163 | 163 | |
| 164 | 164 | // Get the values. |
| 165 | 165 | $values = array_map( |
| 166 | - function ( $instance ) { |
|
| 166 | + function($instance) { |
|
| 167 | 167 | return $instance['value']; |
| 168 | 168 | }, |
| 169 | 169 | $instances |
@@ -172,19 +172,19 @@ discard block |
||
| 172 | 172 | // We might receive empty values, remove them. |
| 173 | 173 | $non_empty_values = array_filter( |
| 174 | 174 | $values, |
| 175 | - function ( $value ) { |
|
| 176 | - return ! empty( $value ); |
|
| 175 | + function($value) { |
|
| 176 | + return ! empty($value); |
|
| 177 | 177 | } |
| 178 | 178 | ); |
| 179 | 179 | |
| 180 | 180 | // Skip empty properties. |
| 181 | - if ( empty( $non_empty_values ) ) { |
|
| 181 | + if (empty($non_empty_values)) { |
|
| 182 | 182 | continue; |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | // @@todo: need to handle maybe Numbers and URLs differently. |
| 186 | 186 | // Make an array a single value when possible. |
| 187 | - $jsonld[ $name ] = self::make_one( $non_empty_values ); |
|
| 187 | + $jsonld[$name] = self::make_one($non_empty_values); |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | } |
@@ -198,25 +198,25 @@ discard block |
||
| 198 | 198 | * @return array The array remapped. |
| 199 | 199 | * @since 3.8.0 |
| 200 | 200 | */ |
| 201 | - public static function post_process( $jsonld ) { |
|
| 201 | + public static function post_process($jsonld) { |
|
| 202 | 202 | |
| 203 | - foreach ( $jsonld as $key => $value ) { |
|
| 204 | - if ( 'streetAddress' === $key || 'postalCode' === $key || 'addressLocality' === $key || 'addressRegion' === $key || 'addressCountry' === $key || 'postOfficeBoxNumber' === $key ) { |
|
| 203 | + foreach ($jsonld as $key => $value) { |
|
| 204 | + if ('streetAddress' === $key || 'postalCode' === $key || 'addressLocality' === $key || 'addressRegion' === $key || 'addressCountry' === $key || 'postOfficeBoxNumber' === $key) { |
|
| 205 | 205 | $jsonld['address']['@type'] = 'PostalAddress'; |
| 206 | - $jsonld['address'][ $key ] = $value; |
|
| 207 | - unset( $jsonld[ $key ] ); |
|
| 206 | + $jsonld['address'][$key] = $value; |
|
| 207 | + unset($jsonld[$key]); |
|
| 208 | 208 | } |
| 209 | 209 | |
| 210 | - if ( 'latitude' === $key || 'longitude' === $key ) { |
|
| 210 | + if ('latitude' === $key || 'longitude' === $key) { |
|
| 211 | 211 | $jsonld['geo']['@type'] = 'GeoCoordinates'; |
| 212 | - $jsonld['geo'][ $key ] = $value; |
|
| 213 | - unset( $jsonld[ $key ] ); |
|
| 212 | + $jsonld['geo'][$key] = $value; |
|
| 213 | + unset($jsonld[$key]); |
|
| 214 | 214 | } |
| 215 | 215 | |
| 216 | - if ( 'calories' === $key ) { |
|
| 216 | + if ('calories' === $key) { |
|
| 217 | 217 | $jsonld['nutrition']['@type'] = 'NutritionInformation'; |
| 218 | - $jsonld['nutrition'][ $key ] = $value; |
|
| 219 | - unset( $jsonld[ $key ] ); |
|
| 218 | + $jsonld['nutrition'][$key] = $value; |
|
| 219 | + unset($jsonld[$key]); |
|
| 220 | 220 | } |
| 221 | 221 | } |
| 222 | 222 | |
@@ -11,153 +11,153 @@ |
||
| 11 | 11 | |
| 12 | 12 | class Jsonld_Generator { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * @var \Wordlift_Entity_Type_Service |
|
| 16 | - */ |
|
| 17 | - private $entity_type_service; |
|
| 18 | - /** |
|
| 19 | - * @var \Wordlift_Property_Getter |
|
| 20 | - */ |
|
| 21 | - private $property_getter; |
|
| 22 | - /** |
|
| 23 | - * @var Type_Service |
|
| 24 | - */ |
|
| 25 | - private $term_entity_type_service; |
|
| 26 | - /** |
|
| 27 | - * @var \Wordlift_Entity_Service |
|
| 28 | - */ |
|
| 29 | - private $entity_service; |
|
| 30 | - |
|
| 31 | - public function __construct( $entity_type_service, $property_getter ) { |
|
| 32 | - $this->entity_type_service = $entity_type_service; |
|
| 33 | - $this->property_getter = $property_getter; |
|
| 34 | - $this->term_entity_type_service = Type_Service::get_instance(); |
|
| 35 | - $this->entity_service = \Wordlift_Entity_Service::get_instance(); |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - public function init() { |
|
| 39 | - add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - public function wl_term_jsonld_array( $data, $term_id ) { |
|
| 43 | - $jsonld = $data['jsonld']; |
|
| 44 | - $references = $data['references']; |
|
| 45 | - |
|
| 46 | - $term_jsonld_data = $this->get_jsonld_data_for_term( $term_id ); |
|
| 47 | - |
|
| 48 | - // Return early if we dont have the entity data |
|
| 49 | - // for the term. |
|
| 50 | - if ( ! $term_jsonld_data ) { |
|
| 51 | - return $data; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - $term_jsonld = $term_jsonld_data['jsonld']; |
|
| 55 | - |
|
| 56 | - $references = array_merge( $references, $term_jsonld_data['references'] ); |
|
| 57 | - |
|
| 58 | - array_unshift( $jsonld, $term_jsonld ); |
|
| 59 | - |
|
| 60 | - return array( |
|
| 61 | - 'jsonld' => $jsonld, |
|
| 62 | - 'references' => $references, |
|
| 63 | - ); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - private function get_jsonld_data_for_term( $term_id ) { |
|
| 67 | - |
|
| 68 | - $id = $this->entity_service->get_uri( $term_id, Object_Type_Enum::TERM ); |
|
| 69 | - |
|
| 70 | - // If we don't have a dataset URI, then don't publish the term data |
|
| 71 | - // on this page. |
|
| 72 | - if ( ! $id ) { |
|
| 73 | - return false; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - $references = array(); |
|
| 77 | - $term = get_term( $term_id ); |
|
| 78 | - $permalink = get_term_link( $term ); |
|
| 79 | - |
|
| 80 | - $custom_fields = $this->entity_type_service->get_custom_fields_for_term( $term_id ); |
|
| 81 | - $term = get_term( $term_id ); |
|
| 82 | - $jsonld = array( |
|
| 83 | - '@context' => 'http://schema.org', |
|
| 84 | - 'name' => $term->name, |
|
| 85 | - '@type' => $this->term_entity_type_service->get_entity_types_labels( $term_id ), |
|
| 86 | - '@id' => $id, |
|
| 87 | - 'description' => $term->description, |
|
| 88 | - ); |
|
| 89 | - |
|
| 90 | - if ( ! $custom_fields || ! is_array( $custom_fields ) ) { |
|
| 91 | - return $jsonld; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - foreach ( $custom_fields as $key => $value ) { |
|
| 95 | - $name = $this->relative_to_schema_context( $value['predicate'] ); |
|
| 96 | - $value = $this->property_getter->get( $term_id, $key, Object_Type_Enum::TERM ); |
|
| 97 | - $value = $this->process_value( $value, $references ); |
|
| 98 | - if ( ! isset( $value ) || |
|
| 99 | - is_array( $value ) && empty( $value ) || |
|
| 100 | - is_string( $value ) && empty( $value ) ) { |
|
| 101 | - continue; |
|
| 102 | - } |
|
| 103 | - $jsonld[ $name ] = $value; |
|
| 104 | - |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - $jsonld = \Wordlift_Entity_Post_To_Jsonld_Converter::post_process( $jsonld ); |
|
| 108 | - |
|
| 109 | - if ( $permalink ) { |
|
| 110 | - $jsonld['mainEntityOfPage'] = $permalink; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - return apply_filters( |
|
| 114 | - 'wl_no_vocabulary_term_jsonld_array', |
|
| 115 | - array( |
|
| 116 | - 'jsonld' => $jsonld, |
|
| 117 | - 'references' => $references, |
|
| 118 | - ), |
|
| 119 | - $term_id |
|
| 120 | - ); |
|
| 121 | - |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - private function relative_to_schema_context( $predicate ) { |
|
| 125 | - return str_replace( 'http://schema.org/', '', $predicate ); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - private function process_value( $value, &$references ) { |
|
| 129 | - |
|
| 130 | - if ( is_array( $value ) |
|
| 131 | - && count( $value ) > 0 |
|
| 132 | - && $value[0] instanceof \Wordlift_Property_Entity_Reference ) { |
|
| 133 | - |
|
| 134 | - // All of the references from the custom fields are post references. |
|
| 135 | - $references = array_merge( |
|
| 136 | - $references, |
|
| 137 | - array_map( |
|
| 138 | - function ( $property_entity_reference ) { |
|
| 139 | - /** |
|
| 140 | - * @var $property_entity_reference \Wordlift_Property_Entity_Reference |
|
| 141 | - */ |
|
| 142 | - return $property_entity_reference->to_reference(); |
|
| 143 | - }, |
|
| 144 | - $value |
|
| 145 | - ) |
|
| 146 | - ); |
|
| 147 | - |
|
| 148 | - return array_map( |
|
| 149 | - function ( $reference ) { |
|
| 150 | - /** |
|
| 151 | - * @var $reference \Wordlift_Property_Entity_Reference |
|
| 152 | - */ |
|
| 153 | - return array( '@id' => $reference->get_url() ); |
|
| 154 | - }, |
|
| 155 | - $value |
|
| 156 | - ); |
|
| 157 | - |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - return $value; |
|
| 161 | - } |
|
| 14 | + /** |
|
| 15 | + * @var \Wordlift_Entity_Type_Service |
|
| 16 | + */ |
|
| 17 | + private $entity_type_service; |
|
| 18 | + /** |
|
| 19 | + * @var \Wordlift_Property_Getter |
|
| 20 | + */ |
|
| 21 | + private $property_getter; |
|
| 22 | + /** |
|
| 23 | + * @var Type_Service |
|
| 24 | + */ |
|
| 25 | + private $term_entity_type_service; |
|
| 26 | + /** |
|
| 27 | + * @var \Wordlift_Entity_Service |
|
| 28 | + */ |
|
| 29 | + private $entity_service; |
|
| 30 | + |
|
| 31 | + public function __construct( $entity_type_service, $property_getter ) { |
|
| 32 | + $this->entity_type_service = $entity_type_service; |
|
| 33 | + $this->property_getter = $property_getter; |
|
| 34 | + $this->term_entity_type_service = Type_Service::get_instance(); |
|
| 35 | + $this->entity_service = \Wordlift_Entity_Service::get_instance(); |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + public function init() { |
|
| 39 | + add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + public function wl_term_jsonld_array( $data, $term_id ) { |
|
| 43 | + $jsonld = $data['jsonld']; |
|
| 44 | + $references = $data['references']; |
|
| 45 | + |
|
| 46 | + $term_jsonld_data = $this->get_jsonld_data_for_term( $term_id ); |
|
| 47 | + |
|
| 48 | + // Return early if we dont have the entity data |
|
| 49 | + // for the term. |
|
| 50 | + if ( ! $term_jsonld_data ) { |
|
| 51 | + return $data; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + $term_jsonld = $term_jsonld_data['jsonld']; |
|
| 55 | + |
|
| 56 | + $references = array_merge( $references, $term_jsonld_data['references'] ); |
|
| 57 | + |
|
| 58 | + array_unshift( $jsonld, $term_jsonld ); |
|
| 59 | + |
|
| 60 | + return array( |
|
| 61 | + 'jsonld' => $jsonld, |
|
| 62 | + 'references' => $references, |
|
| 63 | + ); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + private function get_jsonld_data_for_term( $term_id ) { |
|
| 67 | + |
|
| 68 | + $id = $this->entity_service->get_uri( $term_id, Object_Type_Enum::TERM ); |
|
| 69 | + |
|
| 70 | + // If we don't have a dataset URI, then don't publish the term data |
|
| 71 | + // on this page. |
|
| 72 | + if ( ! $id ) { |
|
| 73 | + return false; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + $references = array(); |
|
| 77 | + $term = get_term( $term_id ); |
|
| 78 | + $permalink = get_term_link( $term ); |
|
| 79 | + |
|
| 80 | + $custom_fields = $this->entity_type_service->get_custom_fields_for_term( $term_id ); |
|
| 81 | + $term = get_term( $term_id ); |
|
| 82 | + $jsonld = array( |
|
| 83 | + '@context' => 'http://schema.org', |
|
| 84 | + 'name' => $term->name, |
|
| 85 | + '@type' => $this->term_entity_type_service->get_entity_types_labels( $term_id ), |
|
| 86 | + '@id' => $id, |
|
| 87 | + 'description' => $term->description, |
|
| 88 | + ); |
|
| 89 | + |
|
| 90 | + if ( ! $custom_fields || ! is_array( $custom_fields ) ) { |
|
| 91 | + return $jsonld; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + foreach ( $custom_fields as $key => $value ) { |
|
| 95 | + $name = $this->relative_to_schema_context( $value['predicate'] ); |
|
| 96 | + $value = $this->property_getter->get( $term_id, $key, Object_Type_Enum::TERM ); |
|
| 97 | + $value = $this->process_value( $value, $references ); |
|
| 98 | + if ( ! isset( $value ) || |
|
| 99 | + is_array( $value ) && empty( $value ) || |
|
| 100 | + is_string( $value ) && empty( $value ) ) { |
|
| 101 | + continue; |
|
| 102 | + } |
|
| 103 | + $jsonld[ $name ] = $value; |
|
| 104 | + |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + $jsonld = \Wordlift_Entity_Post_To_Jsonld_Converter::post_process( $jsonld ); |
|
| 108 | + |
|
| 109 | + if ( $permalink ) { |
|
| 110 | + $jsonld['mainEntityOfPage'] = $permalink; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + return apply_filters( |
|
| 114 | + 'wl_no_vocabulary_term_jsonld_array', |
|
| 115 | + array( |
|
| 116 | + 'jsonld' => $jsonld, |
|
| 117 | + 'references' => $references, |
|
| 118 | + ), |
|
| 119 | + $term_id |
|
| 120 | + ); |
|
| 121 | + |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + private function relative_to_schema_context( $predicate ) { |
|
| 125 | + return str_replace( 'http://schema.org/', '', $predicate ); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + private function process_value( $value, &$references ) { |
|
| 129 | + |
|
| 130 | + if ( is_array( $value ) |
|
| 131 | + && count( $value ) > 0 |
|
| 132 | + && $value[0] instanceof \Wordlift_Property_Entity_Reference ) { |
|
| 133 | + |
|
| 134 | + // All of the references from the custom fields are post references. |
|
| 135 | + $references = array_merge( |
|
| 136 | + $references, |
|
| 137 | + array_map( |
|
| 138 | + function ( $property_entity_reference ) { |
|
| 139 | + /** |
|
| 140 | + * @var $property_entity_reference \Wordlift_Property_Entity_Reference |
|
| 141 | + */ |
|
| 142 | + return $property_entity_reference->to_reference(); |
|
| 143 | + }, |
|
| 144 | + $value |
|
| 145 | + ) |
|
| 146 | + ); |
|
| 147 | + |
|
| 148 | + return array_map( |
|
| 149 | + function ( $reference ) { |
|
| 150 | + /** |
|
| 151 | + * @var $reference \Wordlift_Property_Entity_Reference |
|
| 152 | + */ |
|
| 153 | + return array( '@id' => $reference->get_url() ); |
|
| 154 | + }, |
|
| 155 | + $value |
|
| 156 | + ); |
|
| 157 | + |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + return $value; |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | 163 | } |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | */ |
| 29 | 29 | private $entity_service; |
| 30 | 30 | |
| 31 | - public function __construct( $entity_type_service, $property_getter ) { |
|
| 31 | + public function __construct($entity_type_service, $property_getter) { |
|
| 32 | 32 | $this->entity_type_service = $entity_type_service; |
| 33 | 33 | $this->property_getter = $property_getter; |
| 34 | 34 | $this->term_entity_type_service = Type_Service::get_instance(); |
@@ -36,26 +36,26 @@ discard block |
||
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | public function init() { |
| 39 | - add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 39 | + add_filter('wl_term_jsonld_array', array($this, 'wl_term_jsonld_array'), 10, 2); |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - public function wl_term_jsonld_array( $data, $term_id ) { |
|
| 42 | + public function wl_term_jsonld_array($data, $term_id) { |
|
| 43 | 43 | $jsonld = $data['jsonld']; |
| 44 | 44 | $references = $data['references']; |
| 45 | 45 | |
| 46 | - $term_jsonld_data = $this->get_jsonld_data_for_term( $term_id ); |
|
| 46 | + $term_jsonld_data = $this->get_jsonld_data_for_term($term_id); |
|
| 47 | 47 | |
| 48 | 48 | // Return early if we dont have the entity data |
| 49 | 49 | // for the term. |
| 50 | - if ( ! $term_jsonld_data ) { |
|
| 50 | + if ( ! $term_jsonld_data) { |
|
| 51 | 51 | return $data; |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | $term_jsonld = $term_jsonld_data['jsonld']; |
| 55 | 55 | |
| 56 | - $references = array_merge( $references, $term_jsonld_data['references'] ); |
|
| 56 | + $references = array_merge($references, $term_jsonld_data['references']); |
|
| 57 | 57 | |
| 58 | - array_unshift( $jsonld, $term_jsonld ); |
|
| 58 | + array_unshift($jsonld, $term_jsonld); |
|
| 59 | 59 | |
| 60 | 60 | return array( |
| 61 | 61 | 'jsonld' => $jsonld, |
@@ -63,50 +63,50 @@ discard block |
||
| 63 | 63 | ); |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | - private function get_jsonld_data_for_term( $term_id ) { |
|
| 66 | + private function get_jsonld_data_for_term($term_id) { |
|
| 67 | 67 | |
| 68 | - $id = $this->entity_service->get_uri( $term_id, Object_Type_Enum::TERM ); |
|
| 68 | + $id = $this->entity_service->get_uri($term_id, Object_Type_Enum::TERM); |
|
| 69 | 69 | |
| 70 | 70 | // If we don't have a dataset URI, then don't publish the term data |
| 71 | 71 | // on this page. |
| 72 | - if ( ! $id ) { |
|
| 72 | + if ( ! $id) { |
|
| 73 | 73 | return false; |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | $references = array(); |
| 77 | - $term = get_term( $term_id ); |
|
| 78 | - $permalink = get_term_link( $term ); |
|
| 77 | + $term = get_term($term_id); |
|
| 78 | + $permalink = get_term_link($term); |
|
| 79 | 79 | |
| 80 | - $custom_fields = $this->entity_type_service->get_custom_fields_for_term( $term_id ); |
|
| 81 | - $term = get_term( $term_id ); |
|
| 80 | + $custom_fields = $this->entity_type_service->get_custom_fields_for_term($term_id); |
|
| 81 | + $term = get_term($term_id); |
|
| 82 | 82 | $jsonld = array( |
| 83 | 83 | '@context' => 'http://schema.org', |
| 84 | 84 | 'name' => $term->name, |
| 85 | - '@type' => $this->term_entity_type_service->get_entity_types_labels( $term_id ), |
|
| 85 | + '@type' => $this->term_entity_type_service->get_entity_types_labels($term_id), |
|
| 86 | 86 | '@id' => $id, |
| 87 | 87 | 'description' => $term->description, |
| 88 | 88 | ); |
| 89 | 89 | |
| 90 | - if ( ! $custom_fields || ! is_array( $custom_fields ) ) { |
|
| 90 | + if ( ! $custom_fields || ! is_array($custom_fields)) { |
|
| 91 | 91 | return $jsonld; |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | - foreach ( $custom_fields as $key => $value ) { |
|
| 95 | - $name = $this->relative_to_schema_context( $value['predicate'] ); |
|
| 96 | - $value = $this->property_getter->get( $term_id, $key, Object_Type_Enum::TERM ); |
|
| 97 | - $value = $this->process_value( $value, $references ); |
|
| 98 | - if ( ! isset( $value ) || |
|
| 99 | - is_array( $value ) && empty( $value ) || |
|
| 100 | - is_string( $value ) && empty( $value ) ) { |
|
| 94 | + foreach ($custom_fields as $key => $value) { |
|
| 95 | + $name = $this->relative_to_schema_context($value['predicate']); |
|
| 96 | + $value = $this->property_getter->get($term_id, $key, Object_Type_Enum::TERM); |
|
| 97 | + $value = $this->process_value($value, $references); |
|
| 98 | + if ( ! isset($value) || |
|
| 99 | + is_array($value) && empty($value) || |
|
| 100 | + is_string($value) && empty($value)) { |
|
| 101 | 101 | continue; |
| 102 | 102 | } |
| 103 | - $jsonld[ $name ] = $value; |
|
| 103 | + $jsonld[$name] = $value; |
|
| 104 | 104 | |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | - $jsonld = \Wordlift_Entity_Post_To_Jsonld_Converter::post_process( $jsonld ); |
|
| 107 | + $jsonld = \Wordlift_Entity_Post_To_Jsonld_Converter::post_process($jsonld); |
|
| 108 | 108 | |
| 109 | - if ( $permalink ) { |
|
| 109 | + if ($permalink) { |
|
| 110 | 110 | $jsonld['mainEntityOfPage'] = $permalink; |
| 111 | 111 | } |
| 112 | 112 | |
@@ -121,21 +121,21 @@ discard block |
||
| 121 | 121 | |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | - private function relative_to_schema_context( $predicate ) { |
|
| 125 | - return str_replace( 'http://schema.org/', '', $predicate ); |
|
| 124 | + private function relative_to_schema_context($predicate) { |
|
| 125 | + return str_replace('http://schema.org/', '', $predicate); |
|
| 126 | 126 | } |
| 127 | 127 | |
| 128 | - private function process_value( $value, &$references ) { |
|
| 128 | + private function process_value($value, &$references) { |
|
| 129 | 129 | |
| 130 | - if ( is_array( $value ) |
|
| 131 | - && count( $value ) > 0 |
|
| 132 | - && $value[0] instanceof \Wordlift_Property_Entity_Reference ) { |
|
| 130 | + if (is_array($value) |
|
| 131 | + && count($value) > 0 |
|
| 132 | + && $value[0] instanceof \Wordlift_Property_Entity_Reference) { |
|
| 133 | 133 | |
| 134 | 134 | // All of the references from the custom fields are post references. |
| 135 | 135 | $references = array_merge( |
| 136 | 136 | $references, |
| 137 | 137 | array_map( |
| 138 | - function ( $property_entity_reference ) { |
|
| 138 | + function($property_entity_reference) { |
|
| 139 | 139 | /** |
| 140 | 140 | * @var $property_entity_reference \Wordlift_Property_Entity_Reference |
| 141 | 141 | */ |
@@ -146,11 +146,11 @@ discard block |
||
| 146 | 146 | ); |
| 147 | 147 | |
| 148 | 148 | return array_map( |
| 149 | - function ( $reference ) { |
|
| 149 | + function($reference) { |
|
| 150 | 150 | /** |
| 151 | 151 | * @var $reference \Wordlift_Property_Entity_Reference |
| 152 | 152 | */ |
| 153 | - return array( '@id' => $reference->get_url() ); |
|
| 153 | + return array('@id' => $reference->get_url()); |
|
| 154 | 154 | }, |
| 155 | 155 | $value |
| 156 | 156 | ); |
@@ -15,122 +15,122 @@ |
||
| 15 | 15 | |
| 16 | 16 | class Entity_Query_Service { |
| 17 | 17 | |
| 18 | - private static $instance = null; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * The singleton instance. |
|
| 22 | - * |
|
| 23 | - * @return Entity_Query_Service |
|
| 24 | - */ |
|
| 25 | - public static function get_instance() { |
|
| 26 | - if ( ! isset( self::$instance ) ) { |
|
| 27 | - self::$instance = new self(); |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - return self::$instance; |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - private function query_posts( $query, $schema_types, $limit ) { |
|
| 34 | - return wl_entity_get_by_title( $query, true, true, $limit, $schema_types ); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - private function query_terms( $query, $schema_types, $limit ) { |
|
| 38 | - global $wpdb; |
|
| 39 | - $schema_types = join( |
|
| 40 | - ',', |
|
| 41 | - array_map( |
|
| 42 | - function ( $schema_type ) { |
|
| 43 | - return "'" . esc_sql( strtolower( $schema_type ) ) . "'"; |
|
| 44 | - }, |
|
| 45 | - $schema_types |
|
| 46 | - ) |
|
| 47 | - ); |
|
| 48 | - |
|
| 49 | - return $wpdb->get_results( |
|
| 50 | - $wpdb->prepare( |
|
| 51 | - "SELECT DISTINCT t.term_id as id, t.name as title, tm.meta_value as schema_type_name FROM $wpdb->terms t INNER JOIN $wpdb->termmeta tm |
|
| 18 | + private static $instance = null; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * The singleton instance. |
|
| 22 | + * |
|
| 23 | + * @return Entity_Query_Service |
|
| 24 | + */ |
|
| 25 | + public static function get_instance() { |
|
| 26 | + if ( ! isset( self::$instance ) ) { |
|
| 27 | + self::$instance = new self(); |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + return self::$instance; |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + private function query_posts( $query, $schema_types, $limit ) { |
|
| 34 | + return wl_entity_get_by_title( $query, true, true, $limit, $schema_types ); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + private function query_terms( $query, $schema_types, $limit ) { |
|
| 38 | + global $wpdb; |
|
| 39 | + $schema_types = join( |
|
| 40 | + ',', |
|
| 41 | + array_map( |
|
| 42 | + function ( $schema_type ) { |
|
| 43 | + return "'" . esc_sql( strtolower( $schema_type ) ) . "'"; |
|
| 44 | + }, |
|
| 45 | + $schema_types |
|
| 46 | + ) |
|
| 47 | + ); |
|
| 48 | + |
|
| 49 | + return $wpdb->get_results( |
|
| 50 | + $wpdb->prepare( |
|
| 51 | + "SELECT DISTINCT t.term_id as id, t.name as title, tm.meta_value as schema_type_name FROM $wpdb->terms t INNER JOIN $wpdb->termmeta tm |
|
| 52 | 52 | ON t.term_id=tm.term_id WHERE t.name LIKE %s AND (tm.meta_key = %s AND tm.meta_value IN ($schema_types)) LIMIT %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 53 | - '%' . $wpdb->esc_like( $query ) . '%', |
|
| 54 | - \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 55 | - $limit |
|
| 56 | - ) |
|
| 57 | - ); |
|
| 58 | - |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @param $results |
|
| 63 | - * @param $object_type |
|
| 64 | - * |
|
| 65 | - * @return Entity[] |
|
| 66 | - */ |
|
| 67 | - private function transform_posts( $results ) { |
|
| 68 | - return array_map( |
|
| 69 | - function ( $item ) { |
|
| 70 | - return new Entity( $item->schema_type_name, new Wordpress_Content( get_post( $item->id ) ) ); |
|
| 71 | - }, |
|
| 72 | - $results |
|
| 73 | - ); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - private function transform_terms( $results ) { |
|
| 77 | - return array_map( |
|
| 78 | - function ( $item ) { |
|
| 79 | - return new Entity( $item->schema_type_name, new Wordpress_Content( get_term( $item->id ) ) ); |
|
| 80 | - }, |
|
| 81 | - $results |
|
| 82 | - ); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @param $query |
|
| 87 | - * @param $schema_types |
|
| 88 | - * @param $limit |
|
| 89 | - * |
|
| 90 | - * @return Entity[] |
|
| 91 | - */ |
|
| 92 | - public function query( $query, $schema_types = array(), $limit = 10 ) { |
|
| 93 | - |
|
| 94 | - $results = $this->transform_posts( $this->query_posts( $query, $schema_types, $limit ) ); |
|
| 95 | - |
|
| 96 | - if ( count( $results ) >= $limit ) { |
|
| 97 | - return $results; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $results = array_merge( $results, $this->transform_terms( $this->query_terms( $query, $schema_types, $limit ) ) ); |
|
| 101 | - |
|
| 102 | - return $results; |
|
| 103 | - |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - public function get( $linked_entities ) { |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @var $term_type_service Type_Service |
|
| 110 | - */ |
|
| 111 | - $term_type_service = Type_Service::get_instance(); |
|
| 112 | - $post_entity_type_service = \Wordlift_Entity_Type_Service::get_instance(); |
|
| 113 | - |
|
| 114 | - return array_filter( |
|
| 115 | - array_map( |
|
| 116 | - function ( $item ) use ( $term_type_service, $post_entity_type_service ) { |
|
| 117 | - $parts = explode( '_', $item ); |
|
| 118 | - $type = Object_Type_Enum::from_string( $parts[0] ); |
|
| 119 | - $identifier = $parts[1]; |
|
| 120 | - |
|
| 121 | - if ( Object_Type_Enum::POST === $type ) { |
|
| 122 | - return new Entity( join( ',', $post_entity_type_service->get_names( $identifier ) ), new Wordpress_Content( get_post( $identifier ) ) ); |
|
| 123 | - } elseif ( Object_Type_Enum::TERM === $type ) { |
|
| 124 | - |
|
| 125 | - return new Entity( join( ',', $term_type_service->get_entity_types_labels( $identifier ) ), new Wordpress_Content( get_term( $identifier ) ) ); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - // return new Entity( $item->schema_type_name, new Wordpress_Content( get_term( $item->id ) ) ); |
|
| 129 | - }, |
|
| 130 | - $linked_entities |
|
| 131 | - ) |
|
| 132 | - ); |
|
| 133 | - |
|
| 134 | - } |
|
| 53 | + '%' . $wpdb->esc_like( $query ) . '%', |
|
| 54 | + \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 55 | + $limit |
|
| 56 | + ) |
|
| 57 | + ); |
|
| 58 | + |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @param $results |
|
| 63 | + * @param $object_type |
|
| 64 | + * |
|
| 65 | + * @return Entity[] |
|
| 66 | + */ |
|
| 67 | + private function transform_posts( $results ) { |
|
| 68 | + return array_map( |
|
| 69 | + function ( $item ) { |
|
| 70 | + return new Entity( $item->schema_type_name, new Wordpress_Content( get_post( $item->id ) ) ); |
|
| 71 | + }, |
|
| 72 | + $results |
|
| 73 | + ); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + private function transform_terms( $results ) { |
|
| 77 | + return array_map( |
|
| 78 | + function ( $item ) { |
|
| 79 | + return new Entity( $item->schema_type_name, new Wordpress_Content( get_term( $item->id ) ) ); |
|
| 80 | + }, |
|
| 81 | + $results |
|
| 82 | + ); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @param $query |
|
| 87 | + * @param $schema_types |
|
| 88 | + * @param $limit |
|
| 89 | + * |
|
| 90 | + * @return Entity[] |
|
| 91 | + */ |
|
| 92 | + public function query( $query, $schema_types = array(), $limit = 10 ) { |
|
| 93 | + |
|
| 94 | + $results = $this->transform_posts( $this->query_posts( $query, $schema_types, $limit ) ); |
|
| 95 | + |
|
| 96 | + if ( count( $results ) >= $limit ) { |
|
| 97 | + return $results; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $results = array_merge( $results, $this->transform_terms( $this->query_terms( $query, $schema_types, $limit ) ) ); |
|
| 101 | + |
|
| 102 | + return $results; |
|
| 103 | + |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + public function get( $linked_entities ) { |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @var $term_type_service Type_Service |
|
| 110 | + */ |
|
| 111 | + $term_type_service = Type_Service::get_instance(); |
|
| 112 | + $post_entity_type_service = \Wordlift_Entity_Type_Service::get_instance(); |
|
| 113 | + |
|
| 114 | + return array_filter( |
|
| 115 | + array_map( |
|
| 116 | + function ( $item ) use ( $term_type_service, $post_entity_type_service ) { |
|
| 117 | + $parts = explode( '_', $item ); |
|
| 118 | + $type = Object_Type_Enum::from_string( $parts[0] ); |
|
| 119 | + $identifier = $parts[1]; |
|
| 120 | + |
|
| 121 | + if ( Object_Type_Enum::POST === $type ) { |
|
| 122 | + return new Entity( join( ',', $post_entity_type_service->get_names( $identifier ) ), new Wordpress_Content( get_post( $identifier ) ) ); |
|
| 123 | + } elseif ( Object_Type_Enum::TERM === $type ) { |
|
| 124 | + |
|
| 125 | + return new Entity( join( ',', $term_type_service->get_entity_types_labels( $identifier ) ), new Wordpress_Content( get_term( $identifier ) ) ); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + // return new Entity( $item->schema_type_name, new Wordpress_Content( get_term( $item->id ) ) ); |
|
| 129 | + }, |
|
| 130 | + $linked_entities |
|
| 131 | + ) |
|
| 132 | + ); |
|
| 133 | + |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | 136 | } |
@@ -23,24 +23,24 @@ discard block |
||
| 23 | 23 | * @return Entity_Query_Service |
| 24 | 24 | */ |
| 25 | 25 | public static function get_instance() { |
| 26 | - if ( ! isset( self::$instance ) ) { |
|
| 26 | + if ( ! isset(self::$instance)) { |
|
| 27 | 27 | self::$instance = new self(); |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | return self::$instance; |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - private function query_posts( $query, $schema_types, $limit ) { |
|
| 34 | - return wl_entity_get_by_title( $query, true, true, $limit, $schema_types ); |
|
| 33 | + private function query_posts($query, $schema_types, $limit) { |
|
| 34 | + return wl_entity_get_by_title($query, true, true, $limit, $schema_types); |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - private function query_terms( $query, $schema_types, $limit ) { |
|
| 37 | + private function query_terms($query, $schema_types, $limit) { |
|
| 38 | 38 | global $wpdb; |
| 39 | 39 | $schema_types = join( |
| 40 | 40 | ',', |
| 41 | 41 | array_map( |
| 42 | - function ( $schema_type ) { |
|
| 43 | - return "'" . esc_sql( strtolower( $schema_type ) ) . "'"; |
|
| 42 | + function($schema_type) { |
|
| 43 | + return "'".esc_sql(strtolower($schema_type))."'"; |
|
| 44 | 44 | }, |
| 45 | 45 | $schema_types |
| 46 | 46 | ) |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | $wpdb->prepare( |
| 51 | 51 | "SELECT DISTINCT t.term_id as id, t.name as title, tm.meta_value as schema_type_name FROM $wpdb->terms t INNER JOIN $wpdb->termmeta tm |
| 52 | 52 | ON t.term_id=tm.term_id WHERE t.name LIKE %s AND (tm.meta_key = %s AND tm.meta_value IN ($schema_types)) LIMIT %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 53 | - '%' . $wpdb->esc_like( $query ) . '%', |
|
| 53 | + '%'.$wpdb->esc_like($query).'%', |
|
| 54 | 54 | \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
| 55 | 55 | $limit |
| 56 | 56 | ) |
@@ -64,19 +64,19 @@ discard block |
||
| 64 | 64 | * |
| 65 | 65 | * @return Entity[] |
| 66 | 66 | */ |
| 67 | - private function transform_posts( $results ) { |
|
| 67 | + private function transform_posts($results) { |
|
| 68 | 68 | return array_map( |
| 69 | - function ( $item ) { |
|
| 70 | - return new Entity( $item->schema_type_name, new Wordpress_Content( get_post( $item->id ) ) ); |
|
| 69 | + function($item) { |
|
| 70 | + return new Entity($item->schema_type_name, new Wordpress_Content(get_post($item->id))); |
|
| 71 | 71 | }, |
| 72 | 72 | $results |
| 73 | 73 | ); |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | - private function transform_terms( $results ) { |
|
| 76 | + private function transform_terms($results) { |
|
| 77 | 77 | return array_map( |
| 78 | - function ( $item ) { |
|
| 79 | - return new Entity( $item->schema_type_name, new Wordpress_Content( get_term( $item->id ) ) ); |
|
| 78 | + function($item) { |
|
| 79 | + return new Entity($item->schema_type_name, new Wordpress_Content(get_term($item->id))); |
|
| 80 | 80 | }, |
| 81 | 81 | $results |
| 82 | 82 | ); |
@@ -89,21 +89,21 @@ discard block |
||
| 89 | 89 | * |
| 90 | 90 | * @return Entity[] |
| 91 | 91 | */ |
| 92 | - public function query( $query, $schema_types = array(), $limit = 10 ) { |
|
| 92 | + public function query($query, $schema_types = array(), $limit = 10) { |
|
| 93 | 93 | |
| 94 | - $results = $this->transform_posts( $this->query_posts( $query, $schema_types, $limit ) ); |
|
| 94 | + $results = $this->transform_posts($this->query_posts($query, $schema_types, $limit)); |
|
| 95 | 95 | |
| 96 | - if ( count( $results ) >= $limit ) { |
|
| 96 | + if (count($results) >= $limit) { |
|
| 97 | 97 | return $results; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - $results = array_merge( $results, $this->transform_terms( $this->query_terms( $query, $schema_types, $limit ) ) ); |
|
| 100 | + $results = array_merge($results, $this->transform_terms($this->query_terms($query, $schema_types, $limit))); |
|
| 101 | 101 | |
| 102 | 102 | return $results; |
| 103 | 103 | |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | - public function get( $linked_entities ) { |
|
| 106 | + public function get($linked_entities) { |
|
| 107 | 107 | |
| 108 | 108 | /** |
| 109 | 109 | * @var $term_type_service Type_Service |
@@ -113,16 +113,16 @@ discard block |
||
| 113 | 113 | |
| 114 | 114 | return array_filter( |
| 115 | 115 | array_map( |
| 116 | - function ( $item ) use ( $term_type_service, $post_entity_type_service ) { |
|
| 117 | - $parts = explode( '_', $item ); |
|
| 118 | - $type = Object_Type_Enum::from_string( $parts[0] ); |
|
| 116 | + function($item) use ($term_type_service, $post_entity_type_service) { |
|
| 117 | + $parts = explode('_', $item); |
|
| 118 | + $type = Object_Type_Enum::from_string($parts[0]); |
|
| 119 | 119 | $identifier = $parts[1]; |
| 120 | 120 | |
| 121 | - if ( Object_Type_Enum::POST === $type ) { |
|
| 122 | - return new Entity( join( ',', $post_entity_type_service->get_names( $identifier ) ), new Wordpress_Content( get_post( $identifier ) ) ); |
|
| 123 | - } elseif ( Object_Type_Enum::TERM === $type ) { |
|
| 121 | + if (Object_Type_Enum::POST === $type) { |
|
| 122 | + return new Entity(join(',', $post_entity_type_service->get_names($identifier)), new Wordpress_Content(get_post($identifier))); |
|
| 123 | + } elseif (Object_Type_Enum::TERM === $type) { |
|
| 124 | 124 | |
| 125 | - return new Entity( join( ',', $term_type_service->get_entity_types_labels( $identifier ) ), new Wordpress_Content( get_term( $identifier ) ) ); |
|
| 125 | + return new Entity(join(',', $term_type_service->get_entity_types_labels($identifier)), new Wordpress_Content(get_term($identifier))); |
|
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | // return new Entity( $item->schema_type_name, new Wordpress_Content( get_term( $item->id ) ) ); |
@@ -14,126 +14,126 @@ |
||
| 14 | 14 | |
| 15 | 15 | class Api_Data_Hooks { |
| 16 | 16 | |
| 17 | - const META_KEY = 'wl_schema_url'; |
|
| 18 | - |
|
| 19 | - protected $cache_requests = array(); |
|
| 20 | - |
|
| 21 | - public function __construct() { |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Hook for Post Save |
|
| 25 | - */ |
|
| 26 | - add_action( 'save_post', array( $this, 'post_save_request_delete_cache' ) ); |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Check for Meta Key change on Post Save |
|
| 30 | - */ |
|
| 31 | - add_action( 'updated_post_meta', array( $this, 'post_meta_request_delete_cache' ), 10, 4 ); |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Initiate the purge cache requests |
|
| 35 | - */ |
|
| 36 | - add_action( 'shutdown', array( $this, 'init_purge_cache_requests' ) ); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - public function post_save_request_delete_cache( $post_id ) { |
|
| 40 | - $this->delete_cache_for_meta_values( $post_id ); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 44 | - public function post_meta_request_delete_cache( $meta_id, $post_id, $meta_key, $_meta_value ) { |
|
| 45 | - $this->cache_requests[] = $post_id; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - public function init_purge_cache_requests() { |
|
| 49 | - // Bail early. |
|
| 50 | - if ( empty( $this->cache_requests ) ) { |
|
| 51 | - return; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - // Avoid duplicate cache requests. |
|
| 55 | - $unique_requests = array_unique( $this->cache_requests ); |
|
| 56 | - |
|
| 57 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 58 | - foreach ( $unique_requests as $key => $post_id ) { |
|
| 59 | - $this->delete_cache_for_meta_values( $post_id ); |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @param integer $post_id |
|
| 65 | - */ |
|
| 66 | - private function delete_cache_for_meta_values( $post_id ) { |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Get Post Meta Values |
|
| 70 | - */ |
|
| 71 | - $values = get_post_meta( $post_id, self::META_KEY, false ); |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Iterate over $values array |
|
| 75 | - */ |
|
| 76 | - if ( ! empty( $values ) ) { |
|
| 77 | - foreach ( $values as $link ) { |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Skip the <permalink> |
|
| 81 | - */ |
|
| 82 | - if ( '<permalink>' === $link || 'http://<permalink>/' === $link ) { |
|
| 83 | - $link = get_permalink(); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Sanitize the link |
|
| 88 | - */ |
|
| 89 | - $link = $this->sanitize( $link ); |
|
| 90 | - /** |
|
| 91 | - * Make actual API DELETE cache request |
|
| 92 | - */ |
|
| 93 | - $this->api_call_delete_cache( $link ); |
|
| 94 | - } |
|
| 95 | - } |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @desc Sanitize the $link |
|
| 100 | - * |
|
| 101 | - * @param string $link |
|
| 102 | - * |
|
| 103 | - * @return string |
|
| 104 | - */ |
|
| 105 | - private function sanitize( $link ) { |
|
| 106 | - return preg_replace( '/:\//i', '', $link ); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @desc Do a DELETE request with WP request function |
|
| 111 | - * |
|
| 112 | - * @param string $path path that goes after the URL eg. "/user/login" |
|
| 113 | - * |
|
| 114 | - * @return bool True if successful otherwise false |
|
| 115 | - */ |
|
| 116 | - private function api_call_delete_cache( $path ) { |
|
| 117 | - |
|
| 118 | - // Bailout if path is empty |
|
| 119 | - if ( empty( $path ) ) { |
|
| 120 | - return false; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - $log = \Wordlift_Log_Service::get_logger( 'api_call_delete_cache' ); |
|
| 124 | - |
|
| 125 | - $log->debug( "Delete cache request started:: '$path'" ); |
|
| 126 | - |
|
| 127 | - $url = Wordlift_Configuration_Service::get_instance()->get_api_url() . 'data/' . $path; |
|
| 128 | - $args = array( |
|
| 129 | - 'method' => 'DELETE', |
|
| 130 | - 'blocking' => false, |
|
| 131 | - 'timeout' => 0.01, |
|
| 132 | - ); |
|
| 133 | - |
|
| 134 | - // Execute the request |
|
| 135 | - wp_remote_request( $url, $args ); |
|
| 136 | - |
|
| 137 | - } |
|
| 17 | + const META_KEY = 'wl_schema_url'; |
|
| 18 | + |
|
| 19 | + protected $cache_requests = array(); |
|
| 20 | + |
|
| 21 | + public function __construct() { |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Hook for Post Save |
|
| 25 | + */ |
|
| 26 | + add_action( 'save_post', array( $this, 'post_save_request_delete_cache' ) ); |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Check for Meta Key change on Post Save |
|
| 30 | + */ |
|
| 31 | + add_action( 'updated_post_meta', array( $this, 'post_meta_request_delete_cache' ), 10, 4 ); |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Initiate the purge cache requests |
|
| 35 | + */ |
|
| 36 | + add_action( 'shutdown', array( $this, 'init_purge_cache_requests' ) ); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + public function post_save_request_delete_cache( $post_id ) { |
|
| 40 | + $this->delete_cache_for_meta_values( $post_id ); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 44 | + public function post_meta_request_delete_cache( $meta_id, $post_id, $meta_key, $_meta_value ) { |
|
| 45 | + $this->cache_requests[] = $post_id; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + public function init_purge_cache_requests() { |
|
| 49 | + // Bail early. |
|
| 50 | + if ( empty( $this->cache_requests ) ) { |
|
| 51 | + return; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + // Avoid duplicate cache requests. |
|
| 55 | + $unique_requests = array_unique( $this->cache_requests ); |
|
| 56 | + |
|
| 57 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 58 | + foreach ( $unique_requests as $key => $post_id ) { |
|
| 59 | + $this->delete_cache_for_meta_values( $post_id ); |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @param integer $post_id |
|
| 65 | + */ |
|
| 66 | + private function delete_cache_for_meta_values( $post_id ) { |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Get Post Meta Values |
|
| 70 | + */ |
|
| 71 | + $values = get_post_meta( $post_id, self::META_KEY, false ); |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Iterate over $values array |
|
| 75 | + */ |
|
| 76 | + if ( ! empty( $values ) ) { |
|
| 77 | + foreach ( $values as $link ) { |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Skip the <permalink> |
|
| 81 | + */ |
|
| 82 | + if ( '<permalink>' === $link || 'http://<permalink>/' === $link ) { |
|
| 83 | + $link = get_permalink(); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Sanitize the link |
|
| 88 | + */ |
|
| 89 | + $link = $this->sanitize( $link ); |
|
| 90 | + /** |
|
| 91 | + * Make actual API DELETE cache request |
|
| 92 | + */ |
|
| 93 | + $this->api_call_delete_cache( $link ); |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @desc Sanitize the $link |
|
| 100 | + * |
|
| 101 | + * @param string $link |
|
| 102 | + * |
|
| 103 | + * @return string |
|
| 104 | + */ |
|
| 105 | + private function sanitize( $link ) { |
|
| 106 | + return preg_replace( '/:\//i', '', $link ); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @desc Do a DELETE request with WP request function |
|
| 111 | + * |
|
| 112 | + * @param string $path path that goes after the URL eg. "/user/login" |
|
| 113 | + * |
|
| 114 | + * @return bool True if successful otherwise false |
|
| 115 | + */ |
|
| 116 | + private function api_call_delete_cache( $path ) { |
|
| 117 | + |
|
| 118 | + // Bailout if path is empty |
|
| 119 | + if ( empty( $path ) ) { |
|
| 120 | + return false; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + $log = \Wordlift_Log_Service::get_logger( 'api_call_delete_cache' ); |
|
| 124 | + |
|
| 125 | + $log->debug( "Delete cache request started:: '$path'" ); |
|
| 126 | + |
|
| 127 | + $url = Wordlift_Configuration_Service::get_instance()->get_api_url() . 'data/' . $path; |
|
| 128 | + $args = array( |
|
| 129 | + 'method' => 'DELETE', |
|
| 130 | + 'blocking' => false, |
|
| 131 | + 'timeout' => 0.01, |
|
| 132 | + ); |
|
| 133 | + |
|
| 134 | + // Execute the request |
|
| 135 | + wp_remote_request( $url, $args ); |
|
| 136 | + |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | 139 | } |
@@ -23,74 +23,74 @@ discard block |
||
| 23 | 23 | /** |
| 24 | 24 | * Hook for Post Save |
| 25 | 25 | */ |
| 26 | - add_action( 'save_post', array( $this, 'post_save_request_delete_cache' ) ); |
|
| 26 | + add_action('save_post', array($this, 'post_save_request_delete_cache')); |
|
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * Check for Meta Key change on Post Save |
| 30 | 30 | */ |
| 31 | - add_action( 'updated_post_meta', array( $this, 'post_meta_request_delete_cache' ), 10, 4 ); |
|
| 31 | + add_action('updated_post_meta', array($this, 'post_meta_request_delete_cache'), 10, 4); |
|
| 32 | 32 | |
| 33 | 33 | /** |
| 34 | 34 | * Initiate the purge cache requests |
| 35 | 35 | */ |
| 36 | - add_action( 'shutdown', array( $this, 'init_purge_cache_requests' ) ); |
|
| 36 | + add_action('shutdown', array($this, 'init_purge_cache_requests')); |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - public function post_save_request_delete_cache( $post_id ) { |
|
| 40 | - $this->delete_cache_for_meta_values( $post_id ); |
|
| 39 | + public function post_save_request_delete_cache($post_id) { |
|
| 40 | + $this->delete_cache_for_meta_values($post_id); |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 44 | - public function post_meta_request_delete_cache( $meta_id, $post_id, $meta_key, $_meta_value ) { |
|
| 44 | + public function post_meta_request_delete_cache($meta_id, $post_id, $meta_key, $_meta_value) { |
|
| 45 | 45 | $this->cache_requests[] = $post_id; |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | public function init_purge_cache_requests() { |
| 49 | 49 | // Bail early. |
| 50 | - if ( empty( $this->cache_requests ) ) { |
|
| 50 | + if (empty($this->cache_requests)) { |
|
| 51 | 51 | return; |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | // Avoid duplicate cache requests. |
| 55 | - $unique_requests = array_unique( $this->cache_requests ); |
|
| 55 | + $unique_requests = array_unique($this->cache_requests); |
|
| 56 | 56 | |
| 57 | 57 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 58 | - foreach ( $unique_requests as $key => $post_id ) { |
|
| 59 | - $this->delete_cache_for_meta_values( $post_id ); |
|
| 58 | + foreach ($unique_requests as $key => $post_id) { |
|
| 59 | + $this->delete_cache_for_meta_values($post_id); |
|
| 60 | 60 | } |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | /** |
| 64 | 64 | * @param integer $post_id |
| 65 | 65 | */ |
| 66 | - private function delete_cache_for_meta_values( $post_id ) { |
|
| 66 | + private function delete_cache_for_meta_values($post_id) { |
|
| 67 | 67 | |
| 68 | 68 | /** |
| 69 | 69 | * Get Post Meta Values |
| 70 | 70 | */ |
| 71 | - $values = get_post_meta( $post_id, self::META_KEY, false ); |
|
| 71 | + $values = get_post_meta($post_id, self::META_KEY, false); |
|
| 72 | 72 | |
| 73 | 73 | /** |
| 74 | 74 | * Iterate over $values array |
| 75 | 75 | */ |
| 76 | - if ( ! empty( $values ) ) { |
|
| 77 | - foreach ( $values as $link ) { |
|
| 76 | + if ( ! empty($values)) { |
|
| 77 | + foreach ($values as $link) { |
|
| 78 | 78 | |
| 79 | 79 | /** |
| 80 | 80 | * Skip the <permalink> |
| 81 | 81 | */ |
| 82 | - if ( '<permalink>' === $link || 'http://<permalink>/' === $link ) { |
|
| 82 | + if ('<permalink>' === $link || 'http://<permalink>/' === $link) { |
|
| 83 | 83 | $link = get_permalink(); |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | /** |
| 87 | 87 | * Sanitize the link |
| 88 | 88 | */ |
| 89 | - $link = $this->sanitize( $link ); |
|
| 89 | + $link = $this->sanitize($link); |
|
| 90 | 90 | /** |
| 91 | 91 | * Make actual API DELETE cache request |
| 92 | 92 | */ |
| 93 | - $this->api_call_delete_cache( $link ); |
|
| 93 | + $this->api_call_delete_cache($link); |
|
| 94 | 94 | } |
| 95 | 95 | } |
| 96 | 96 | } |
@@ -102,8 +102,8 @@ discard block |
||
| 102 | 102 | * |
| 103 | 103 | * @return string |
| 104 | 104 | */ |
| 105 | - private function sanitize( $link ) { |
|
| 106 | - return preg_replace( '/:\//i', '', $link ); |
|
| 105 | + private function sanitize($link) { |
|
| 106 | + return preg_replace('/:\//i', '', $link); |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | /** |
@@ -113,18 +113,18 @@ discard block |
||
| 113 | 113 | * |
| 114 | 114 | * @return bool True if successful otherwise false |
| 115 | 115 | */ |
| 116 | - private function api_call_delete_cache( $path ) { |
|
| 116 | + private function api_call_delete_cache($path) { |
|
| 117 | 117 | |
| 118 | 118 | // Bailout if path is empty |
| 119 | - if ( empty( $path ) ) { |
|
| 119 | + if (empty($path)) { |
|
| 120 | 120 | return false; |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | - $log = \Wordlift_Log_Service::get_logger( 'api_call_delete_cache' ); |
|
| 123 | + $log = \Wordlift_Log_Service::get_logger('api_call_delete_cache'); |
|
| 124 | 124 | |
| 125 | - $log->debug( "Delete cache request started:: '$path'" ); |
|
| 125 | + $log->debug("Delete cache request started:: '$path'"); |
|
| 126 | 126 | |
| 127 | - $url = Wordlift_Configuration_Service::get_instance()->get_api_url() . 'data/' . $path; |
|
| 127 | + $url = Wordlift_Configuration_Service::get_instance()->get_api_url().'data/'.$path; |
|
| 128 | 128 | $args = array( |
| 129 | 129 | 'method' => 'DELETE', |
| 130 | 130 | 'blocking' => false, |
@@ -132,7 +132,7 @@ discard block |
||
| 132 | 132 | ); |
| 133 | 133 | |
| 134 | 134 | // Execute the request |
| 135 | - wp_remote_request( $url, $args ); |
|
| 135 | + wp_remote_request($url, $args); |
|
| 136 | 136 | |
| 137 | 137 | } |
| 138 | 138 | |
@@ -16,37 +16,37 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Url_Property_Service extends Wordlift_Simple_Property_Service { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The meta key for the schema:url property. |
|
| 21 | - */ |
|
| 22 | - const META_KEY = 'wl_schema_url'; |
|
| 19 | + /** |
|
| 20 | + * The meta key for the schema:url property. |
|
| 21 | + */ |
|
| 22 | + const META_KEY = 'wl_schema_url'; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Get the URLs associated with the post. |
|
| 26 | - * |
|
| 27 | - * @param int $id The post id. |
|
| 28 | - * @param string $meta_key The meta key. |
|
| 29 | - * |
|
| 30 | - * @return array An array of URLs. |
|
| 31 | - */ |
|
| 32 | - public function get( $id, $meta_key, $type ) { |
|
| 24 | + /** |
|
| 25 | + * Get the URLs associated with the post. |
|
| 26 | + * |
|
| 27 | + * @param int $id The post id. |
|
| 28 | + * @param string $meta_key The meta key. |
|
| 29 | + * |
|
| 30 | + * @return array An array of URLs. |
|
| 31 | + */ |
|
| 32 | + public function get( $id, $meta_key, $type ) { |
|
| 33 | 33 | |
| 34 | - // Get the meta values and push the <permalink> to |
|
| 35 | - // ensure that default url will be added to the schema:url's. |
|
| 36 | - /* |
|
| 34 | + // Get the meta values and push the <permalink> to |
|
| 35 | + // ensure that default url will be added to the schema:url's. |
|
| 36 | + /* |
|
| 37 | 37 | * Do not add `<permalink>` if one or more URLs have been provided by the editor. |
| 38 | 38 | * |
| 39 | 39 | * @see https://github.com/insideout10/wordlift-plugin/issues/913 |
| 40 | 40 | * |
| 41 | 41 | * @since 3.21.1 |
| 42 | 42 | */ |
| 43 | - $urls = parent::get( $id, $meta_key, $type ); |
|
| 44 | - $urls = array_filter( $urls ? $urls : array( '<permalink>' ) ); |
|
| 43 | + $urls = parent::get( $id, $meta_key, $type ); |
|
| 44 | + $urls = array_filter( $urls ? $urls : array( '<permalink>' ) ); |
|
| 45 | 45 | |
| 46 | - // Convert <permalink> in actual permalink values. |
|
| 47 | - return array_map( |
|
| 48 | - function ( $item ) use ( $id, $type ) { |
|
| 49 | - /* |
|
| 46 | + // Convert <permalink> in actual permalink values. |
|
| 47 | + return array_map( |
|
| 48 | + function ( $item ) use ( $id, $type ) { |
|
| 49 | + /* |
|
| 50 | 50 | * If `<permalink>` get the production permalink. |
| 51 | 51 | * |
| 52 | 52 | * @since 3.20.0 |
@@ -54,22 +54,22 @@ discard block |
||
| 54 | 54 | * @see https://github.com/insideout10/wordlift-plugin/issues/850. |
| 55 | 55 | */ |
| 56 | 56 | |
| 57 | - if ( '<permalink>' !== $item && 'http://<permalink>/' !== $item ) { |
|
| 58 | - return $item; |
|
| 59 | - } |
|
| 57 | + if ( '<permalink>' !== $item && 'http://<permalink>/' !== $item ) { |
|
| 58 | + return $item; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - // Permalinks. |
|
| 62 | - switch ( $type ) { |
|
| 63 | - case Object_Type_Enum::POST: |
|
| 64 | - return Wordlift_Post_Adapter::get_production_permalink( $id ); |
|
| 65 | - case Object_Type_Enum::TERM: |
|
| 66 | - return get_term_link( $id ); |
|
| 67 | - default: |
|
| 68 | - return $item; |
|
| 69 | - } |
|
| 70 | - }, |
|
| 71 | - array_unique( $urls ) |
|
| 72 | - ); |
|
| 73 | - } |
|
| 61 | + // Permalinks. |
|
| 62 | + switch ( $type ) { |
|
| 63 | + case Object_Type_Enum::POST: |
|
| 64 | + return Wordlift_Post_Adapter::get_production_permalink( $id ); |
|
| 65 | + case Object_Type_Enum::TERM: |
|
| 66 | + return get_term_link( $id ); |
|
| 67 | + default: |
|
| 68 | + return $item; |
|
| 69 | + } |
|
| 70 | + }, |
|
| 71 | + array_unique( $urls ) |
|
| 72 | + ); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | 75 | } |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | * |
| 30 | 30 | * @return array An array of URLs. |
| 31 | 31 | */ |
| 32 | - public function get( $id, $meta_key, $type ) { |
|
| 32 | + public function get($id, $meta_key, $type) { |
|
| 33 | 33 | |
| 34 | 34 | // Get the meta values and push the <permalink> to |
| 35 | 35 | // ensure that default url will be added to the schema:url's. |
@@ -40,12 +40,12 @@ discard block |
||
| 40 | 40 | * |
| 41 | 41 | * @since 3.21.1 |
| 42 | 42 | */ |
| 43 | - $urls = parent::get( $id, $meta_key, $type ); |
|
| 44 | - $urls = array_filter( $urls ? $urls : array( '<permalink>' ) ); |
|
| 43 | + $urls = parent::get($id, $meta_key, $type); |
|
| 44 | + $urls = array_filter($urls ? $urls : array('<permalink>')); |
|
| 45 | 45 | |
| 46 | 46 | // Convert <permalink> in actual permalink values. |
| 47 | 47 | return array_map( |
| 48 | - function ( $item ) use ( $id, $type ) { |
|
| 48 | + function($item) use ($id, $type) { |
|
| 49 | 49 | /* |
| 50 | 50 | * If `<permalink>` get the production permalink. |
| 51 | 51 | * |
@@ -54,21 +54,21 @@ discard block |
||
| 54 | 54 | * @see https://github.com/insideout10/wordlift-plugin/issues/850. |
| 55 | 55 | */ |
| 56 | 56 | |
| 57 | - if ( '<permalink>' !== $item && 'http://<permalink>/' !== $item ) { |
|
| 57 | + if ('<permalink>' !== $item && 'http://<permalink>/' !== $item) { |
|
| 58 | 58 | return $item; |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | // Permalinks. |
| 62 | - switch ( $type ) { |
|
| 62 | + switch ($type) { |
|
| 63 | 63 | case Object_Type_Enum::POST: |
| 64 | - return Wordlift_Post_Adapter::get_production_permalink( $id ); |
|
| 64 | + return Wordlift_Post_Adapter::get_production_permalink($id); |
|
| 65 | 65 | case Object_Type_Enum::TERM: |
| 66 | - return get_term_link( $id ); |
|
| 66 | + return get_term_link($id); |
|
| 67 | 67 | default: |
| 68 | 68 | return $item; |
| 69 | 69 | } |
| 70 | 70 | }, |
| 71 | - array_unique( $urls ) |
|
| 71 | + array_unique($urls) |
|
| 72 | 72 | ); |
| 73 | 73 | } |
| 74 | 74 | |