@@ -16,95 +16,95 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Property_Entity_Reference { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The URL associated with this entity reference. |
|
| 21 | - * |
|
| 22 | - * @since 3.8.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var string $url The URL associated with the entity reference. |
|
| 25 | - */ |
|
| 26 | - private $url; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The entity post id. |
|
| 30 | - * |
|
| 31 | - * @since 3.10.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var int $id The entity post id. |
|
| 34 | - */ |
|
| 35 | - private $id; |
|
| 36 | - |
|
| 37 | - private $required; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Create a Wordlift_Property_Entity_Reference instance with the provided URL. |
|
| 41 | - * |
|
| 42 | - * @param string $url The URL. |
|
| 43 | - * @param int $id The entity post id. |
|
| 44 | - * @param bool $required Whether this property is always required in SD output, default false. |
|
| 45 | - * |
|
| 46 | - * @since 3.8.0 |
|
| 47 | - */ |
|
| 48 | - public function __construct( $url, $id, $required = false ) { |
|
| 49 | - |
|
| 50 | - $this->url = $url; |
|
| 51 | - $this->id = $id; |
|
| 52 | - $this->required = $required; |
|
| 53 | - |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Get the URL associated with this entity reference. |
|
| 58 | - * |
|
| 59 | - * @return string The URL associated with the entity reference. |
|
| 60 | - * @since 3.8.0 |
|
| 61 | - * |
|
| 62 | - */ |
|
| 63 | - public function get_url() { |
|
| 64 | - |
|
| 65 | - return $this->url; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Get the entity id. |
|
| 70 | - * |
|
| 71 | - * @return int The entity id. |
|
| 72 | - * @since 3.10.0 |
|
| 73 | - * |
|
| 74 | - */ |
|
| 75 | - public function get_id() { |
|
| 76 | - |
|
| 77 | - return $this->id; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Get the required flag for this {@link Wordlift_Property_Entity_Reference}. |
|
| 82 | - * |
|
| 83 | - * The required flag may tell converters or consumers (like {@link \Wordlift\Jsonld\Jsonld_Service} that this |
|
| 84 | - * property needs to be output to SD (JSON-LD). |
|
| 85 | - * |
|
| 86 | - * @since 3.27.1 |
|
| 87 | - */ |
|
| 88 | - public function get_required() { |
|
| 89 | - |
|
| 90 | - return $this->required; |
|
| 91 | - |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Set the required flag for this {@link Wordlift_Property_Entity_Reference}. |
|
| 96 | - * |
|
| 97 | - * The required flag may tell converters or consumers (like {@link \Wordlift\Jsonld\Jsonld_Service} that this |
|
| 98 | - * property needs to be output to SD (JSON-LD). |
|
| 99 | - * |
|
| 100 | - * @param bool $value |
|
| 101 | - * |
|
| 102 | - * @since 3.27.1 |
|
| 103 | - */ |
|
| 104 | - public function set_required( $value ) { |
|
| 105 | - |
|
| 106 | - $this->required = $value; |
|
| 107 | - |
|
| 108 | - } |
|
| 19 | + /** |
|
| 20 | + * The URL associated with this entity reference. |
|
| 21 | + * |
|
| 22 | + * @since 3.8.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var string $url The URL associated with the entity reference. |
|
| 25 | + */ |
|
| 26 | + private $url; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The entity post id. |
|
| 30 | + * |
|
| 31 | + * @since 3.10.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var int $id The entity post id. |
|
| 34 | + */ |
|
| 35 | + private $id; |
|
| 36 | + |
|
| 37 | + private $required; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Create a Wordlift_Property_Entity_Reference instance with the provided URL. |
|
| 41 | + * |
|
| 42 | + * @param string $url The URL. |
|
| 43 | + * @param int $id The entity post id. |
|
| 44 | + * @param bool $required Whether this property is always required in SD output, default false. |
|
| 45 | + * |
|
| 46 | + * @since 3.8.0 |
|
| 47 | + */ |
|
| 48 | + public function __construct( $url, $id, $required = false ) { |
|
| 49 | + |
|
| 50 | + $this->url = $url; |
|
| 51 | + $this->id = $id; |
|
| 52 | + $this->required = $required; |
|
| 53 | + |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Get the URL associated with this entity reference. |
|
| 58 | + * |
|
| 59 | + * @return string The URL associated with the entity reference. |
|
| 60 | + * @since 3.8.0 |
|
| 61 | + * |
|
| 62 | + */ |
|
| 63 | + public function get_url() { |
|
| 64 | + |
|
| 65 | + return $this->url; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Get the entity id. |
|
| 70 | + * |
|
| 71 | + * @return int The entity id. |
|
| 72 | + * @since 3.10.0 |
|
| 73 | + * |
|
| 74 | + */ |
|
| 75 | + public function get_id() { |
|
| 76 | + |
|
| 77 | + return $this->id; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Get the required flag for this {@link Wordlift_Property_Entity_Reference}. |
|
| 82 | + * |
|
| 83 | + * The required flag may tell converters or consumers (like {@link \Wordlift\Jsonld\Jsonld_Service} that this |
|
| 84 | + * property needs to be output to SD (JSON-LD). |
|
| 85 | + * |
|
| 86 | + * @since 3.27.1 |
|
| 87 | + */ |
|
| 88 | + public function get_required() { |
|
| 89 | + |
|
| 90 | + return $this->required; |
|
| 91 | + |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Set the required flag for this {@link Wordlift_Property_Entity_Reference}. |
|
| 96 | + * |
|
| 97 | + * The required flag may tell converters or consumers (like {@link \Wordlift\Jsonld\Jsonld_Service} that this |
|
| 98 | + * property needs to be output to SD (JSON-LD). |
|
| 99 | + * |
|
| 100 | + * @param bool $value |
|
| 101 | + * |
|
| 102 | + * @since 3.27.1 |
|
| 103 | + */ |
|
| 104 | + public function set_required( $value ) { |
|
| 105 | + |
|
| 106 | + $this->required = $value; |
|
| 107 | + |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | 110 | } |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | * |
| 46 | 46 | * @since 3.8.0 |
| 47 | 47 | */ |
| 48 | - public function __construct( $url, $id, $required = false ) { |
|
| 48 | + public function __construct($url, $id, $required = false) { |
|
| 49 | 49 | |
| 50 | 50 | $this->url = $url; |
| 51 | 51 | $this->id = $id; |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | * |
| 102 | 102 | * @since 3.27.1 |
| 103 | 103 | */ |
| 104 | - public function set_required( $value ) { |
|
| 104 | + public function set_required($value) { |
|
| 105 | 105 | |
| 106 | 106 | $this->required = $value; |
| 107 | 107 | |
@@ -12,15 +12,15 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Wordlift_Install_3_27_1 extends Wordlift_Install { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * {@inheritdoc} |
|
| 17 | - */ |
|
| 18 | - protected static $version = '3.27.1'; |
|
| 15 | + /** |
|
| 16 | + * {@inheritdoc} |
|
| 17 | + */ |
|
| 18 | + protected static $version = '3.27.1'; |
|
| 19 | 19 | |
| 20 | - public function install() { |
|
| 21 | - $procedure = new Recipe_Maker_Entity_Type_Procedure(); |
|
| 22 | - $procedure->run_procedure(); |
|
| 23 | - } |
|
| 20 | + public function install() { |
|
| 21 | + $procedure = new Recipe_Maker_Entity_Type_Procedure(); |
|
| 22 | + $procedure->run_procedure(); |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | 25 | |
| 26 | 26 | } |
@@ -11,32 +11,32 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class Recipe_Maker_Post_Type_Hook { |
| 13 | 13 | |
| 14 | - const RECIPE_MAKER_POST_TYPE = 'wprm_recipe'; |
|
| 14 | + const RECIPE_MAKER_POST_TYPE = 'wprm_recipe'; |
|
| 15 | 15 | |
| 16 | - public function __construct() { |
|
| 16 | + public function __construct() { |
|
| 17 | 17 | |
| 18 | - add_filter( 'wl_default_entity_type_for_post_type', |
|
| 19 | - array( $this, 'wl_default_entity_type_for_post_type' ), 10, 2 ); |
|
| 18 | + add_filter( 'wl_default_entity_type_for_post_type', |
|
| 19 | + array( $this, 'wl_default_entity_type_for_post_type' ), 10, 2 ); |
|
| 20 | 20 | |
| 21 | - add_filter( 'wl_valid_entity_post_types', array( $this, 'add_post_type' ) ); |
|
| 21 | + add_filter( 'wl_valid_entity_post_types', array( $this, 'add_post_type' ) ); |
|
| 22 | 22 | |
| 23 | 23 | |
| 24 | - } |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - public function add_post_type( $post_types ) { |
|
| 26 | + public function add_post_type( $post_types ) { |
|
| 27 | 27 | |
| 28 | - $post_types[] = self::RECIPE_MAKER_POST_TYPE; |
|
| 28 | + $post_types[] = self::RECIPE_MAKER_POST_TYPE; |
|
| 29 | 29 | |
| 30 | - return $post_types; |
|
| 31 | - } |
|
| 30 | + return $post_types; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | 33 | |
| 34 | - public function wl_default_entity_type_for_post_type( $entity_type, $post_type ) { |
|
| 34 | + public function wl_default_entity_type_for_post_type( $entity_type, $post_type ) { |
|
| 35 | 35 | |
| 36 | - if ( self::RECIPE_MAKER_POST_TYPE === $post_type ) { |
|
| 37 | - return 'http://schema.org/Recipe'; |
|
| 38 | - } |
|
| 36 | + if ( self::RECIPE_MAKER_POST_TYPE === $post_type ) { |
|
| 37 | + return 'http://schema.org/Recipe'; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - return $entity_type; |
|
| 41 | - } |
|
| 40 | + return $entity_type; |
|
| 41 | + } |
|
| 42 | 42 | } |
@@ -15,15 +15,15 @@ discard block |
||
| 15 | 15 | |
| 16 | 16 | public function __construct() { |
| 17 | 17 | |
| 18 | - add_filter( 'wl_default_entity_type_for_post_type', |
|
| 19 | - array( $this, 'wl_default_entity_type_for_post_type' ), 10, 2 ); |
|
| 18 | + add_filter('wl_default_entity_type_for_post_type', |
|
| 19 | + array($this, 'wl_default_entity_type_for_post_type'), 10, 2); |
|
| 20 | 20 | |
| 21 | - add_filter( 'wl_valid_entity_post_types', array( $this, 'add_post_type' ) ); |
|
| 21 | + add_filter('wl_valid_entity_post_types', array($this, 'add_post_type')); |
|
| 22 | 22 | |
| 23 | 23 | |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | - public function add_post_type( $post_types ) { |
|
| 26 | + public function add_post_type($post_types) { |
|
| 27 | 27 | |
| 28 | 28 | $post_types[] = self::RECIPE_MAKER_POST_TYPE; |
| 29 | 29 | |
@@ -31,9 +31,9 @@ discard block |
||
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | |
| 34 | - public function wl_default_entity_type_for_post_type( $entity_type, $post_type ) { |
|
| 34 | + public function wl_default_entity_type_for_post_type($entity_type, $post_type) { |
|
| 35 | 35 | |
| 36 | - if ( self::RECIPE_MAKER_POST_TYPE === $post_type ) { |
|
| 36 | + if (self::RECIPE_MAKER_POST_TYPE === $post_type) { |
|
| 37 | 37 | return 'http://schema.org/Recipe'; |
| 38 | 38 | } |
| 39 | 39 | |
@@ -10,30 +10,30 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class Recipe_Maker_Entity_Type_Procedure { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * Run the procedure and set all wprecipe post type |
|
| 15 | - * to 'Recipe' in entity type. |
|
| 16 | - */ |
|
| 17 | - public function run_procedure() { |
|
| 18 | - $posts = $this->get_all_published_recipe_maker_posts(); |
|
| 19 | - foreach ( $posts as $post_id ) { |
|
| 20 | - // set entity type to Product. |
|
| 21 | - \Wordlift_Entity_Type_Service::get_instance() |
|
| 22 | - ->set( $post_id, |
|
| 23 | - 'http://schema.org/Recipe', true ); |
|
| 24 | - } |
|
| 25 | - } |
|
| 13 | + /** |
|
| 14 | + * Run the procedure and set all wprecipe post type |
|
| 15 | + * to 'Recipe' in entity type. |
|
| 16 | + */ |
|
| 17 | + public function run_procedure() { |
|
| 18 | + $posts = $this->get_all_published_recipe_maker_posts(); |
|
| 19 | + foreach ( $posts as $post_id ) { |
|
| 20 | + // set entity type to Product. |
|
| 21 | + \Wordlift_Entity_Type_Service::get_instance() |
|
| 22 | + ->set( $post_id, |
|
| 23 | + 'http://schema.org/Recipe', true ); |
|
| 24 | + } |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @return int[]|\WP_Post[] |
|
| 29 | - */ |
|
| 30 | - private function get_all_published_recipe_maker_posts() { |
|
| 31 | - return get_posts( array( |
|
| 32 | - 'post_type' => Recipe_Maker_Post_Type_Hook::RECIPE_MAKER_POST_TYPE, |
|
| 33 | - 'posts_per_page' => - 1, |
|
| 34 | - 'post_status' => 'publish', |
|
| 35 | - 'fields' => 'ids' |
|
| 36 | - ) ); |
|
| 37 | - } |
|
| 27 | + /** |
|
| 28 | + * @return int[]|\WP_Post[] |
|
| 29 | + */ |
|
| 30 | + private function get_all_published_recipe_maker_posts() { |
|
| 31 | + return get_posts( array( |
|
| 32 | + 'post_type' => Recipe_Maker_Post_Type_Hook::RECIPE_MAKER_POST_TYPE, |
|
| 33 | + 'posts_per_page' => - 1, |
|
| 34 | + 'post_status' => 'publish', |
|
| 35 | + 'fields' => 'ids' |
|
| 36 | + ) ); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | 39 | } |
@@ -16,11 +16,11 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | public function run_procedure() { |
| 18 | 18 | $posts = $this->get_all_published_recipe_maker_posts(); |
| 19 | - foreach ( $posts as $post_id ) { |
|
| 19 | + foreach ($posts as $post_id) { |
|
| 20 | 20 | // set entity type to Product. |
| 21 | 21 | \Wordlift_Entity_Type_Service::get_instance() |
| 22 | - ->set( $post_id, |
|
| 23 | - 'http://schema.org/Recipe', true ); |
|
| 22 | + ->set($post_id, |
|
| 23 | + 'http://schema.org/Recipe', true); |
|
| 24 | 24 | } |
| 25 | 25 | } |
| 26 | 26 | |
@@ -28,12 +28,12 @@ discard block |
||
| 28 | 28 | * @return int[]|\WP_Post[] |
| 29 | 29 | */ |
| 30 | 30 | private function get_all_published_recipe_maker_posts() { |
| 31 | - return get_posts( array( |
|
| 31 | + return get_posts(array( |
|
| 32 | 32 | 'post_type' => Recipe_Maker_Post_Type_Hook::RECIPE_MAKER_POST_TYPE, |
| 33 | - 'posts_per_page' => - 1, |
|
| 33 | + 'posts_per_page' => -1, |
|
| 34 | 34 | 'post_status' => 'publish', |
| 35 | 35 | 'fields' => 'ids' |
| 36 | - ) ); |
|
| 36 | + )); |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | } |
@@ -7,31 +7,31 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Recipe_Maker_Validation_Service { |
| 9 | 9 | |
| 10 | - public function is_atleast_once_recipe_present_in_the_post( $post_id ) { |
|
| 10 | + public function is_atleast_once_recipe_present_in_the_post( $post_id ) { |
|
| 11 | 11 | |
| 12 | - if ( ! $this->is_wp_recipe_maker_available() ) { |
|
| 13 | - return false; |
|
| 14 | - } |
|
| 15 | - $recipe_ids = \WPRM_Recipe_Manager::get_recipe_ids_from_post( $post_id ); |
|
| 12 | + if ( ! $this->is_wp_recipe_maker_available() ) { |
|
| 13 | + return false; |
|
| 14 | + } |
|
| 15 | + $recipe_ids = \WPRM_Recipe_Manager::get_recipe_ids_from_post( $post_id ); |
|
| 16 | 16 | |
| 17 | - return is_array( $recipe_ids ) ? count( $recipe_ids ) > 0 : false; |
|
| 18 | - } |
|
| 17 | + return is_array( $recipe_ids ) ? count( $recipe_ids ) > 0 : false; |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - public function is_wp_recipe_maker_available() { |
|
| 21 | - /** |
|
| 22 | - * Dont alter the jsonld if the classes are not present. |
|
| 23 | - */ |
|
| 24 | - if ( ! class_exists( '\WPRM_Recipe_Manager' ) || ! class_exists( 'WPRM_Metadata' ) ) { |
|
| 25 | - return false; |
|
| 26 | - } |
|
| 27 | - if ( ! method_exists( '\WPRM_Recipe_Manager', 'get_recipe_ids_from_post' ) || |
|
| 28 | - ! method_exists( '\WPRM_Recipe_Manager', 'get_recipe' ) || |
|
| 29 | - ! method_exists( '\WPRM_Metadata', 'get_metadata_details' ) |
|
| 30 | - ) { |
|
| 31 | - return false; |
|
| 32 | - } |
|
| 20 | + public function is_wp_recipe_maker_available() { |
|
| 21 | + /** |
|
| 22 | + * Dont alter the jsonld if the classes are not present. |
|
| 23 | + */ |
|
| 24 | + if ( ! class_exists( '\WPRM_Recipe_Manager' ) || ! class_exists( 'WPRM_Metadata' ) ) { |
|
| 25 | + return false; |
|
| 26 | + } |
|
| 27 | + if ( ! method_exists( '\WPRM_Recipe_Manager', 'get_recipe_ids_from_post' ) || |
|
| 28 | + ! method_exists( '\WPRM_Recipe_Manager', 'get_recipe' ) || |
|
| 29 | + ! method_exists( '\WPRM_Metadata', 'get_metadata_details' ) |
|
| 30 | + ) { |
|
| 31 | + return false; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - return true; |
|
| 35 | - } |
|
| 34 | + return true; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | 37 | } |
@@ -7,26 +7,26 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Recipe_Maker_Validation_Service { |
| 9 | 9 | |
| 10 | - public function is_atleast_once_recipe_present_in_the_post( $post_id ) { |
|
| 10 | + public function is_atleast_once_recipe_present_in_the_post($post_id) { |
|
| 11 | 11 | |
| 12 | - if ( ! $this->is_wp_recipe_maker_available() ) { |
|
| 12 | + if ( ! $this->is_wp_recipe_maker_available()) { |
|
| 13 | 13 | return false; |
| 14 | 14 | } |
| 15 | - $recipe_ids = \WPRM_Recipe_Manager::get_recipe_ids_from_post( $post_id ); |
|
| 15 | + $recipe_ids = \WPRM_Recipe_Manager::get_recipe_ids_from_post($post_id); |
|
| 16 | 16 | |
| 17 | - return is_array( $recipe_ids ) ? count( $recipe_ids ) > 0 : false; |
|
| 17 | + return is_array($recipe_ids) ? count($recipe_ids) > 0 : false; |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | public function is_wp_recipe_maker_available() { |
| 21 | 21 | /** |
| 22 | 22 | * Dont alter the jsonld if the classes are not present. |
| 23 | 23 | */ |
| 24 | - if ( ! class_exists( '\WPRM_Recipe_Manager' ) || ! class_exists( 'WPRM_Metadata' ) ) { |
|
| 24 | + if ( ! class_exists('\WPRM_Recipe_Manager') || ! class_exists('WPRM_Metadata')) { |
|
| 25 | 25 | return false; |
| 26 | 26 | } |
| 27 | - if ( ! method_exists( '\WPRM_Recipe_Manager', 'get_recipe_ids_from_post' ) || |
|
| 28 | - ! method_exists( '\WPRM_Recipe_Manager', 'get_recipe' ) || |
|
| 29 | - ! method_exists( '\WPRM_Metadata', 'get_metadata_details' ) |
|
| 27 | + if ( ! method_exists('\WPRM_Recipe_Manager', 'get_recipe_ids_from_post') || |
|
| 28 | + ! method_exists('\WPRM_Recipe_Manager', 'get_recipe') || |
|
| 29 | + ! method_exists('\WPRM_Metadata', 'get_metadata_details') |
|
| 30 | 30 | ) { |
| 31 | 31 | return false; |
| 32 | 32 | } |
@@ -15,61 +15,61 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Recipe_Maker_After_Get_Jsonld_Hook { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var Recipe_Maker_Validation_Service |
|
| 20 | - */ |
|
| 21 | - private $recipe_maker_validation_service; |
|
| 18 | + /** |
|
| 19 | + * @var Recipe_Maker_Validation_Service |
|
| 20 | + */ |
|
| 21 | + private $recipe_maker_validation_service; |
|
| 22 | 22 | |
| 23 | - public function __construct( $recipe_maker_validation_service ) { |
|
| 24 | - $this->recipe_maker_validation_service = $recipe_maker_validation_service; |
|
| 25 | - // Add the filter to alter final jsonld. |
|
| 26 | - add_filter( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 10, 2 ); |
|
| 27 | - } |
|
| 23 | + public function __construct( $recipe_maker_validation_service ) { |
|
| 24 | + $this->recipe_maker_validation_service = $recipe_maker_validation_service; |
|
| 25 | + // Add the filter to alter final jsonld. |
|
| 26 | + add_filter( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 10, 2 ); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Add isPartOf to all the recipes. |
|
| 31 | - * |
|
| 32 | - * @param $jsonld array |
|
| 33 | - * |
|
| 34 | - * @param $post_id int |
|
| 35 | - * |
|
| 36 | - * @return array The altered jsonld array. |
|
| 37 | - */ |
|
| 38 | - public function wl_after_get_jsonld( $jsonld, $post_id ) { |
|
| 39 | - if ( ! is_array( $jsonld ) || count( $jsonld ) === 0 ) { |
|
| 40 | - return $jsonld; |
|
| 41 | - } |
|
| 42 | - // If there are no recipes in the post then dont alter the jsonld. |
|
| 43 | - if ( ! $this->recipe_maker_validation_service->is_atleast_once_recipe_present_in_the_post( $post_id ) ) { |
|
| 44 | - return $jsonld; |
|
| 45 | - } |
|
| 29 | + /** |
|
| 30 | + * Add isPartOf to all the recipes. |
|
| 31 | + * |
|
| 32 | + * @param $jsonld array |
|
| 33 | + * |
|
| 34 | + * @param $post_id int |
|
| 35 | + * |
|
| 36 | + * @return array The altered jsonld array. |
|
| 37 | + */ |
|
| 38 | + public function wl_after_get_jsonld( $jsonld, $post_id ) { |
|
| 39 | + if ( ! is_array( $jsonld ) || count( $jsonld ) === 0 ) { |
|
| 40 | + return $jsonld; |
|
| 41 | + } |
|
| 42 | + // If there are no recipes in the post then dont alter the jsonld. |
|
| 43 | + if ( ! $this->recipe_maker_validation_service->is_atleast_once_recipe_present_in_the_post( $post_id ) ) { |
|
| 44 | + return $jsonld; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - // We remove the current post jsonld. |
|
| 48 | - $post_jsonld = array_shift( $jsonld ); |
|
| 49 | - $post_jsonld_id = array_key_exists( '@id', $post_jsonld ) ? $post_jsonld['@id'] : false; |
|
| 47 | + // We remove the current post jsonld. |
|
| 48 | + $post_jsonld = array_shift( $jsonld ); |
|
| 49 | + $post_jsonld_id = array_key_exists( '@id', $post_jsonld ) ? $post_jsonld['@id'] : false; |
|
| 50 | 50 | |
| 51 | - if ( ! $post_jsonld_id ) { |
|
| 52 | - return $jsonld; |
|
| 53 | - } |
|
| 51 | + if ( ! $post_jsonld_id ) { |
|
| 52 | + return $jsonld; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - foreach ( $jsonld as $key => $value ) { |
|
| 56 | - if ( ! array_key_exists( '@type', $value ) ) { |
|
| 57 | - continue; |
|
| 58 | - } |
|
| 59 | - $type = $value['@type']; |
|
| 60 | - if ( $type === 'Recipe' ) { |
|
| 61 | - $value['isPartOf'] = array( |
|
| 62 | - '@id' => $post_jsonld_id |
|
| 63 | - ); |
|
| 64 | - $jsonld[ $key ] = $value; |
|
| 65 | - } |
|
| 66 | - } |
|
| 55 | + foreach ( $jsonld as $key => $value ) { |
|
| 56 | + if ( ! array_key_exists( '@type', $value ) ) { |
|
| 57 | + continue; |
|
| 58 | + } |
|
| 59 | + $type = $value['@type']; |
|
| 60 | + if ( $type === 'Recipe' ) { |
|
| 61 | + $value['isPartOf'] = array( |
|
| 62 | + '@id' => $post_jsonld_id |
|
| 63 | + ); |
|
| 64 | + $jsonld[ $key ] = $value; |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - // Add back the post jsonld to first of array. |
|
| 69 | - array_unshift( $jsonld, $post_jsonld ); |
|
| 68 | + // Add back the post jsonld to first of array. |
|
| 69 | + array_unshift( $jsonld, $post_jsonld ); |
|
| 70 | 70 | |
| 71 | - return $jsonld; |
|
| 72 | - } |
|
| 71 | + return $jsonld; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | 74 | |
| 75 | 75 | } |
@@ -20,10 +20,10 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | private $recipe_maker_validation_service; |
| 22 | 22 | |
| 23 | - public function __construct( $recipe_maker_validation_service ) { |
|
| 23 | + public function __construct($recipe_maker_validation_service) { |
|
| 24 | 24 | $this->recipe_maker_validation_service = $recipe_maker_validation_service; |
| 25 | 25 | // Add the filter to alter final jsonld. |
| 26 | - add_filter( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 10, 2 ); |
|
| 26 | + add_filter('wl_after_get_jsonld', array($this, 'wl_after_get_jsonld'), 10, 2); |
|
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | /** |
@@ -35,38 +35,38 @@ discard block |
||
| 35 | 35 | * |
| 36 | 36 | * @return array The altered jsonld array. |
| 37 | 37 | */ |
| 38 | - public function wl_after_get_jsonld( $jsonld, $post_id ) { |
|
| 39 | - if ( ! is_array( $jsonld ) || count( $jsonld ) === 0 ) { |
|
| 38 | + public function wl_after_get_jsonld($jsonld, $post_id) { |
|
| 39 | + if ( ! is_array($jsonld) || count($jsonld) === 0) { |
|
| 40 | 40 | return $jsonld; |
| 41 | 41 | } |
| 42 | 42 | // If there are no recipes in the post then dont alter the jsonld. |
| 43 | - if ( ! $this->recipe_maker_validation_service->is_atleast_once_recipe_present_in_the_post( $post_id ) ) { |
|
| 43 | + if ( ! $this->recipe_maker_validation_service->is_atleast_once_recipe_present_in_the_post($post_id)) { |
|
| 44 | 44 | return $jsonld; |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | // We remove the current post jsonld. |
| 48 | - $post_jsonld = array_shift( $jsonld ); |
|
| 49 | - $post_jsonld_id = array_key_exists( '@id', $post_jsonld ) ? $post_jsonld['@id'] : false; |
|
| 48 | + $post_jsonld = array_shift($jsonld); |
|
| 49 | + $post_jsonld_id = array_key_exists('@id', $post_jsonld) ? $post_jsonld['@id'] : false; |
|
| 50 | 50 | |
| 51 | - if ( ! $post_jsonld_id ) { |
|
| 51 | + if ( ! $post_jsonld_id) { |
|
| 52 | 52 | return $jsonld; |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | - foreach ( $jsonld as $key => $value ) { |
|
| 56 | - if ( ! array_key_exists( '@type', $value ) ) { |
|
| 55 | + foreach ($jsonld as $key => $value) { |
|
| 56 | + if ( ! array_key_exists('@type', $value)) { |
|
| 57 | 57 | continue; |
| 58 | 58 | } |
| 59 | 59 | $type = $value['@type']; |
| 60 | - if ( $type === 'Recipe' ) { |
|
| 60 | + if ($type === 'Recipe') { |
|
| 61 | 61 | $value['isPartOf'] = array( |
| 62 | 62 | '@id' => $post_jsonld_id |
| 63 | 63 | ); |
| 64 | - $jsonld[ $key ] = $value; |
|
| 64 | + $jsonld[$key] = $value; |
|
| 65 | 65 | } |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | // Add back the post jsonld to first of array. |
| 69 | - array_unshift( $jsonld, $post_jsonld ); |
|
| 69 | + array_unshift($jsonld, $post_jsonld); |
|
| 70 | 70 | |
| 71 | 71 | return $jsonld; |
| 72 | 72 | } |
@@ -8,139 +8,139 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class Wordlift_Remote_Image_Service { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * Save the image with the specified URL locally. |
|
| 13 | - * |
|
| 14 | - * @param string $url The image remote URL. |
|
| 15 | - * |
|
| 16 | - * @return array|false|WP_Error An array with information about the saved image (*path*: the local path to the image, *url*: the local |
|
| 17 | - * url, *content_type*: the image content type) or false on error. |
|
| 18 | - * @since 3.18.0 |
|
| 19 | - * @since 3.23.4 the function may return a WP_Error. |
|
| 20 | - */ |
|
| 21 | - public static function save_from_url( $url ) { |
|
| 22 | - |
|
| 23 | - // Required for REST API calls |
|
| 24 | - if ( ! function_exists( 'WP_Filesystem' ) ) { |
|
| 25 | - require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - // Load `WP_Filesystem`. |
|
| 29 | - add_filter( 'filesystem_method', 'Wordlift_Remote_Image_Service::_return_direct' ); |
|
| 30 | - WP_Filesystem(); |
|
| 31 | - global $wp_filesystem; |
|
| 32 | - |
|
| 33 | - // Parse the url. |
|
| 34 | - $parts = wp_parse_url( $url ); |
|
| 35 | - |
|
| 36 | - // Get the bare filename (filename w/o the extension). |
|
| 37 | - $basename = str_replace( DIRECTORY_SEPARATOR, '_', rawurldecode( |
|
| 38 | - pathinfo( $parts['path'], PATHINFO_FILENAME ) |
|
| 39 | - ) ); |
|
| 40 | - |
|
| 41 | - // Get the base dir. |
|
| 42 | - $wp_upload_dir = wp_upload_dir(); |
|
| 43 | - |
|
| 44 | - // Set the upload directory and URL. |
|
| 45 | - $upload_dir = $wp_upload_dir['basedir'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 46 | - $upload_url = $wp_upload_dir['baseurl'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 47 | - |
|
| 48 | - // Get the full path to the local filename. |
|
| 49 | - $image_full_path = $upload_dir . '/' . $basename; |
|
| 50 | - $image_full_url = $upload_url . '/' . $basename; |
|
| 51 | - |
|
| 52 | - // Create custom directory and bail on failure. |
|
| 53 | - if ( ! wp_mkdir_p( $upload_dir ) ) { |
|
| 54 | - Wordlift_Log_Service::get_logger( 'Wordlift_Remote_Image_Service' ) |
|
| 55 | - ->warn( "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
| 56 | - |
|
| 57 | - return new WP_Error( 'image_error', "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
| 58 | - }; |
|
| 59 | - |
|
| 60 | - $response = wp_remote_get( $url, array( |
|
| 61 | - 'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36' |
|
| 62 | - ) ); |
|
| 63 | - |
|
| 64 | - // Bail if the response is not set. |
|
| 65 | - if ( self::is_response_error( $response ) ) { |
|
| 66 | - Wordlift_Log_Service::get_logger( 'Wordlift_Remote_Image_Service' ) |
|
| 67 | - ->warn( "save_image_from_url : failed to fetch the response from: $url\nThe response was:\n" . var_export( $response, true ) ); |
|
| 68 | - |
|
| 69 | - return new WP_Error( 'image_error', "save_image_from_url : failed to fetch the response from: $url\nThe response was:\n" . var_export( $response, true ) ); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - // Get the content type of response. |
|
| 73 | - $content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
|
| 74 | - |
|
| 75 | - // Get the file extension. |
|
| 76 | - $extension = self::get_extension_from_content_type( $content_type ); |
|
| 77 | - |
|
| 78 | - // Bail if the content type is not supported. |
|
| 79 | - if ( empty( $extension ) ) { |
|
| 80 | - return new WP_Error( 'image_error', "Unsupported content type [ $content_type ]:\n" . var_export( $response, true ) ); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - // Complete the local filename. |
|
| 84 | - $image_full_path .= $extension; |
|
| 85 | - $image_full_url .= $extension; |
|
| 86 | - |
|
| 87 | - // Store the data locally. |
|
| 88 | - $wp_filesystem->put_contents( $image_full_path, wp_remote_retrieve_body( $response ) ); |
|
| 89 | - remove_filter( 'filesystem_method', 'Wordlift_Remote_Image_Service::_return_direct' ); |
|
| 90 | - |
|
| 91 | - // Return the path. |
|
| 92 | - return array( |
|
| 93 | - 'path' => $image_full_path, |
|
| 94 | - 'url' => $image_full_url, |
|
| 95 | - 'content_type' => $content_type, |
|
| 96 | - ); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - static function _return_direct() { |
|
| 100 | - return 'direct'; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Returns the file extension using the content type. |
|
| 105 | - * |
|
| 106 | - * @param string $content_type File content type. |
|
| 107 | - * |
|
| 108 | - * @return string|bool The file extension on success and |
|
| 109 | - * false on fail or if the content type is not supported. |
|
| 110 | - * @since 3.18.0 |
|
| 111 | - * |
|
| 112 | - */ |
|
| 113 | - private static function get_extension_from_content_type( $content_type ) { |
|
| 114 | - |
|
| 115 | - // Return the extension if match. |
|
| 116 | - switch ( $content_type ) { |
|
| 117 | - case 'image/jpeg': |
|
| 118 | - case 'image/jpg': |
|
| 119 | - return '.jpg'; |
|
| 120 | - case 'image/gif': |
|
| 121 | - return '.gif'; |
|
| 122 | - case 'image/png': |
|
| 123 | - return '.png'; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - // Otherwise return false. |
|
| 127 | - return false; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Checks whether a response is an error. |
|
| 132 | - * |
|
| 133 | - * @param array|WP_Error $response The response. |
|
| 134 | - * |
|
| 135 | - * @return bool True if the response is an error, otherwise false. |
|
| 136 | - * @since 3.23.4 |
|
| 137 | - */ |
|
| 138 | - private static function is_response_error( $response ) { |
|
| 139 | - |
|
| 140 | - return ( is_wp_error( $response ) |
|
| 141 | - || 200 !== (int) $response['response']['code'] |
|
| 142 | - || ! isset( $response['body'] ) |
|
| 143 | - ); |
|
| 144 | - } |
|
| 11 | + /** |
|
| 12 | + * Save the image with the specified URL locally. |
|
| 13 | + * |
|
| 14 | + * @param string $url The image remote URL. |
|
| 15 | + * |
|
| 16 | + * @return array|false|WP_Error An array with information about the saved image (*path*: the local path to the image, *url*: the local |
|
| 17 | + * url, *content_type*: the image content type) or false on error. |
|
| 18 | + * @since 3.18.0 |
|
| 19 | + * @since 3.23.4 the function may return a WP_Error. |
|
| 20 | + */ |
|
| 21 | + public static function save_from_url( $url ) { |
|
| 22 | + |
|
| 23 | + // Required for REST API calls |
|
| 24 | + if ( ! function_exists( 'WP_Filesystem' ) ) { |
|
| 25 | + require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + // Load `WP_Filesystem`. |
|
| 29 | + add_filter( 'filesystem_method', 'Wordlift_Remote_Image_Service::_return_direct' ); |
|
| 30 | + WP_Filesystem(); |
|
| 31 | + global $wp_filesystem; |
|
| 32 | + |
|
| 33 | + // Parse the url. |
|
| 34 | + $parts = wp_parse_url( $url ); |
|
| 35 | + |
|
| 36 | + // Get the bare filename (filename w/o the extension). |
|
| 37 | + $basename = str_replace( DIRECTORY_SEPARATOR, '_', rawurldecode( |
|
| 38 | + pathinfo( $parts['path'], PATHINFO_FILENAME ) |
|
| 39 | + ) ); |
|
| 40 | + |
|
| 41 | + // Get the base dir. |
|
| 42 | + $wp_upload_dir = wp_upload_dir(); |
|
| 43 | + |
|
| 44 | + // Set the upload directory and URL. |
|
| 45 | + $upload_dir = $wp_upload_dir['basedir'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 46 | + $upload_url = $wp_upload_dir['baseurl'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 47 | + |
|
| 48 | + // Get the full path to the local filename. |
|
| 49 | + $image_full_path = $upload_dir . '/' . $basename; |
|
| 50 | + $image_full_url = $upload_url . '/' . $basename; |
|
| 51 | + |
|
| 52 | + // Create custom directory and bail on failure. |
|
| 53 | + if ( ! wp_mkdir_p( $upload_dir ) ) { |
|
| 54 | + Wordlift_Log_Service::get_logger( 'Wordlift_Remote_Image_Service' ) |
|
| 55 | + ->warn( "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
| 56 | + |
|
| 57 | + return new WP_Error( 'image_error', "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
| 58 | + }; |
|
| 59 | + |
|
| 60 | + $response = wp_remote_get( $url, array( |
|
| 61 | + 'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36' |
|
| 62 | + ) ); |
|
| 63 | + |
|
| 64 | + // Bail if the response is not set. |
|
| 65 | + if ( self::is_response_error( $response ) ) { |
|
| 66 | + Wordlift_Log_Service::get_logger( 'Wordlift_Remote_Image_Service' ) |
|
| 67 | + ->warn( "save_image_from_url : failed to fetch the response from: $url\nThe response was:\n" . var_export( $response, true ) ); |
|
| 68 | + |
|
| 69 | + return new WP_Error( 'image_error', "save_image_from_url : failed to fetch the response from: $url\nThe response was:\n" . var_export( $response, true ) ); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + // Get the content type of response. |
|
| 73 | + $content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
|
| 74 | + |
|
| 75 | + // Get the file extension. |
|
| 76 | + $extension = self::get_extension_from_content_type( $content_type ); |
|
| 77 | + |
|
| 78 | + // Bail if the content type is not supported. |
|
| 79 | + if ( empty( $extension ) ) { |
|
| 80 | + return new WP_Error( 'image_error', "Unsupported content type [ $content_type ]:\n" . var_export( $response, true ) ); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + // Complete the local filename. |
|
| 84 | + $image_full_path .= $extension; |
|
| 85 | + $image_full_url .= $extension; |
|
| 86 | + |
|
| 87 | + // Store the data locally. |
|
| 88 | + $wp_filesystem->put_contents( $image_full_path, wp_remote_retrieve_body( $response ) ); |
|
| 89 | + remove_filter( 'filesystem_method', 'Wordlift_Remote_Image_Service::_return_direct' ); |
|
| 90 | + |
|
| 91 | + // Return the path. |
|
| 92 | + return array( |
|
| 93 | + 'path' => $image_full_path, |
|
| 94 | + 'url' => $image_full_url, |
|
| 95 | + 'content_type' => $content_type, |
|
| 96 | + ); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + static function _return_direct() { |
|
| 100 | + return 'direct'; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Returns the file extension using the content type. |
|
| 105 | + * |
|
| 106 | + * @param string $content_type File content type. |
|
| 107 | + * |
|
| 108 | + * @return string|bool The file extension on success and |
|
| 109 | + * false on fail or if the content type is not supported. |
|
| 110 | + * @since 3.18.0 |
|
| 111 | + * |
|
| 112 | + */ |
|
| 113 | + private static function get_extension_from_content_type( $content_type ) { |
|
| 114 | + |
|
| 115 | + // Return the extension if match. |
|
| 116 | + switch ( $content_type ) { |
|
| 117 | + case 'image/jpeg': |
|
| 118 | + case 'image/jpg': |
|
| 119 | + return '.jpg'; |
|
| 120 | + case 'image/gif': |
|
| 121 | + return '.gif'; |
|
| 122 | + case 'image/png': |
|
| 123 | + return '.png'; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + // Otherwise return false. |
|
| 127 | + return false; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Checks whether a response is an error. |
|
| 132 | + * |
|
| 133 | + * @param array|WP_Error $response The response. |
|
| 134 | + * |
|
| 135 | + * @return bool True if the response is an error, otherwise false. |
|
| 136 | + * @since 3.23.4 |
|
| 137 | + */ |
|
| 138 | + private static function is_response_error( $response ) { |
|
| 139 | + |
|
| 140 | + return ( is_wp_error( $response ) |
|
| 141 | + || 200 !== (int) $response['response']['code'] |
|
| 142 | + || ! isset( $response['body'] ) |
|
| 143 | + ); |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | 146 | } |
@@ -18,66 +18,66 @@ discard block |
||
| 18 | 18 | * @since 3.18.0 |
| 19 | 19 | * @since 3.23.4 the function may return a WP_Error. |
| 20 | 20 | */ |
| 21 | - public static function save_from_url( $url ) { |
|
| 21 | + public static function save_from_url($url) { |
|
| 22 | 22 | |
| 23 | 23 | // Required for REST API calls |
| 24 | - if ( ! function_exists( 'WP_Filesystem' ) ) { |
|
| 25 | - require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
|
| 24 | + if ( ! function_exists('WP_Filesystem')) { |
|
| 25 | + require_once(ABSPATH.'wp-admin/includes/file.php'); |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | // Load `WP_Filesystem`. |
| 29 | - add_filter( 'filesystem_method', 'Wordlift_Remote_Image_Service::_return_direct' ); |
|
| 29 | + add_filter('filesystem_method', 'Wordlift_Remote_Image_Service::_return_direct'); |
|
| 30 | 30 | WP_Filesystem(); |
| 31 | 31 | global $wp_filesystem; |
| 32 | 32 | |
| 33 | 33 | // Parse the url. |
| 34 | - $parts = wp_parse_url( $url ); |
|
| 34 | + $parts = wp_parse_url($url); |
|
| 35 | 35 | |
| 36 | 36 | // Get the bare filename (filename w/o the extension). |
| 37 | - $basename = str_replace( DIRECTORY_SEPARATOR, '_', rawurldecode( |
|
| 38 | - pathinfo( $parts['path'], PATHINFO_FILENAME ) |
|
| 39 | - ) ); |
|
| 37 | + $basename = str_replace(DIRECTORY_SEPARATOR, '_', rawurldecode( |
|
| 38 | + pathinfo($parts['path'], PATHINFO_FILENAME) |
|
| 39 | + )); |
|
| 40 | 40 | |
| 41 | 41 | // Get the base dir. |
| 42 | 42 | $wp_upload_dir = wp_upload_dir(); |
| 43 | 43 | |
| 44 | 44 | // Set the upload directory and URL. |
| 45 | - $upload_dir = $wp_upload_dir['basedir'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 46 | - $upload_url = $wp_upload_dir['baseurl'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 45 | + $upload_dir = $wp_upload_dir['basedir'].'/wl'.$wp_upload_dir['subdir']; |
|
| 46 | + $upload_url = $wp_upload_dir['baseurl'].'/wl'.$wp_upload_dir['subdir']; |
|
| 47 | 47 | |
| 48 | 48 | // Get the full path to the local filename. |
| 49 | - $image_full_path = $upload_dir . '/' . $basename; |
|
| 50 | - $image_full_url = $upload_url . '/' . $basename; |
|
| 49 | + $image_full_path = $upload_dir.'/'.$basename; |
|
| 50 | + $image_full_url = $upload_url.'/'.$basename; |
|
| 51 | 51 | |
| 52 | 52 | // Create custom directory and bail on failure. |
| 53 | - if ( ! wp_mkdir_p( $upload_dir ) ) { |
|
| 54 | - Wordlift_Log_Service::get_logger( 'Wordlift_Remote_Image_Service' ) |
|
| 55 | - ->warn( "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
| 53 | + if ( ! wp_mkdir_p($upload_dir)) { |
|
| 54 | + Wordlift_Log_Service::get_logger('Wordlift_Remote_Image_Service') |
|
| 55 | + ->warn("save_image_from_url : failed creating upload dir $upload_dir \n"); |
|
| 56 | 56 | |
| 57 | - return new WP_Error( 'image_error', "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
| 57 | + return new WP_Error('image_error', "save_image_from_url : failed creating upload dir $upload_dir \n"); |
|
| 58 | 58 | }; |
| 59 | 59 | |
| 60 | - $response = wp_remote_get( $url, array( |
|
| 60 | + $response = wp_remote_get($url, array( |
|
| 61 | 61 | 'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36' |
| 62 | - ) ); |
|
| 62 | + )); |
|
| 63 | 63 | |
| 64 | 64 | // Bail if the response is not set. |
| 65 | - if ( self::is_response_error( $response ) ) { |
|
| 66 | - Wordlift_Log_Service::get_logger( 'Wordlift_Remote_Image_Service' ) |
|
| 67 | - ->warn( "save_image_from_url : failed to fetch the response from: $url\nThe response was:\n" . var_export( $response, true ) ); |
|
| 65 | + if (self::is_response_error($response)) { |
|
| 66 | + Wordlift_Log_Service::get_logger('Wordlift_Remote_Image_Service') |
|
| 67 | + ->warn("save_image_from_url : failed to fetch the response from: $url\nThe response was:\n".var_export($response, true)); |
|
| 68 | 68 | |
| 69 | - return new WP_Error( 'image_error', "save_image_from_url : failed to fetch the response from: $url\nThe response was:\n" . var_export( $response, true ) ); |
|
| 69 | + return new WP_Error('image_error', "save_image_from_url : failed to fetch the response from: $url\nThe response was:\n".var_export($response, true)); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | // Get the content type of response. |
| 73 | - $content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
|
| 73 | + $content_type = wp_remote_retrieve_header($response, 'content-type'); |
|
| 74 | 74 | |
| 75 | 75 | // Get the file extension. |
| 76 | - $extension = self::get_extension_from_content_type( $content_type ); |
|
| 76 | + $extension = self::get_extension_from_content_type($content_type); |
|
| 77 | 77 | |
| 78 | 78 | // Bail if the content type is not supported. |
| 79 | - if ( empty( $extension ) ) { |
|
| 80 | - return new WP_Error( 'image_error', "Unsupported content type [ $content_type ]:\n" . var_export( $response, true ) ); |
|
| 79 | + if (empty($extension)) { |
|
| 80 | + return new WP_Error('image_error', "Unsupported content type [ $content_type ]:\n".var_export($response, true)); |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | // Complete the local filename. |
@@ -85,8 +85,8 @@ discard block |
||
| 85 | 85 | $image_full_url .= $extension; |
| 86 | 86 | |
| 87 | 87 | // Store the data locally. |
| 88 | - $wp_filesystem->put_contents( $image_full_path, wp_remote_retrieve_body( $response ) ); |
|
| 89 | - remove_filter( 'filesystem_method', 'Wordlift_Remote_Image_Service::_return_direct' ); |
|
| 88 | + $wp_filesystem->put_contents($image_full_path, wp_remote_retrieve_body($response)); |
|
| 89 | + remove_filter('filesystem_method', 'Wordlift_Remote_Image_Service::_return_direct'); |
|
| 90 | 90 | |
| 91 | 91 | // Return the path. |
| 92 | 92 | return array( |
@@ -110,10 +110,10 @@ discard block |
||
| 110 | 110 | * @since 3.18.0 |
| 111 | 111 | * |
| 112 | 112 | */ |
| 113 | - private static function get_extension_from_content_type( $content_type ) { |
|
| 113 | + private static function get_extension_from_content_type($content_type) { |
|
| 114 | 114 | |
| 115 | 115 | // Return the extension if match. |
| 116 | - switch ( $content_type ) { |
|
| 116 | + switch ($content_type) { |
|
| 117 | 117 | case 'image/jpeg': |
| 118 | 118 | case 'image/jpg': |
| 119 | 119 | return '.jpg'; |
@@ -135,11 +135,11 @@ discard block |
||
| 135 | 135 | * @return bool True if the response is an error, otherwise false. |
| 136 | 136 | * @since 3.23.4 |
| 137 | 137 | */ |
| 138 | - private static function is_response_error( $response ) { |
|
| 138 | + private static function is_response_error($response) { |
|
| 139 | 139 | |
| 140 | - return ( is_wp_error( $response ) |
|
| 140 | + return (is_wp_error($response) |
|
| 141 | 141 | || 200 !== (int) $response['response']['code'] |
| 142 | - || ! isset( $response['body'] ) |
|
| 142 | + || ! isset($response['body']) |
|
| 143 | 143 | ); |
| 144 | 144 | } |
| 145 | 145 | |
@@ -19,27 +19,27 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | interface Rule_Validator { |
| 21 | 21 | |
| 22 | - const IS_EQUAL_TO = '==='; |
|
| 23 | - const IS_NOT_EQUAL_TO = '!=='; |
|
| 22 | + const IS_EQUAL_TO = '==='; |
|
| 23 | + const IS_NOT_EQUAL_TO = '!=='; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Get the validator label. |
|
| 27 | - * |
|
| 28 | - * @return string The validator label. |
|
| 29 | - */ |
|
| 30 | - public function get_label(); |
|
| 25 | + /** |
|
| 26 | + * Get the validator label. |
|
| 27 | + * |
|
| 28 | + * @return string The validator label. |
|
| 29 | + */ |
|
| 30 | + public function get_label(); |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Test whether a post passes a validation. |
|
| 34 | - * |
|
| 35 | - * @param int $identifier The post id or term id. |
|
| 36 | - * @param string $operator The operator. |
|
| 37 | - * @param string $operand_1 The first operand. |
|
| 38 | - * @param string $operand_2 The second operand. |
|
| 39 | - * @param string $type The type is either post or term. |
|
| 40 | - * |
|
| 41 | - * @return bool Whether the post passes or not the validation. |
|
| 42 | - */ |
|
| 43 | - public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ); |
|
| 32 | + /** |
|
| 33 | + * Test whether a post passes a validation. |
|
| 34 | + * |
|
| 35 | + * @param int $identifier The post id or term id. |
|
| 36 | + * @param string $operator The operator. |
|
| 37 | + * @param string $operand_1 The first operand. |
|
| 38 | + * @param string $operand_2 The second operand. |
|
| 39 | + * @param string $type The type is either post or term. |
|
| 40 | + * |
|
| 41 | + * @return bool Whether the post passes or not the validation. |
|
| 42 | + */ |
|
| 43 | + public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ); |
|
| 44 | 44 | |
| 45 | 45 | } |
@@ -40,6 +40,6 @@ |
||
| 40 | 40 | * |
| 41 | 41 | * @return bool Whether the post passes or not the validation. |
| 42 | 42 | */ |
| 43 | - public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ); |
|
| 43 | + public function is_valid($identifier, $operator, $operand_1, $operand_2, $type); |
|
| 44 | 44 | |
| 45 | 45 | } |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | * |
| 32 | 32 | * @param Rule_Validators_Registry $rule_validators_registry |
| 33 | 33 | */ |
| 34 | - public function __construct( $rule_validators_registry ) { |
|
| 34 | + public function __construct($rule_validators_registry) { |
|
| 35 | 35 | |
| 36 | 36 | $this->rule_validators_registry = $rule_validators_registry; |
| 37 | 37 | |
@@ -46,18 +46,18 @@ discard block |
||
| 46 | 46 | * |
| 47 | 47 | * @return bool Whether the post passes at least one rule group. |
| 48 | 48 | */ |
| 49 | - public function is_valid( $identifier, $rule_groups, $type ) { |
|
| 49 | + public function is_valid($identifier, $rule_groups, $type) { |
|
| 50 | 50 | |
| 51 | 51 | // Validate each group. Return true as soon as one group is validated (all rules). |
| 52 | - foreach ( (array) $rule_groups as $rule_group ) { |
|
| 53 | - foreach ( $rule_group['rules'] as $rule ) { |
|
| 52 | + foreach ((array) $rule_groups as $rule_group) { |
|
| 53 | + foreach ($rule_group['rules'] as $rule) { |
|
| 54 | 54 | $rule_field_one = $rule['rule_field_one']; |
| 55 | 55 | $rule_logic_field = $rule['rule_logic_field']; |
| 56 | 56 | $rule_field_two = $rule['rule_field_two']; |
| 57 | 57 | |
| 58 | - $rule_validator = $this->rule_validators_registry->get_rule_validator( $rule_field_one ); |
|
| 58 | + $rule_validator = $this->rule_validators_registry->get_rule_validator($rule_field_one); |
|
| 59 | 59 | // Skip to the next Rule Group if a rule isn't valid. |
| 60 | - if ( ! $rule_validator->is_valid( $identifier, $rule_logic_field, $rule_field_one, $rule_field_two, $type ) ) { |
|
| 60 | + if ( ! $rule_validator->is_valid($identifier, $rule_logic_field, $rule_field_one, $rule_field_two, $type)) { |
|
| 61 | 61 | continue 2; |
| 62 | 62 | } |
| 63 | 63 | |
@@ -19,55 +19,55 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class Rule_Groups_Validator { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * The {@link Rule_Validators_Registry} instance. |
|
| 24 | - * |
|
| 25 | - * @var Rule_Validators_Registry $rule_validators_registry The {@link Rule_Validators_Registry} instance. |
|
| 26 | - */ |
|
| 27 | - private $rule_validators_registry; |
|
| 22 | + /** |
|
| 23 | + * The {@link Rule_Validators_Registry} instance. |
|
| 24 | + * |
|
| 25 | + * @var Rule_Validators_Registry $rule_validators_registry The {@link Rule_Validators_Registry} instance. |
|
| 26 | + */ |
|
| 27 | + private $rule_validators_registry; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Rule_Groups_Validator constructor. |
|
| 31 | - * |
|
| 32 | - * @param Rule_Validators_Registry $rule_validators_registry |
|
| 33 | - */ |
|
| 34 | - public function __construct( $rule_validators_registry ) { |
|
| 29 | + /** |
|
| 30 | + * Rule_Groups_Validator constructor. |
|
| 31 | + * |
|
| 32 | + * @param Rule_Validators_Registry $rule_validators_registry |
|
| 33 | + */ |
|
| 34 | + public function __construct( $rule_validators_registry ) { |
|
| 35 | 35 | |
| 36 | - $this->rule_validators_registry = $rule_validators_registry; |
|
| 36 | + $this->rule_validators_registry = $rule_validators_registry; |
|
| 37 | 37 | |
| 38 | - } |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Check whether the specified post passes at least one group of rules. |
|
| 42 | - * |
|
| 43 | - * @param int $identifier The post id or term id. |
|
| 44 | - * @param array $rule_groups An array of rules' groups. |
|
| 45 | - * @param $type string Post or term |
|
| 46 | - * |
|
| 47 | - * @return bool Whether the post passes at least one rule group. |
|
| 48 | - */ |
|
| 49 | - public function is_valid( $identifier, $rule_groups, $type ) { |
|
| 40 | + /** |
|
| 41 | + * Check whether the specified post passes at least one group of rules. |
|
| 42 | + * |
|
| 43 | + * @param int $identifier The post id or term id. |
|
| 44 | + * @param array $rule_groups An array of rules' groups. |
|
| 45 | + * @param $type string Post or term |
|
| 46 | + * |
|
| 47 | + * @return bool Whether the post passes at least one rule group. |
|
| 48 | + */ |
|
| 49 | + public function is_valid( $identifier, $rule_groups, $type ) { |
|
| 50 | 50 | |
| 51 | - // Validate each group. Return true as soon as one group is validated (all rules). |
|
| 52 | - foreach ( (array) $rule_groups as $rule_group ) { |
|
| 53 | - foreach ( $rule_group['rules'] as $rule ) { |
|
| 54 | - $rule_field_one = $rule['rule_field_one']; |
|
| 55 | - $rule_logic_field = $rule['rule_logic_field']; |
|
| 56 | - $rule_field_two = $rule['rule_field_two']; |
|
| 51 | + // Validate each group. Return true as soon as one group is validated (all rules). |
|
| 52 | + foreach ( (array) $rule_groups as $rule_group ) { |
|
| 53 | + foreach ( $rule_group['rules'] as $rule ) { |
|
| 54 | + $rule_field_one = $rule['rule_field_one']; |
|
| 55 | + $rule_logic_field = $rule['rule_logic_field']; |
|
| 56 | + $rule_field_two = $rule['rule_field_two']; |
|
| 57 | 57 | |
| 58 | - $rule_validator = $this->rule_validators_registry->get_rule_validator( $rule_field_one ); |
|
| 59 | - // Skip to the next Rule Group if a rule isn't valid. |
|
| 60 | - if ( ! $rule_validator->is_valid( $identifier, $rule_logic_field, $rule_field_one, $rule_field_two, $type ) ) { |
|
| 61 | - continue 2; |
|
| 62 | - } |
|
| 58 | + $rule_validator = $this->rule_validators_registry->get_rule_validator( $rule_field_one ); |
|
| 59 | + // Skip to the next Rule Group if a rule isn't valid. |
|
| 60 | + if ( ! $rule_validator->is_valid( $identifier, $rule_logic_field, $rule_field_one, $rule_field_two, $type ) ) { |
|
| 61 | + continue 2; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - } |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - // If we got here it means that all the rules have been validated (or the rules' group has no rules). |
|
| 67 | - return true; |
|
| 68 | - } |
|
| 66 | + // If we got here it means that all the rules have been validated (or the rules' group has no rules). |
|
| 67 | + return true; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - return false; |
|
| 71 | - } |
|
| 70 | + return false; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | 73 | } |