Completed
Push — develop ( a280ae...c0cfb6 )
by David
01:50 queued 18s
created
src/modules/include-exclude/includes/Plugin_Enabled.php 1 patch
Indentation   +89 added lines, -89 removed lines patch added patch discarded remove patch
@@ -16,94 +16,94 @@
 block discarded – undo
16 16
  */
17 17
 class Plugin_Enabled {
18 18
 
19
-	/**
20
-	 * @var Configuration $configuration
21
-	 */
22
-	private $configuration;
23
-
24
-	public function __construct( $configuration ) {
25
-		$this->configuration = $configuration;
26
-	}
27
-
28
-	/**
29
-	 * Register hooks.
30
-	 */
31
-	public function register_hooks() {
32
-		add_filter( 'wl_is_enabled', array( $this, 'wl_is_enabled' ) );
33
-	}
34
-
35
-	/**
36
-	 * Enable/Disable WordLift Plugin.
37
-	 *
38
-	 * @param $enabled
39
-	 *
40
-	 * @return bool|mixed
41
-	 */
42
-	public function wl_is_enabled( $enabled ) {
43
-
44
-		// Always enable wordlift on admin and rest api pages.
45
-		if ( is_admin() || $this->is_rest_request() ) {
46
-			return $enabled;
47
-		}
48
-
49
-		$path    = strtok( (string) $_SERVER['REQUEST_URI'], '?' ); // phpcs:ignore
50
-		$options = get_option( 'wl_exclude_include_urls_settings' );
51
-
52
-		// Bail out if URLs are not set.
53
-		if ( empty( $options['urls'] ) ) {
54
-			return $enabled;
55
-		}
56
-
57
-		$current_url = trailingslashit( home_url( $path ) );
58
-
59
-		return $this->are_urls_included( $current_url );
60
-	}
61
-
62
-	public function are_urls_included( $urls ) {
63
-		// Ensure we deal with an array. We `trailingslashit` all URLs to avoid issues with missing slashes.
64
-		$urls = array_map( 'trailingslashit', (array) $urls );
65
-
66
-		// Set a default state.
67
-		$include_by_default = ( $this->configuration->get_default() === 'include' );
68
-
69
-		// Get URLs into an array from settings, trim them and make absolute if needed.
70
-		$configured_urls = array_map(
71
-			function ( $url ) {
72
-				$url = trim( $url );
73
-				if ( substr( $url, 0, 4 ) !== 'http' ) {
74
-					return trailingslashit( home_url( $url ) );
75
-				}
76
-
77
-				// Add a trailing slash and return the url
78
-				return trailingslashit( $url );
79
-			},
80
-			explode( PHP_EOL, $this->configuration->get_urls() )
81
-		);
82
-
83
-		// Check if any of the provided URLs is in the configured URLs.
84
-		$intersection = array_intersect( $urls, $configured_urls );
85
-		if ( ! empty( $intersection ) ) {
86
-			return ! $include_by_default;
87
-		}
88
-
89
-		return $include_by_default;
90
-	}
91
-
92
-	/**
93
-	 * We cant rely on WP_REST_REQUEST constant here since it is loaded after init hook
94
-	 *
95
-	 * @return bool
96
-	 */
97
-	protected function is_rest_request() {
98
-		if ( empty( $_SERVER['REQUEST_URI'] ) ) {
99
-			// Probably a CLI request.
100
-			return false;
101
-		}
102
-
103
-		$rest_prefix = trailingslashit( rest_get_url_prefix() );
104
-		$path        = strtok( (string) $_SERVER['REQUEST_URI'], '?' ); // phpcs:ignore
105
-
106
-		return strpos( $path, $rest_prefix ) !== false;
107
-	}
19
+    /**
20
+     * @var Configuration $configuration
21
+     */
22
+    private $configuration;
23
+
24
+    public function __construct( $configuration ) {
25
+        $this->configuration = $configuration;
26
+    }
27
+
28
+    /**
29
+     * Register hooks.
30
+     */
31
+    public function register_hooks() {
32
+        add_filter( 'wl_is_enabled', array( $this, 'wl_is_enabled' ) );
33
+    }
34
+
35
+    /**
36
+     * Enable/Disable WordLift Plugin.
37
+     *
38
+     * @param $enabled
39
+     *
40
+     * @return bool|mixed
41
+     */
42
+    public function wl_is_enabled( $enabled ) {
43
+
44
+        // Always enable wordlift on admin and rest api pages.
45
+        if ( is_admin() || $this->is_rest_request() ) {
46
+            return $enabled;
47
+        }
48
+
49
+        $path    = strtok( (string) $_SERVER['REQUEST_URI'], '?' ); // phpcs:ignore
50
+        $options = get_option( 'wl_exclude_include_urls_settings' );
51
+
52
+        // Bail out if URLs are not set.
53
+        if ( empty( $options['urls'] ) ) {
54
+            return $enabled;
55
+        }
56
+
57
+        $current_url = trailingslashit( home_url( $path ) );
58
+
59
+        return $this->are_urls_included( $current_url );
60
+    }
61
+
62
+    public function are_urls_included( $urls ) {
63
+        // Ensure we deal with an array. We `trailingslashit` all URLs to avoid issues with missing slashes.
64
+        $urls = array_map( 'trailingslashit', (array) $urls );
65
+
66
+        // Set a default state.
67
+        $include_by_default = ( $this->configuration->get_default() === 'include' );
68
+
69
+        // Get URLs into an array from settings, trim them and make absolute if needed.
70
+        $configured_urls = array_map(
71
+            function ( $url ) {
72
+                $url = trim( $url );
73
+                if ( substr( $url, 0, 4 ) !== 'http' ) {
74
+                    return trailingslashit( home_url( $url ) );
75
+                }
76
+
77
+                // Add a trailing slash and return the url
78
+                return trailingslashit( $url );
79
+            },
80
+            explode( PHP_EOL, $this->configuration->get_urls() )
81
+        );
82
+
83
+        // Check if any of the provided URLs is in the configured URLs.
84
+        $intersection = array_intersect( $urls, $configured_urls );
85
+        if ( ! empty( $intersection ) ) {
86
+            return ! $include_by_default;
87
+        }
88
+
89
+        return $include_by_default;
90
+    }
91
+
92
+    /**
93
+     * We cant rely on WP_REST_REQUEST constant here since it is loaded after init hook
94
+     *
95
+     * @return bool
96
+     */
97
+    protected function is_rest_request() {
98
+        if ( empty( $_SERVER['REQUEST_URI'] ) ) {
99
+            // Probably a CLI request.
100
+            return false;
101
+        }
102
+
103
+        $rest_prefix = trailingslashit( rest_get_url_prefix() );
104
+        $path        = strtok( (string) $_SERVER['REQUEST_URI'], '?' ); // phpcs:ignore
105
+
106
+        return strpos( $path, $rest_prefix ) !== false;
107
+    }
108 108
 
109 109
 }
Please login to merge, or discard this patch.
src/modules/include-exclude/includes/Configuration.php 1 patch
Indentation   +45 added lines, -45 removed lines patch added patch discarded remove patch
@@ -4,50 +4,50 @@
 block discarded – undo
4 4
 
5 5
 class Configuration {
6 6
 
7
-	private static $instance;
8
-
9
-	private $type;
10
-	private $urls;
11
-
12
-	protected function __construct() {
13
-		$include_exclude_data = get_option( 'wl_exclude_include_urls_settings', array() );
14
-		$include_exclude      = isset( $include_exclude_data['include_exclude'] ) ? (array) $include_exclude_data['include_exclude'] : array();
15
-
16
-		$this->type = in_array(
17
-			$include_exclude,
18
-			array(
19
-				'include',
20
-				'exclude',
21
-			),
22
-			true
23
-		)
24
-			? $include_exclude : 'exclude';
25
-		$this->urls = $include_exclude_data['urls'];
26
-	}
27
-
28
-	public static function get_instance() {
29
-		if ( ! isset( self::$instance ) ) {
30
-			self::$instance = new self();
31
-		}
32
-
33
-		return self::$instance;
34
-	}
35
-
36
-	public function get_type() {
37
-		return $this->type;
38
-	}
39
-
40
-	/**
41
-	 * Gets the default action, which is the inverse of the configuration type.
42
-	 *
43
-	 * @return string
44
-	 */
45
-	public function get_default() {
46
-		return ( $this->type === 'exclude' ? 'include' : 'exclude' );
47
-	}
48
-
49
-	public function get_urls() {
50
-		return $this->urls;
51
-	}
7
+    private static $instance;
8
+
9
+    private $type;
10
+    private $urls;
11
+
12
+    protected function __construct() {
13
+        $include_exclude_data = get_option( 'wl_exclude_include_urls_settings', array() );
14
+        $include_exclude      = isset( $include_exclude_data['include_exclude'] ) ? (array) $include_exclude_data['include_exclude'] : array();
15
+
16
+        $this->type = in_array(
17
+            $include_exclude,
18
+            array(
19
+                'include',
20
+                'exclude',
21
+            ),
22
+            true
23
+        )
24
+            ? $include_exclude : 'exclude';
25
+        $this->urls = $include_exclude_data['urls'];
26
+    }
27
+
28
+    public static function get_instance() {
29
+        if ( ! isset( self::$instance ) ) {
30
+            self::$instance = new self();
31
+        }
32
+
33
+        return self::$instance;
34
+    }
35
+
36
+    public function get_type() {
37
+        return $this->type;
38
+    }
39
+
40
+    /**
41
+     * Gets the default action, which is the inverse of the configuration type.
42
+     *
43
+     * @return string
44
+     */
45
+    public function get_default() {
46
+        return ( $this->type === 'exclude' ? 'include' : 'exclude' );
47
+    }
48
+
49
+    public function get_urls() {
50
+        return $this->urls;
51
+    }
52 52
 
53 53
 }
Please login to merge, or discard this patch.
src/modules/include-exclude/includes/Jsonld_Interceptor.php 1 patch
Indentation   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -4,28 +4,28 @@
 block discarded – undo
4 4
 
5 5
 class Jsonld_Interceptor {
6 6
 
7
-	/** @var Plugin_Enabled $plugin_enabled */
8
-	private $plugin_enabled;
7
+    /** @var Plugin_Enabled $plugin_enabled */
8
+    private $plugin_enabled;
9 9
 
10
-	public function __construct( $plugin_enabled ) {
11
-		$this->plugin_enabled = $plugin_enabled;
12
-	}
10
+    public function __construct( $plugin_enabled ) {
11
+        $this->plugin_enabled = $plugin_enabled;
12
+    }
13 13
 
14
-	public function register_hooks() {
15
-		add_filter( 'wl_after_get_jsonld', array( $this, 'after_get_jsonld' ) );
16
-	}
14
+    public function register_hooks() {
15
+        add_filter( 'wl_after_get_jsonld', array( $this, 'after_get_jsonld' ) );
16
+    }
17 17
 
18
-	public function after_get_jsonld( $jsonld_arr ) {
19
-		if ( ! is_array( $jsonld_arr ) || empty( $jsonld_arr ) || ! isset( $jsonld_arr[0]['url'] ) ) {
20
-			return $jsonld_arr;
21
-		}
18
+    public function after_get_jsonld( $jsonld_arr ) {
19
+        if ( ! is_array( $jsonld_arr ) || empty( $jsonld_arr ) || ! isset( $jsonld_arr[0]['url'] ) ) {
20
+            return $jsonld_arr;
21
+        }
22 22
 
23
-		// If the URLs are included then publish them.
24
-		if ( $this->plugin_enabled->are_urls_included( $jsonld_arr[0]['url'] ) ) {
25
-			return $jsonld_arr;
26
-		}
23
+        // If the URLs are included then publish them.
24
+        if ( $this->plugin_enabled->are_urls_included( $jsonld_arr[0]['url'] ) ) {
25
+            return $jsonld_arr;
26
+        }
27 27
 
28
-		return array();
29
-	}
28
+        return array();
29
+    }
30 30
 
31 31
 }
Please login to merge, or discard this patch.
src/modules/include-exclude/load.php 1 patch
Indentation   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -13,7 +13,7 @@  discard block
 block discarded – undo
13 13
 use Wordlift\Modules\Common\Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
14 14
 
15 15
 if ( ! defined( 'ABSPATH' ) ) {
16
-	exit;
16
+    exit;
17 17
 }
18 18
 
19 19
 /**
@@ -22,7 +22,7 @@  discard block
 block discarded – undo
22 22
  */
23 23
 $wl_features = get_option( '_wl_features', array() );
24 24
 if ( ! apply_filters( 'wl_feature__enable__include-exclude', isset( $wl_features['include-exclude'] ) && $wl_features['include-exclude'] ) ) { // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
25
-	return;
25
+    return;
26 26
 }
27 27
 
28 28
 /**
@@ -32,29 +32,29 @@  discard block
 block discarded – undo
32 32
  */
33 33
 function __wl_include_exclude__load() {
34 34
 
35
-	// Autoloader for plugin itself.
36
-	if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
37
-		require_once __DIR__ . '/vendor/autoload.php';
38
-	}
35
+    // Autoloader for plugin itself.
36
+    if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
37
+        require_once __DIR__ . '/vendor/autoload.php';
38
+    }
39 39
 
40
-	$container_builder = new ContainerBuilder();
41
-	$loader            = new YamlFileLoader( $container_builder, new FileLocator( __DIR__ ) );
42
-	$loader->load( 'services.yml' );
43
-	$container_builder->compile();
40
+    $container_builder = new ContainerBuilder();
41
+    $loader            = new YamlFileLoader( $container_builder, new FileLocator( __DIR__ ) );
42
+    $loader->load( 'services.yml' );
43
+    $container_builder->compile();
44 44
 
45
-	$enabled = $container_builder->get( 'Wordlift\Modules\Include_Exclude\Plugin_Enabled' );
46
-	$enabled->register_hooks();
45
+    $enabled = $container_builder->get( 'Wordlift\Modules\Include_Exclude\Plugin_Enabled' );
46
+    $enabled->register_hooks();
47 47
 
48
-	if ( apply_filters( 'wl_is_enabled', true ) ) {
49
-		$settings = $container_builder->get( 'Wordlift\Modules\Include_Exclude\Admin\Settings' );
50
-		$settings->register_hooks();
48
+    if ( apply_filters( 'wl_is_enabled', true ) ) {
49
+        $settings = $container_builder->get( 'Wordlift\Modules\Include_Exclude\Admin\Settings' );
50
+        $settings->register_hooks();
51 51
 
52
-		$api = $container_builder->get( 'Wordlift\Modules\Include_Exclude\API' );
53
-		$api->register_hooks();
52
+        $api = $container_builder->get( 'Wordlift\Modules\Include_Exclude\API' );
53
+        $api->register_hooks();
54 54
 
55
-		$jsonld_interceptor = $container_builder->get( 'Wordlift\Modules\Include_Exclude\Jsonld_Interceptor' );
56
-		$jsonld_interceptor->register_hooks();
57
-	}
55
+        $jsonld_interceptor = $container_builder->get( 'Wordlift\Modules\Include_Exclude\Jsonld_Interceptor' );
56
+        $jsonld_interceptor->register_hooks();
57
+    }
58 58
 
59 59
 }
60 60
 
Please login to merge, or discard this patch.
src/includes/class-wordlift-post-to-jsonld-converter.php 1 patch
Indentation   +435 added lines, -435 removed lines patch added patch discarded remove patch
@@ -18,465 +18,465 @@
 block discarded – undo
18 18
  */
19 19
 class Wordlift_Post_To_Jsonld_Converter extends Wordlift_Abstract_Post_To_Jsonld_Converter {
20 20
 
21
-	/**
22
-	 * @var Wordlift_Post_To_Jsonld_Converter
23
-	 */
24
-	private static $instance;
25
-
26
-	/**
27
-	 * A {@link Wordlift_Log_Service} instance.
28
-	 *
29
-	 * @since  3.10.0
30
-	 * @access private
31
-	 * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
32
-	 */
33
-	private $log;
34
-
35
-	/**
36
-	 * @var false
37
-	 */
38
-	private $disable_convert_filters;
39
-
40
-	/**
41
-	 * Wordlift_Post_To_Jsonld_Converter constructor.
42
-	 *
43
-	 * @param Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
44
-	 * @param Wordlift_User_Service        $user_service A {@link Wordlift_User_Service} instance.
45
-	 * @param Wordlift_Attachment_Service  $attachment_service A {@link Wordlift_Attachment_Service} instance.
46
-	 *
47
-	 * @since 3.10.0
48
-	 */
49
-	public function __construct( $entity_type_service, $user_service, $attachment_service, $disable_convert_filters = false ) {
50
-		parent::__construct( $entity_type_service, $user_service, $attachment_service, Wordlift_Property_Getter_Factory::create() );
51
-		$this->disable_convert_filters = $disable_convert_filters;
52
-		// Set a reference to the logger.
53
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' );
54
-
55
-		self::$instance = $this;
56
-
57
-	}
58
-
59
-	public static function get_instance() {
60
-
61
-		return self::$instance;
62
-	}
63
-
64
-	public function new_instance_with_filters_disabled() {
65
-		return new static( $this->entity_type_service, $this->user_service, $this->attachment_service, true );
66
-	}
67
-
68
-	/**
69
-	 * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference
70
-	 * found while processing the post is set in the $references array.
71
-	 *
72
-	 * @param int              $post_id The post id.
73
-	 * @param array<Reference> $references An array of entity references.
74
-	 * @param array            $references_infos
75
-	 *
76
-	 * @return array A JSON-LD array.
77
-	 * @since 3.10.0
78
-	 */
79
-	public function convert( $post_id, &$references = array(), &$references_infos = array(), $relations = null ) {
80
-
81
-		// Get the post instance.
82
-		$post = get_post( $post_id );
83
-		if ( null === $post ) {
84
-			// Post not found.
85
-			return null;
86
-		}
87
-
88
-		// Get the base JSON-LD and the list of entities referenced by this entity.
89
-		$jsonld = parent::convert( $post_id, $references, $references_infos, $relations );
90
-
91
-		// Set WebPage by default.
92
-		if ( empty( $jsonld['@type'] ) ) {
93
-			$jsonld['@type'] = 'WebPage';
94
-		}
95
-
96
-		// Get the entity name.
97
-		$jsonld['headline'] = $post->post_title;
98
-
99
-		$custom_fields = $this->entity_type_service->get_custom_fields_for_post( $post_id );
100
-
101
-		if ( isset( $custom_fields ) ) {
102
-			$this->process_type_custom_fields( $jsonld, $custom_fields, $post, $references, $references_infos );
103
-		}
104
-
105
-		// Set the published and modified dates.
106
-		/*
21
+    /**
22
+     * @var Wordlift_Post_To_Jsonld_Converter
23
+     */
24
+    private static $instance;
25
+
26
+    /**
27
+     * A {@link Wordlift_Log_Service} instance.
28
+     *
29
+     * @since  3.10.0
30
+     * @access private
31
+     * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
32
+     */
33
+    private $log;
34
+
35
+    /**
36
+     * @var false
37
+     */
38
+    private $disable_convert_filters;
39
+
40
+    /**
41
+     * Wordlift_Post_To_Jsonld_Converter constructor.
42
+     *
43
+     * @param Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
44
+     * @param Wordlift_User_Service        $user_service A {@link Wordlift_User_Service} instance.
45
+     * @param Wordlift_Attachment_Service  $attachment_service A {@link Wordlift_Attachment_Service} instance.
46
+     *
47
+     * @since 3.10.0
48
+     */
49
+    public function __construct( $entity_type_service, $user_service, $attachment_service, $disable_convert_filters = false ) {
50
+        parent::__construct( $entity_type_service, $user_service, $attachment_service, Wordlift_Property_Getter_Factory::create() );
51
+        $this->disable_convert_filters = $disable_convert_filters;
52
+        // Set a reference to the logger.
53
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' );
54
+
55
+        self::$instance = $this;
56
+
57
+    }
58
+
59
+    public static function get_instance() {
60
+
61
+        return self::$instance;
62
+    }
63
+
64
+    public function new_instance_with_filters_disabled() {
65
+        return new static( $this->entity_type_service, $this->user_service, $this->attachment_service, true );
66
+    }
67
+
68
+    /**
69
+     * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference
70
+     * found while processing the post is set in the $references array.
71
+     *
72
+     * @param int              $post_id The post id.
73
+     * @param array<Reference> $references An array of entity references.
74
+     * @param array            $references_infos
75
+     *
76
+     * @return array A JSON-LD array.
77
+     * @since 3.10.0
78
+     */
79
+    public function convert( $post_id, &$references = array(), &$references_infos = array(), $relations = null ) {
80
+
81
+        // Get the post instance.
82
+        $post = get_post( $post_id );
83
+        if ( null === $post ) {
84
+            // Post not found.
85
+            return null;
86
+        }
87
+
88
+        // Get the base JSON-LD and the list of entities referenced by this entity.
89
+        $jsonld = parent::convert( $post_id, $references, $references_infos, $relations );
90
+
91
+        // Set WebPage by default.
92
+        if ( empty( $jsonld['@type'] ) ) {
93
+            $jsonld['@type'] = 'WebPage';
94
+        }
95
+
96
+        // Get the entity name.
97
+        $jsonld['headline'] = $post->post_title;
98
+
99
+        $custom_fields = $this->entity_type_service->get_custom_fields_for_post( $post_id );
100
+
101
+        if ( isset( $custom_fields ) ) {
102
+            $this->process_type_custom_fields( $jsonld, $custom_fields, $post, $references, $references_infos );
103
+        }
104
+
105
+        // Set the published and modified dates.
106
+        /*
107 107
 		 * Set the `datePublished` and `dateModified` using the local timezone.
108 108
 		 *
109 109
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/887
110 110
 		 *
111 111
 		 * @since 3.20.0
112 112
 		 */
113
-		try {
114
-			$default_timezone = date_default_timezone_get();
115
-			$timezone         = get_option( 'timezone_string' );
116
-			if ( ! empty( $timezone ) ) {
117
-				date_default_timezone_set( $timezone ); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
118
-				$jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post );
119
-				$jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post );
120
-				date_default_timezone_set( $default_timezone ); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
121
-			} else {
122
-				$jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false );
123
-				$jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false );
124
-			}
125
-		} catch ( Exception $e ) {
126
-			$jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false );
127
-			$jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false );
128
-		}
129
-
130
-		// Get the word count for the post.
131
-		/*
113
+        try {
114
+            $default_timezone = date_default_timezone_get();
115
+            $timezone         = get_option( 'timezone_string' );
116
+            if ( ! empty( $timezone ) ) {
117
+                date_default_timezone_set( $timezone ); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
118
+                $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post );
119
+                $jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post );
120
+                date_default_timezone_set( $default_timezone ); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
121
+            } else {
122
+                $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false );
123
+                $jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false );
124
+            }
125
+        } catch ( Exception $e ) {
126
+            $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false );
127
+            $jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false );
128
+        }
129
+
130
+        // Get the word count for the post.
131
+        /*
132 132
 		 * Do not display the `wordCount` on a `WebPage`.
133 133
 		 *
134 134
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/888
135 135
 		 *
136 136
 		 * @since 3.20.0
137 137
 		 */
138
-		if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) {
139
-			$post_adapter        = new Wordlift_Post_Adapter( $post_id );
140
-			$jsonld['wordCount'] = $post_adapter->word_count();
141
-		}
138
+        if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) {
139
+            $post_adapter        = new Wordlift_Post_Adapter( $post_id );
140
+            $jsonld['wordCount'] = $post_adapter->word_count();
141
+        }
142 142
 
143
-		/*
143
+        /*
144 144
 		 * Add keywords, articleSection, commentCount and inLanguage properties to `Article` JSON-LD
145 145
 		 *
146 146
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/1140
147 147
 		 *
148 148
 		 * @since 3.27.2
149 149
 		 */
150
-		if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) {
151
-			$post_adapter    = new Wordlift_Post_Adapter( $post_id );
152
-			$keywords        = $post_adapter->keywords();
153
-			$article_section = $post_adapter->article_section();
154
-			$comment_count   = $post_adapter->comment_count();
155
-			$locale          = $post_adapter->locale();
156
-
157
-			if ( isset( $keywords ) ) {
158
-				$jsonld['keywords'] = $keywords;
159
-			}
160
-			if ( ! empty( $article_section ) ) {
161
-				$jsonld['articleSection'] = $article_section;
162
-			}
163
-			$jsonld['commentCount'] = $comment_count;
164
-			$jsonld['inLanguage']   = $locale;
165
-		}
166
-
167
-		// Set the publisher.
168
-		$this->set_publisher( $jsonld );
169
-
170
-		/**
171
-		 * Call the `wl_post_jsonld_author` filter.
172
-		 *
173
-		 * This filter checks if there are co-authors or a single author and
174
-		 * returns a JSON-LD fragment for the author(s).
175
-		 *
176
-		 * @param array $value {
177
-		 *
178
-		 * @type array $jsonld The JSON-LD structure.
179
-		 * @type int[] $references An array of post IDs.
180
-		 * }
181
-		 *
182
-		 * @param int $post_id The {@link WP_Post} `id`.
183
-		 *
184
-		 * @since 3.51.4
185
-		 *
186
-		 * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/
187
-		 */
188
-		$ret_val = apply_filters(
189
-			'wl_jsonld_author',
190
-			array(
191
-				'author'     => $this->get_author( $post->post_author, $references ),
192
-				'references' => $references,
193
-			),
194
-			$post_id
195
-		);
196
-
197
-		// Set the values returned by the author filter.
198
-		/*
150
+        if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) {
151
+            $post_adapter    = new Wordlift_Post_Adapter( $post_id );
152
+            $keywords        = $post_adapter->keywords();
153
+            $article_section = $post_adapter->article_section();
154
+            $comment_count   = $post_adapter->comment_count();
155
+            $locale          = $post_adapter->locale();
156
+
157
+            if ( isset( $keywords ) ) {
158
+                $jsonld['keywords'] = $keywords;
159
+            }
160
+            if ( ! empty( $article_section ) ) {
161
+                $jsonld['articleSection'] = $article_section;
162
+            }
163
+            $jsonld['commentCount'] = $comment_count;
164
+            $jsonld['inLanguage']   = $locale;
165
+        }
166
+
167
+        // Set the publisher.
168
+        $this->set_publisher( $jsonld );
169
+
170
+        /**
171
+         * Call the `wl_post_jsonld_author` filter.
172
+         *
173
+         * This filter checks if there are co-authors or a single author and
174
+         * returns a JSON-LD fragment for the author(s).
175
+         *
176
+         * @param array $value {
177
+         *
178
+         * @type array $jsonld The JSON-LD structure.
179
+         * @type int[] $references An array of post IDs.
180
+         * }
181
+         *
182
+         * @param int $post_id The {@link WP_Post} `id`.
183
+         *
184
+         * @since 3.51.4
185
+         *
186
+         * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/
187
+         */
188
+        $ret_val = apply_filters(
189
+            'wl_jsonld_author',
190
+            array(
191
+                'author'     => $this->get_author( $post->post_author, $references ),
192
+                'references' => $references,
193
+            ),
194
+            $post_id
195
+        );
196
+
197
+        // Set the values returned by the author filter.
198
+        /*
199 199
 		 * Do not add the author JSON-LD if an invalid author was referenced in a post.
200 200
 		 *
201 201
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/1728
202 202
 		 *
203 203
 		 * @since 3.53.2
204 204
 		 */
205
-		if ( ! empty( $ret_val['author'] ) ) {
206
-			$jsonld['author'] = $ret_val['author'];
207
-			$references       = $ret_val['references'];
208
-		}
209
-
210
-		// Return the JSON-LD if filters are disabled by the client.
211
-		if ( $this->disable_convert_filters ) {
212
-			return $jsonld;
213
-		}
214
-
215
-		/**
216
-		 * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references.
217
-		 *
218
-		 * @param array $value {
219
-		 *
220
-		 * @type array $jsonld The JSON-LD structure.
221
-		 * @type int[] $references An array of post IDs.
222
-		 * @type Relations $relations A set of `Relation`s.
223
-		 * }
224
-		 * @since 3.25.0
225
-		 * @since 3.43.0 The filter provides a `Relations` instance.
226
-		 *
227
-		 * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/
228
-		 *
229
-		 * @api
230
-		 */
231
-		$ret_val = apply_filters(
232
-			'wl_post_jsonld_array',
233
-			array(
234
-				'jsonld'           => $jsonld,
235
-				'references'       => $references, // This one is only an array of post IDs.
236
-				'references_infos' => $references_infos,
237
-				'relations'        => $relations,
238
-			),
239
-			$post_id
240
-		);
241
-
242
-		$jsonld     = $ret_val['jsonld'];
243
-		$references = $ret_val['references'];
244
-
245
-		/**
246
-		 * Call the `wl_post_jsonld` filter.
247
-		 *
248
-		 * @param array $jsonld The JSON-LD structure.
249
-		 * @param int $post_id The {@link WP_Post} `id`.
250
-		 * @param array $references The array of referenced entities.
251
-		 *
252
-		 * @since 3.14.0
253
-		 *
254
-		 * @api
255
-		 */
256
-		return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references );
257
-	}
258
-
259
-	/**
260
-	 * Get the author's JSON-LD fragment.
261
-	 *
262
-	 * The JSON-LD fragment is generated using the {@link WP_User}'s data or
263
-	 * the referenced entity if configured for the {@link WP_User}.
264
-	 *
265
-	 * @param int   $author_id The author {@link WP_User}'s `id`.
266
-	 * @param array $references An array of referenced entities.
267
-	 *
268
-	 * @return string|array A JSON-LD structure.
269
-	 * @since 3.14.0
270
-	 */
271
-	public function get_author( $author_id, &$references ) {
272
-
273
-		// Get the entity bound to this user.
274
-		$entity_id = $this->user_service->get_entity( $author_id );
275
-
276
-		if ( ! empty( $entity_id ) && 'publish' === get_post_status( $entity_id ) ) {
277
-			// Add the author to the references.
278
-			$author_uri   = Wordlift_Entity_Service::get_instance()->get_uri( $entity_id );
279
-			$references[] = $entity_id;
280
-
281
-			// Return the JSON-LD for the referenced entity.
282
-			return array(
283
-				'@id' => $author_uri,
284
-			);
285
-		}
286
-
287
-		// If there's no entity bound return a simple author structure.
288
-		if ( false !== get_userdata( $author_id ) ) {
289
-			$author            = get_the_author_meta( 'display_name', $author_id );
290
-			$author_first_name = get_the_author_meta( 'first_name', $author_id );
291
-			$author_last_name  = get_the_author_meta( 'last_name', $author_id );
292
-			$author_uri        = $this->user_service->get_uri( $author_id );
293
-
294
-			return array(
295
-				'@type'      => 'Person',
296
-				'@id'        => $author_uri,
297
-				'name'       => $author,
298
-				'givenName'  => $author_first_name,
299
-				'familyName' => $author_last_name,
300
-				'url'        => get_author_posts_url( $author_id ),
301
-			);
302
-		}
303
-
304
-		// No valid entity or author so return empty array
305
-		return array();
306
-	}
307
-
308
-	/**
309
-	 * Enrich the provided params array with publisher data, if available.
310
-	 *
311
-	 * @param array $params The parameters array.
312
-	 *
313
-	 * @since 3.10.0
314
-	 */
315
-	protected function set_publisher( &$params ) {
316
-
317
-		// If the publisher id isn't set don't do anything.
318
-		$publisher_id = Wordlift_Configuration_Service::get_instance()->get_publisher_id();
319
-		if ( empty( $publisher_id ) ) {
320
-			return;
321
-		}
322
-
323
-		// Get the post instance.
324
-		$post = get_post( $publisher_id );
325
-		if ( ! is_a( $post, '\WP_Post' ) ) {
326
-			// Publisher not found.
327
-			return;
328
-		}
329
-
330
-		// Get the item id.
331
-		$id = Wordlift_Entity_Service::get_instance()->get_uri( $publisher_id );
332
-
333
-		// Get the type.
334
-		$type = $this->entity_type_service->get( $publisher_id );
335
-
336
-		// Get the name.
337
-		$name = $post->post_title;
338
-
339
-		// Set the publisher data.
340
-		$params['publisher'] = array(
341
-			'@type' => $this->relative_to_context( $type['uri'] ),
342
-			'@id'   => $id,
343
-			'name'  => $name,
344
-		);
345
-
346
-		// Add the sameAs values associated with the publisher.
347
-		$storage_factory = Wordlift_Storage_Factory::get_instance();
348
-		$sameas          = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id );
349
-		if ( ! empty( $sameas ) ) {
350
-			$params['publisher']['sameAs'] = $sameas;
351
-		}
352
-
353
-		// Set the logo, only for http://schema.org/Organization as Person doesn't
354
-		// support the logo property.
355
-		//
356
-		// See http://schema.org/logo.
357
-		if ( 1 !== preg_match( '~Organization$~', $type['uri'] ) ) {
358
-			return;
359
-		}
360
-
361
-		// Get the publisher logo.
362
-		$publisher_logo = $this->get_publisher_logo( $post->ID );
363
-
364
-		// Bail out if the publisher logo isn't set.
365
-		if ( false === $publisher_logo ) {
366
-			return;
367
-		}
368
-
369
-		// Copy over some useful properties.
370
-		//
371
-		// See https://developers.google.com/search/docs/data-types/articles.
372
-		$params['publisher']['logo']['@type'] = 'ImageObject';
373
-		$params['publisher']['logo']['url']   = $publisher_logo['url'];
374
-
375
-		// If you specify a "width" or "height" value you should leave out
376
-		// 'px'. For example: "width":"4608px" should be "width":"4608".
377
-		//
378
-		// See https://github.com/insideout10/wordlift-plugin/issues/451.
379
-		$params['publisher']['logo']['width']  = $publisher_logo['width'];
380
-		$params['publisher']['logo']['height'] = $publisher_logo['height'];
381
-
382
-	}
383
-
384
-	/**
385
-	 * Get the publisher logo structure.
386
-	 *
387
-	 * The function returns false when the publisher logo cannot be determined, i.e.:
388
-	 *  - the post has no featured image.
389
-	 *  - the featured image has no file.
390
-	 *  - a wp_image_editor instance cannot be instantiated on the original file or on the publisher logo file.
391
-	 *
392
-	 * @param int $post_id The post id.
393
-	 *
394
-	 * @return array|false Returns an array with the `url`, `width` and `height` for the publisher logo or false in case
395
-	 *  of errors.
396
-	 * @since 3.19.2
397
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue.
398
-	 */
399
-	private function get_publisher_logo( $post_id ) {
400
-
401
-		// Get the featured image for the post.
402
-		$thumbnail_id = get_post_thumbnail_id( $post_id );
403
-
404
-		// Bail out if thumbnail not available.
405
-		if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) {
406
-			$this->log->info( "Featured image not set for post $post_id." );
407
-
408
-			return false;
409
-		}
410
-
411
-		// Get the uploads base URL.
412
-		$uploads_dir = wp_upload_dir();
413
-
414
-		// Get the attachment metadata.
415
-		$metadata = wp_get_attachment_metadata( $thumbnail_id );
416
-
417
-		// Bail out if the file isn't set.
418
-		if ( ! isset( $metadata['file'] ) ) {
419
-			$this->log->warn( "Featured image file not found for post $post_id." );
420
-
421
-			return false;
422
-		}
423
-
424
-		// Retrieve the relative filename, e.g. "2018/05/logo_publisher.png"
425
-		$path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file'];
426
-
427
-		// Use image src, if local file does not exist. @see https://github.com/insideout10/wordlift-plugin/issues/1149
428
-		if ( ! file_exists( $path ) ) {
429
-			$this->log->warn( "Featured image file $path doesn't exist for post $post_id." );
430
-
431
-			$attachment_image_src = wp_get_attachment_image_src( $thumbnail_id, '' );
432
-			if ( $attachment_image_src ) {
433
-				return array(
434
-					'url'    => $attachment_image_src[0],
435
-					'width'  => $attachment_image_src[1],
436
-					'height' => $attachment_image_src[2],
437
-				);
438
-			}
439
-
440
-			// Bail out if we cant fetch wp_get_attachment_image_src
441
-			return false;
442
-
443
-		}
444
-
445
-		// Try to get the image editor and bail out if the editor cannot be instantiated.
446
-		$original_file_editor = wp_get_image_editor( $path );
447
-		if ( is_wp_error( $original_file_editor ) ) {
448
-			$this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." );
449
-
450
-			return false;
451
-		}
452
-
453
-		// Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling
454
-		// and we don't actually know the end values.
455
-		$publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' );
456
-
457
-		// If the file doesn't exist yet, create it.
458
-		if ( ! file_exists( $publisher_logo_path ) ) {
459
-			$original_file_editor->resize( 600, 60 );
460
-			$original_file_editor->save( $publisher_logo_path );
461
-		}
462
-
463
-		// Try to get the image editor and bail out if the editor cannot be instantiated.
464
-		$publisher_logo_editor = wp_get_image_editor( $publisher_logo_path );
465
-		if ( is_wp_error( $publisher_logo_editor ) ) {
466
-			$this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." );
467
-
468
-			return false;
469
-		}
470
-
471
-		// Get the actual size.
472
-		$size = $publisher_logo_editor->get_size();
473
-
474
-		// Finally return the array with data.
475
-		return array(
476
-			'url'    => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ),
477
-			'width'  => $size['width'],
478
-			'height' => $size['height'],
479
-		);
480
-	}
205
+        if ( ! empty( $ret_val['author'] ) ) {
206
+            $jsonld['author'] = $ret_val['author'];
207
+            $references       = $ret_val['references'];
208
+        }
209
+
210
+        // Return the JSON-LD if filters are disabled by the client.
211
+        if ( $this->disable_convert_filters ) {
212
+            return $jsonld;
213
+        }
214
+
215
+        /**
216
+         * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references.
217
+         *
218
+         * @param array $value {
219
+         *
220
+         * @type array $jsonld The JSON-LD structure.
221
+         * @type int[] $references An array of post IDs.
222
+         * @type Relations $relations A set of `Relation`s.
223
+         * }
224
+         * @since 3.25.0
225
+         * @since 3.43.0 The filter provides a `Relations` instance.
226
+         *
227
+         * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/
228
+         *
229
+         * @api
230
+         */
231
+        $ret_val = apply_filters(
232
+            'wl_post_jsonld_array',
233
+            array(
234
+                'jsonld'           => $jsonld,
235
+                'references'       => $references, // This one is only an array of post IDs.
236
+                'references_infos' => $references_infos,
237
+                'relations'        => $relations,
238
+            ),
239
+            $post_id
240
+        );
241
+
242
+        $jsonld     = $ret_val['jsonld'];
243
+        $references = $ret_val['references'];
244
+
245
+        /**
246
+         * Call the `wl_post_jsonld` filter.
247
+         *
248
+         * @param array $jsonld The JSON-LD structure.
249
+         * @param int $post_id The {@link WP_Post} `id`.
250
+         * @param array $references The array of referenced entities.
251
+         *
252
+         * @since 3.14.0
253
+         *
254
+         * @api
255
+         */
256
+        return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references );
257
+    }
258
+
259
+    /**
260
+     * Get the author's JSON-LD fragment.
261
+     *
262
+     * The JSON-LD fragment is generated using the {@link WP_User}'s data or
263
+     * the referenced entity if configured for the {@link WP_User}.
264
+     *
265
+     * @param int   $author_id The author {@link WP_User}'s `id`.
266
+     * @param array $references An array of referenced entities.
267
+     *
268
+     * @return string|array A JSON-LD structure.
269
+     * @since 3.14.0
270
+     */
271
+    public function get_author( $author_id, &$references ) {
272
+
273
+        // Get the entity bound to this user.
274
+        $entity_id = $this->user_service->get_entity( $author_id );
275
+
276
+        if ( ! empty( $entity_id ) && 'publish' === get_post_status( $entity_id ) ) {
277
+            // Add the author to the references.
278
+            $author_uri   = Wordlift_Entity_Service::get_instance()->get_uri( $entity_id );
279
+            $references[] = $entity_id;
280
+
281
+            // Return the JSON-LD for the referenced entity.
282
+            return array(
283
+                '@id' => $author_uri,
284
+            );
285
+        }
286
+
287
+        // If there's no entity bound return a simple author structure.
288
+        if ( false !== get_userdata( $author_id ) ) {
289
+            $author            = get_the_author_meta( 'display_name', $author_id );
290
+            $author_first_name = get_the_author_meta( 'first_name', $author_id );
291
+            $author_last_name  = get_the_author_meta( 'last_name', $author_id );
292
+            $author_uri        = $this->user_service->get_uri( $author_id );
293
+
294
+            return array(
295
+                '@type'      => 'Person',
296
+                '@id'        => $author_uri,
297
+                'name'       => $author,
298
+                'givenName'  => $author_first_name,
299
+                'familyName' => $author_last_name,
300
+                'url'        => get_author_posts_url( $author_id ),
301
+            );
302
+        }
303
+
304
+        // No valid entity or author so return empty array
305
+        return array();
306
+    }
307
+
308
+    /**
309
+     * Enrich the provided params array with publisher data, if available.
310
+     *
311
+     * @param array $params The parameters array.
312
+     *
313
+     * @since 3.10.0
314
+     */
315
+    protected function set_publisher( &$params ) {
316
+
317
+        // If the publisher id isn't set don't do anything.
318
+        $publisher_id = Wordlift_Configuration_Service::get_instance()->get_publisher_id();
319
+        if ( empty( $publisher_id ) ) {
320
+            return;
321
+        }
322
+
323
+        // Get the post instance.
324
+        $post = get_post( $publisher_id );
325
+        if ( ! is_a( $post, '\WP_Post' ) ) {
326
+            // Publisher not found.
327
+            return;
328
+        }
329
+
330
+        // Get the item id.
331
+        $id = Wordlift_Entity_Service::get_instance()->get_uri( $publisher_id );
332
+
333
+        // Get the type.
334
+        $type = $this->entity_type_service->get( $publisher_id );
335
+
336
+        // Get the name.
337
+        $name = $post->post_title;
338
+
339
+        // Set the publisher data.
340
+        $params['publisher'] = array(
341
+            '@type' => $this->relative_to_context( $type['uri'] ),
342
+            '@id'   => $id,
343
+            'name'  => $name,
344
+        );
345
+
346
+        // Add the sameAs values associated with the publisher.
347
+        $storage_factory = Wordlift_Storage_Factory::get_instance();
348
+        $sameas          = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id );
349
+        if ( ! empty( $sameas ) ) {
350
+            $params['publisher']['sameAs'] = $sameas;
351
+        }
352
+
353
+        // Set the logo, only for http://schema.org/Organization as Person doesn't
354
+        // support the logo property.
355
+        //
356
+        // See http://schema.org/logo.
357
+        if ( 1 !== preg_match( '~Organization$~', $type['uri'] ) ) {
358
+            return;
359
+        }
360
+
361
+        // Get the publisher logo.
362
+        $publisher_logo = $this->get_publisher_logo( $post->ID );
363
+
364
+        // Bail out if the publisher logo isn't set.
365
+        if ( false === $publisher_logo ) {
366
+            return;
367
+        }
368
+
369
+        // Copy over some useful properties.
370
+        //
371
+        // See https://developers.google.com/search/docs/data-types/articles.
372
+        $params['publisher']['logo']['@type'] = 'ImageObject';
373
+        $params['publisher']['logo']['url']   = $publisher_logo['url'];
374
+
375
+        // If you specify a "width" or "height" value you should leave out
376
+        // 'px'. For example: "width":"4608px" should be "width":"4608".
377
+        //
378
+        // See https://github.com/insideout10/wordlift-plugin/issues/451.
379
+        $params['publisher']['logo']['width']  = $publisher_logo['width'];
380
+        $params['publisher']['logo']['height'] = $publisher_logo['height'];
381
+
382
+    }
383
+
384
+    /**
385
+     * Get the publisher logo structure.
386
+     *
387
+     * The function returns false when the publisher logo cannot be determined, i.e.:
388
+     *  - the post has no featured image.
389
+     *  - the featured image has no file.
390
+     *  - a wp_image_editor instance cannot be instantiated on the original file or on the publisher logo file.
391
+     *
392
+     * @param int $post_id The post id.
393
+     *
394
+     * @return array|false Returns an array with the `url`, `width` and `height` for the publisher logo or false in case
395
+     *  of errors.
396
+     * @since 3.19.2
397
+     * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue.
398
+     */
399
+    private function get_publisher_logo( $post_id ) {
400
+
401
+        // Get the featured image for the post.
402
+        $thumbnail_id = get_post_thumbnail_id( $post_id );
403
+
404
+        // Bail out if thumbnail not available.
405
+        if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) {
406
+            $this->log->info( "Featured image not set for post $post_id." );
407
+
408
+            return false;
409
+        }
410
+
411
+        // Get the uploads base URL.
412
+        $uploads_dir = wp_upload_dir();
413
+
414
+        // Get the attachment metadata.
415
+        $metadata = wp_get_attachment_metadata( $thumbnail_id );
416
+
417
+        // Bail out if the file isn't set.
418
+        if ( ! isset( $metadata['file'] ) ) {
419
+            $this->log->warn( "Featured image file not found for post $post_id." );
420
+
421
+            return false;
422
+        }
423
+
424
+        // Retrieve the relative filename, e.g. "2018/05/logo_publisher.png"
425
+        $path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file'];
426
+
427
+        // Use image src, if local file does not exist. @see https://github.com/insideout10/wordlift-plugin/issues/1149
428
+        if ( ! file_exists( $path ) ) {
429
+            $this->log->warn( "Featured image file $path doesn't exist for post $post_id." );
430
+
431
+            $attachment_image_src = wp_get_attachment_image_src( $thumbnail_id, '' );
432
+            if ( $attachment_image_src ) {
433
+                return array(
434
+                    'url'    => $attachment_image_src[0],
435
+                    'width'  => $attachment_image_src[1],
436
+                    'height' => $attachment_image_src[2],
437
+                );
438
+            }
439
+
440
+            // Bail out if we cant fetch wp_get_attachment_image_src
441
+            return false;
442
+
443
+        }
444
+
445
+        // Try to get the image editor and bail out if the editor cannot be instantiated.
446
+        $original_file_editor = wp_get_image_editor( $path );
447
+        if ( is_wp_error( $original_file_editor ) ) {
448
+            $this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." );
449
+
450
+            return false;
451
+        }
452
+
453
+        // Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling
454
+        // and we don't actually know the end values.
455
+        $publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' );
456
+
457
+        // If the file doesn't exist yet, create it.
458
+        if ( ! file_exists( $publisher_logo_path ) ) {
459
+            $original_file_editor->resize( 600, 60 );
460
+            $original_file_editor->save( $publisher_logo_path );
461
+        }
462
+
463
+        // Try to get the image editor and bail out if the editor cannot be instantiated.
464
+        $publisher_logo_editor = wp_get_image_editor( $publisher_logo_path );
465
+        if ( is_wp_error( $publisher_logo_editor ) ) {
466
+            $this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." );
467
+
468
+            return false;
469
+        }
470
+
471
+        // Get the actual size.
472
+        $size = $publisher_logo_editor->get_size();
473
+
474
+        // Finally return the array with data.
475
+        return array(
476
+            'url'    => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ),
477
+            'width'  => $size['width'],
478
+            'height' => $size['height'],
479
+        );
480
+    }
481 481
 
482 482
 }
Please login to merge, or discard this patch.