@@ -16,110 +16,110 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Related_Entities_Cloud_Shortcode extends Wordlift_Shortcode { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * {@inheritdoc} |
|
| 21 | - */ |
|
| 22 | - const SHORTCODE = 'wl_cloud'; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * The {@link Wordlift_Relation_Service} instance. |
|
| 26 | - * |
|
| 27 | - * @since 3.15.0 |
|
| 28 | - * @access private |
|
| 29 | - * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 30 | - */ |
|
| 31 | - private $relation_service; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Create a {@link Wordlift_Related_Entities_Cloud_Shortcode} instance. |
|
| 35 | - * |
|
| 36 | - * @since 3.15.0 |
|
| 37 | - * |
|
| 38 | - * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 39 | - */ |
|
| 40 | - public function __construct( $relation_service ) { |
|
| 41 | - parent::__construct(); |
|
| 42 | - |
|
| 43 | - $this->relation_service = $relation_service; |
|
| 44 | - |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * {@inheritdoc} |
|
| 49 | - */ |
|
| 50 | - public function render( $atts ) { |
|
| 51 | - |
|
| 52 | - $tags = $this->get_related_entities_tags(); |
|
| 53 | - |
|
| 54 | - // Bail out if there are no associated entities. |
|
| 55 | - if ( empty( $tags ) ) { |
|
| 56 | - return ''; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /* |
|
| 19 | + /** |
|
| 20 | + * {@inheritdoc} |
|
| 21 | + */ |
|
| 22 | + const SHORTCODE = 'wl_cloud'; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * The {@link Wordlift_Relation_Service} instance. |
|
| 26 | + * |
|
| 27 | + * @since 3.15.0 |
|
| 28 | + * @access private |
|
| 29 | + * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 30 | + */ |
|
| 31 | + private $relation_service; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Create a {@link Wordlift_Related_Entities_Cloud_Shortcode} instance. |
|
| 35 | + * |
|
| 36 | + * @since 3.15.0 |
|
| 37 | + * |
|
| 38 | + * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 39 | + */ |
|
| 40 | + public function __construct( $relation_service ) { |
|
| 41 | + parent::__construct(); |
|
| 42 | + |
|
| 43 | + $this->relation_service = $relation_service; |
|
| 44 | + |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * {@inheritdoc} |
|
| 49 | + */ |
|
| 50 | + public function render( $atts ) { |
|
| 51 | + |
|
| 52 | + $tags = $this->get_related_entities_tags(); |
|
| 53 | + |
|
| 54 | + // Bail out if there are no associated entities. |
|
| 55 | + if ( empty( $tags ) ) { |
|
| 56 | + return ''; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /* |
|
| 60 | 60 | * Since the output is use in the widget as well, we need to have the |
| 61 | 61 | * same class as the core tagcloud widget, to easily inherit its styling. |
| 62 | 62 | */ |
| 63 | 63 | |
| 64 | - return '<div class="tagcloud wl-related-entities-cloud">' . |
|
| 65 | - wp_generate_tag_cloud( $tags ) . |
|
| 66 | - '</div>'; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Find the related entities to the currently displayed post and |
|
| 71 | - * calculate the "tags" for them as wp_generate_tag_cloud expects to get. |
|
| 72 | - * |
|
| 73 | - * @since 3.11.0 |
|
| 74 | - * |
|
| 75 | - * @return array Array of tags. Empty array in case we re not in a context |
|
| 76 | - * of a post, or it has no related entities. |
|
| 77 | - */ |
|
| 78 | - public function get_related_entities_tags() { |
|
| 79 | - |
|
| 80 | - // Define the supported types list. |
|
| 81 | - $supported_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
| 82 | - |
|
| 83 | - // Show nothing if not on a post or entity page. |
|
| 84 | - if ( ! is_singular( $supported_types ) ) { |
|
| 85 | - return array(); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - // Get the IDs of entities related to current post. |
|
| 89 | - $related_entities = wl_core_get_related_entity_ids( get_the_ID(), array( 'status' => 'publish' ) ); |
|
| 90 | - |
|
| 91 | - // Bail out if there are no associated entities. |
|
| 92 | - if ( empty( $related_entities ) ) { |
|
| 93 | - return array(); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /* |
|
| 64 | + return '<div class="tagcloud wl-related-entities-cloud">' . |
|
| 65 | + wp_generate_tag_cloud( $tags ) . |
|
| 66 | + '</div>'; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Find the related entities to the currently displayed post and |
|
| 71 | + * calculate the "tags" for them as wp_generate_tag_cloud expects to get. |
|
| 72 | + * |
|
| 73 | + * @since 3.11.0 |
|
| 74 | + * |
|
| 75 | + * @return array Array of tags. Empty array in case we re not in a context |
|
| 76 | + * of a post, or it has no related entities. |
|
| 77 | + */ |
|
| 78 | + public function get_related_entities_tags() { |
|
| 79 | + |
|
| 80 | + // Define the supported types list. |
|
| 81 | + $supported_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
| 82 | + |
|
| 83 | + // Show nothing if not on a post or entity page. |
|
| 84 | + if ( ! is_singular( $supported_types ) ) { |
|
| 85 | + return array(); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + // Get the IDs of entities related to current post. |
|
| 89 | + $related_entities = wl_core_get_related_entity_ids( get_the_ID(), array( 'status' => 'publish' ) ); |
|
| 90 | + |
|
| 91 | + // Bail out if there are no associated entities. |
|
| 92 | + if ( empty( $related_entities ) ) { |
|
| 93 | + return array(); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /* |
|
| 97 | 97 | * Create an array of "tags" to feed to wp_generate_tag_cloud. |
| 98 | 98 | * Use the number of posts and entities connected to the entity as a weight. |
| 99 | 99 | */ |
| 100 | - $tags = array(); |
|
| 101 | - |
|
| 102 | - foreach ( array_unique( $related_entities ) as $entity_id ) { |
|
| 103 | - |
|
| 104 | - $connected_entities = count( wl_core_get_related_entity_ids( $entity_id, array( 'status' => 'publish' ) ) ); |
|
| 105 | - $connected_posts = count( $this->relation_service->get_article_subjects( $entity_id, '*', null, 'publish' ) ); |
|
| 106 | - |
|
| 107 | - $tags[] = (object) array( |
|
| 108 | - 'id' => $entity_id, |
|
| 109 | - // Used to give a unique class on the tag. |
|
| 110 | - 'name' => get_the_title( $entity_id ), |
|
| 111 | - // The text of the tag. |
|
| 112 | - 'slug' => get_the_title( $entity_id ), |
|
| 113 | - // Required but not seem to be relevant |
|
| 114 | - 'link' => get_permalink( $entity_id ), |
|
| 115 | - // the url the tag links to. |
|
| 116 | - 'count' => $connected_entities + $connected_posts, |
|
| 117 | - // The weight. |
|
| 118 | - ); |
|
| 119 | - |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - return $tags; |
|
| 123 | - } |
|
| 100 | + $tags = array(); |
|
| 101 | + |
|
| 102 | + foreach ( array_unique( $related_entities ) as $entity_id ) { |
|
| 103 | + |
|
| 104 | + $connected_entities = count( wl_core_get_related_entity_ids( $entity_id, array( 'status' => 'publish' ) ) ); |
|
| 105 | + $connected_posts = count( $this->relation_service->get_article_subjects( $entity_id, '*', null, 'publish' ) ); |
|
| 106 | + |
|
| 107 | + $tags[] = (object) array( |
|
| 108 | + 'id' => $entity_id, |
|
| 109 | + // Used to give a unique class on the tag. |
|
| 110 | + 'name' => get_the_title( $entity_id ), |
|
| 111 | + // The text of the tag. |
|
| 112 | + 'slug' => get_the_title( $entity_id ), |
|
| 113 | + // Required but not seem to be relevant |
|
| 114 | + 'link' => get_permalink( $entity_id ), |
|
| 115 | + // the url the tag links to. |
|
| 116 | + 'count' => $connected_entities + $connected_posts, |
|
| 117 | + // The weight. |
|
| 118 | + ); |
|
| 119 | + |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + return $tags; |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | 125 | } |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | * |
| 38 | 38 | * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
| 39 | 39 | */ |
| 40 | - public function __construct( $relation_service ) { |
|
| 40 | + public function __construct($relation_service) { |
|
| 41 | 41 | parent::__construct(); |
| 42 | 42 | |
| 43 | 43 | $this->relation_service = $relation_service; |
@@ -47,12 +47,12 @@ discard block |
||
| 47 | 47 | /** |
| 48 | 48 | * {@inheritdoc} |
| 49 | 49 | */ |
| 50 | - public function render( $atts ) { |
|
| 50 | + public function render($atts) { |
|
| 51 | 51 | |
| 52 | 52 | $tags = $this->get_related_entities_tags(); |
| 53 | 53 | |
| 54 | 54 | // Bail out if there are no associated entities. |
| 55 | - if ( empty( $tags ) ) { |
|
| 55 | + if (empty($tags)) { |
|
| 56 | 56 | return ''; |
| 57 | 57 | } |
| 58 | 58 | |
@@ -61,8 +61,8 @@ discard block |
||
| 61 | 61 | * same class as the core tagcloud widget, to easily inherit its styling. |
| 62 | 62 | */ |
| 63 | 63 | |
| 64 | - return '<div class="tagcloud wl-related-entities-cloud">' . |
|
| 65 | - wp_generate_tag_cloud( $tags ) . |
|
| 64 | + return '<div class="tagcloud wl-related-entities-cloud">'. |
|
| 65 | + wp_generate_tag_cloud($tags). |
|
| 66 | 66 | '</div>'; |
| 67 | 67 | } |
| 68 | 68 | |
@@ -81,15 +81,15 @@ discard block |
||
| 81 | 81 | $supported_types = Wordlift_Entity_Service::valid_entity_post_types(); |
| 82 | 82 | |
| 83 | 83 | // Show nothing if not on a post or entity page. |
| 84 | - if ( ! is_singular( $supported_types ) ) { |
|
| 84 | + if ( ! is_singular($supported_types)) { |
|
| 85 | 85 | return array(); |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | // Get the IDs of entities related to current post. |
| 89 | - $related_entities = wl_core_get_related_entity_ids( get_the_ID(), array( 'status' => 'publish' ) ); |
|
| 89 | + $related_entities = wl_core_get_related_entity_ids(get_the_ID(), array('status' => 'publish')); |
|
| 90 | 90 | |
| 91 | 91 | // Bail out if there are no associated entities. |
| 92 | - if ( empty( $related_entities ) ) { |
|
| 92 | + if (empty($related_entities)) { |
|
| 93 | 93 | return array(); |
| 94 | 94 | } |
| 95 | 95 | |
@@ -99,19 +99,19 @@ discard block |
||
| 99 | 99 | */ |
| 100 | 100 | $tags = array(); |
| 101 | 101 | |
| 102 | - foreach ( array_unique( $related_entities ) as $entity_id ) { |
|
| 102 | + foreach (array_unique($related_entities) as $entity_id) { |
|
| 103 | 103 | |
| 104 | - $connected_entities = count( wl_core_get_related_entity_ids( $entity_id, array( 'status' => 'publish' ) ) ); |
|
| 105 | - $connected_posts = count( $this->relation_service->get_article_subjects( $entity_id, '*', null, 'publish' ) ); |
|
| 104 | + $connected_entities = count(wl_core_get_related_entity_ids($entity_id, array('status' => 'publish'))); |
|
| 105 | + $connected_posts = count($this->relation_service->get_article_subjects($entity_id, '*', null, 'publish')); |
|
| 106 | 106 | |
| 107 | 107 | $tags[] = (object) array( |
| 108 | 108 | 'id' => $entity_id, |
| 109 | 109 | // Used to give a unique class on the tag. |
| 110 | - 'name' => get_the_title( $entity_id ), |
|
| 110 | + 'name' => get_the_title($entity_id), |
|
| 111 | 111 | // The text of the tag. |
| 112 | - 'slug' => get_the_title( $entity_id ), |
|
| 112 | + 'slug' => get_the_title($entity_id), |
|
| 113 | 113 | // Required but not seem to be relevant |
| 114 | - 'link' => get_permalink( $entity_id ), |
|
| 114 | + 'link' => get_permalink($entity_id), |
|
| 115 | 115 | // the url the tag links to. |
| 116 | 116 | 'count' => $connected_entities + $connected_posts, |
| 117 | 117 | // The weight. |
@@ -22,128 +22,128 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | class Wordlift_Public { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * The ID of this plugin. |
|
| 27 | - * |
|
| 28 | - * @since 1.0.0 |
|
| 29 | - * @access private |
|
| 30 | - * @var string $plugin_name The ID of this plugin. |
|
| 31 | - */ |
|
| 32 | - private $plugin_name; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * The version of this plugin. |
|
| 36 | - * |
|
| 37 | - * @since 1.0.0 |
|
| 38 | - * @access private |
|
| 39 | - * @var string $version The current version of this plugin. |
|
| 40 | - */ |
|
| 41 | - private $version; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Initialize the class and set its properties. |
|
| 45 | - * |
|
| 46 | - * @since 1.0.0 |
|
| 47 | - * |
|
| 48 | - * @param string $plugin_name The name of the plugin. |
|
| 49 | - * @param string $version The version of this plugin. |
|
| 50 | - */ |
|
| 51 | - public function __construct( $plugin_name, $version ) { |
|
| 52 | - |
|
| 53 | - $this->plugin_name = $plugin_name; |
|
| 54 | - $this->version = $version; |
|
| 55 | - |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Register the stylesheets for the public-facing side of the site. |
|
| 60 | - * |
|
| 61 | - * @since 1.0.0 |
|
| 62 | - */ |
|
| 63 | - public function enqueue_styles() { |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * This function is provided for demonstration purposes only. |
|
| 67 | - * |
|
| 68 | - * An instance of this class should be passed to the run() function |
|
| 69 | - * defined in Wordlift_Loader as all of the hooks are defined |
|
| 70 | - * in that particular class. |
|
| 71 | - * |
|
| 72 | - * The Wordlift_Loader will then create the relationship |
|
| 73 | - * between the defined hooks and the functions defined in this |
|
| 74 | - * class. |
|
| 75 | - */ |
|
| 76 | - |
|
| 77 | - wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-public.css', array(), $this->version, 'all' ); |
|
| 78 | - |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Register the stylesheets for the public-facing side of the site. |
|
| 83 | - * |
|
| 84 | - * @since 1.0.0 |
|
| 85 | - */ |
|
| 86 | - public function enqueue_scripts() { |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * This function is provided for demonstration purposes only. |
|
| 90 | - * |
|
| 91 | - * An instance of this class should be passed to the run() function |
|
| 92 | - * defined in Wordlift_Loader as all of the hooks are defined |
|
| 93 | - * in that particular class. |
|
| 94 | - * |
|
| 95 | - * The Wordlift_Loader will then create the relationship |
|
| 96 | - * between the defined hooks and the functions defined in this |
|
| 97 | - * class. |
|
| 98 | - */ |
|
| 99 | - |
|
| 100 | - $settings = self::get_settings(); |
|
| 101 | - |
|
| 102 | - // Note that we switched the js to be loaded in footer, since it is loading |
|
| 103 | - // the json-ld representation. |
|
| 104 | - wp_enqueue_script( $this->plugin_name, plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/bundle.js', array(), $this->version, true ); |
|
| 105 | - wp_localize_script( $this->plugin_name, 'wlSettings', $settings ); |
|
| 106 | - |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Get the settings array. |
|
| 111 | - * |
|
| 112 | - * @since 3.19.1 |
|
| 113 | - * |
|
| 114 | - * @return array An array with the settings. |
|
| 115 | - */ |
|
| 116 | - public static function get_settings() { |
|
| 117 | - |
|
| 118 | - // Prepare a settings array for client-side functions. |
|
| 119 | - $settings = array( |
|
| 120 | - 'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
|
| 121 | - 'apiUrl' => get_site_url( null, 'wl-api/' ), |
|
| 122 | - ); |
|
| 123 | - |
|
| 124 | - // If we're in a single page, then print out the post id. |
|
| 125 | - if ( is_singular() ) { |
|
| 126 | - $settings['postId'] = get_the_ID(); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - // Add flag that we are on home/blog page. |
|
| 130 | - if ( is_home() || is_front_page() ) { |
|
| 131 | - $settings['isHome'] = true; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - // By default only enable JSON-LD on supported entity pages (includes |
|
| 135 | - // `page`, `post` and `entity` by default) and on the home page. |
|
| 136 | - // |
|
| 137 | - // @see https://github.com/insideout10/wordlift-plugin/issues/733 |
|
| 138 | - $jsonld_enabled = is_home() || is_front_page() || Wordlift_Entity_Type_Service::is_valid_entity_post_type( get_post_type() ); |
|
| 139 | - |
|
| 140 | - // Add the JSON-LD enabled flag, when set to false, the JSON-lD won't |
|
| 141 | - // be loaded. |
|
| 142 | - // |
|
| 143 | - // @see https://github.com/insideout10/wordlift-plugin/issues/642. |
|
| 144 | - $settings['jsonld_enabled'] = apply_filters( 'wl_jsonld_enabled', $jsonld_enabled ); |
|
| 145 | - |
|
| 146 | - return $settings; |
|
| 147 | - } |
|
| 25 | + /** |
|
| 26 | + * The ID of this plugin. |
|
| 27 | + * |
|
| 28 | + * @since 1.0.0 |
|
| 29 | + * @access private |
|
| 30 | + * @var string $plugin_name The ID of this plugin. |
|
| 31 | + */ |
|
| 32 | + private $plugin_name; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * The version of this plugin. |
|
| 36 | + * |
|
| 37 | + * @since 1.0.0 |
|
| 38 | + * @access private |
|
| 39 | + * @var string $version The current version of this plugin. |
|
| 40 | + */ |
|
| 41 | + private $version; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Initialize the class and set its properties. |
|
| 45 | + * |
|
| 46 | + * @since 1.0.0 |
|
| 47 | + * |
|
| 48 | + * @param string $plugin_name The name of the plugin. |
|
| 49 | + * @param string $version The version of this plugin. |
|
| 50 | + */ |
|
| 51 | + public function __construct( $plugin_name, $version ) { |
|
| 52 | + |
|
| 53 | + $this->plugin_name = $plugin_name; |
|
| 54 | + $this->version = $version; |
|
| 55 | + |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Register the stylesheets for the public-facing side of the site. |
|
| 60 | + * |
|
| 61 | + * @since 1.0.0 |
|
| 62 | + */ |
|
| 63 | + public function enqueue_styles() { |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * This function is provided for demonstration purposes only. |
|
| 67 | + * |
|
| 68 | + * An instance of this class should be passed to the run() function |
|
| 69 | + * defined in Wordlift_Loader as all of the hooks are defined |
|
| 70 | + * in that particular class. |
|
| 71 | + * |
|
| 72 | + * The Wordlift_Loader will then create the relationship |
|
| 73 | + * between the defined hooks and the functions defined in this |
|
| 74 | + * class. |
|
| 75 | + */ |
|
| 76 | + |
|
| 77 | + wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-public.css', array(), $this->version, 'all' ); |
|
| 78 | + |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Register the stylesheets for the public-facing side of the site. |
|
| 83 | + * |
|
| 84 | + * @since 1.0.0 |
|
| 85 | + */ |
|
| 86 | + public function enqueue_scripts() { |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * This function is provided for demonstration purposes only. |
|
| 90 | + * |
|
| 91 | + * An instance of this class should be passed to the run() function |
|
| 92 | + * defined in Wordlift_Loader as all of the hooks are defined |
|
| 93 | + * in that particular class. |
|
| 94 | + * |
|
| 95 | + * The Wordlift_Loader will then create the relationship |
|
| 96 | + * between the defined hooks and the functions defined in this |
|
| 97 | + * class. |
|
| 98 | + */ |
|
| 99 | + |
|
| 100 | + $settings = self::get_settings(); |
|
| 101 | + |
|
| 102 | + // Note that we switched the js to be loaded in footer, since it is loading |
|
| 103 | + // the json-ld representation. |
|
| 104 | + wp_enqueue_script( $this->plugin_name, plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/bundle.js', array(), $this->version, true ); |
|
| 105 | + wp_localize_script( $this->plugin_name, 'wlSettings', $settings ); |
|
| 106 | + |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Get the settings array. |
|
| 111 | + * |
|
| 112 | + * @since 3.19.1 |
|
| 113 | + * |
|
| 114 | + * @return array An array with the settings. |
|
| 115 | + */ |
|
| 116 | + public static function get_settings() { |
|
| 117 | + |
|
| 118 | + // Prepare a settings array for client-side functions. |
|
| 119 | + $settings = array( |
|
| 120 | + 'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
|
| 121 | + 'apiUrl' => get_site_url( null, 'wl-api/' ), |
|
| 122 | + ); |
|
| 123 | + |
|
| 124 | + // If we're in a single page, then print out the post id. |
|
| 125 | + if ( is_singular() ) { |
|
| 126 | + $settings['postId'] = get_the_ID(); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + // Add flag that we are on home/blog page. |
|
| 130 | + if ( is_home() || is_front_page() ) { |
|
| 131 | + $settings['isHome'] = true; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + // By default only enable JSON-LD on supported entity pages (includes |
|
| 135 | + // `page`, `post` and `entity` by default) and on the home page. |
|
| 136 | + // |
|
| 137 | + // @see https://github.com/insideout10/wordlift-plugin/issues/733 |
|
| 138 | + $jsonld_enabled = is_home() || is_front_page() || Wordlift_Entity_Type_Service::is_valid_entity_post_type( get_post_type() ); |
|
| 139 | + |
|
| 140 | + // Add the JSON-LD enabled flag, when set to false, the JSON-lD won't |
|
| 141 | + // be loaded. |
|
| 142 | + // |
|
| 143 | + // @see https://github.com/insideout10/wordlift-plugin/issues/642. |
|
| 144 | + $settings['jsonld_enabled'] = apply_filters( 'wl_jsonld_enabled', $jsonld_enabled ); |
|
| 145 | + |
|
| 146 | + return $settings; |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | 149 | } |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | * @param string $plugin_name The name of the plugin. |
| 49 | 49 | * @param string $version The version of this plugin. |
| 50 | 50 | */ |
| 51 | - public function __construct( $plugin_name, $version ) { |
|
| 51 | + public function __construct($plugin_name, $version) { |
|
| 52 | 52 | |
| 53 | 53 | $this->plugin_name = $plugin_name; |
| 54 | 54 | $this->version = $version; |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | * class. |
| 75 | 75 | */ |
| 76 | 76 | |
| 77 | - wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-public.css', array(), $this->version, 'all' ); |
|
| 77 | + wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__).'css/wordlift-public.css', array(), $this->version, 'all'); |
|
| 78 | 78 | |
| 79 | 79 | } |
| 80 | 80 | |
@@ -101,8 +101,8 @@ discard block |
||
| 101 | 101 | |
| 102 | 102 | // Note that we switched the js to be loaded in footer, since it is loading |
| 103 | 103 | // the json-ld representation. |
| 104 | - wp_enqueue_script( $this->plugin_name, plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/bundle.js', array(), $this->version, true ); |
|
| 105 | - wp_localize_script( $this->plugin_name, 'wlSettings', $settings ); |
|
| 104 | + wp_enqueue_script($this->plugin_name, plugin_dir_url(dirname(__FILE__)).'js/dist/bundle.js', array(), $this->version, true); |
|
| 105 | + wp_localize_script($this->plugin_name, 'wlSettings', $settings); |
|
| 106 | 106 | |
| 107 | 107 | } |
| 108 | 108 | |
@@ -117,17 +117,17 @@ discard block |
||
| 117 | 117 | |
| 118 | 118 | // Prepare a settings array for client-side functions. |
| 119 | 119 | $settings = array( |
| 120 | - 'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
|
| 121 | - 'apiUrl' => get_site_url( null, 'wl-api/' ), |
|
| 120 | + 'ajaxUrl' => admin_url('admin-ajax.php'), |
|
| 121 | + 'apiUrl' => get_site_url(null, 'wl-api/'), |
|
| 122 | 122 | ); |
| 123 | 123 | |
| 124 | 124 | // If we're in a single page, then print out the post id. |
| 125 | - if ( is_singular() ) { |
|
| 125 | + if (is_singular()) { |
|
| 126 | 126 | $settings['postId'] = get_the_ID(); |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | // Add flag that we are on home/blog page. |
| 130 | - if ( is_home() || is_front_page() ) { |
|
| 130 | + if (is_home() || is_front_page()) { |
|
| 131 | 131 | $settings['isHome'] = true; |
| 132 | 132 | } |
| 133 | 133 | |
@@ -135,13 +135,13 @@ discard block |
||
| 135 | 135 | // `page`, `post` and `entity` by default) and on the home page. |
| 136 | 136 | // |
| 137 | 137 | // @see https://github.com/insideout10/wordlift-plugin/issues/733 |
| 138 | - $jsonld_enabled = is_home() || is_front_page() || Wordlift_Entity_Type_Service::is_valid_entity_post_type( get_post_type() ); |
|
| 138 | + $jsonld_enabled = is_home() || is_front_page() || Wordlift_Entity_Type_Service::is_valid_entity_post_type(get_post_type()); |
|
| 139 | 139 | |
| 140 | 140 | // Add the JSON-LD enabled flag, when set to false, the JSON-lD won't |
| 141 | 141 | // be loaded. |
| 142 | 142 | // |
| 143 | 143 | // @see https://github.com/insideout10/wordlift-plugin/issues/642. |
| 144 | - $settings['jsonld_enabled'] = apply_filters( 'wl_jsonld_enabled', $jsonld_enabled ); |
|
| 144 | + $settings['jsonld_enabled'] = apply_filters('wl_jsonld_enabled', $jsonld_enabled); |
|
| 145 | 145 | |
| 146 | 146 | return $settings; |
| 147 | 147 | } |
@@ -21,64 +21,64 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_AMP_Service { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * The {@link \Wordlift_Jsonld_Service} instance. |
|
| 26 | - * |
|
| 27 | - * @since 3.19.1 |
|
| 28 | - * @access private |
|
| 29 | - * @var \Wordlift_Jsonld_Service $jsonld_service The {@link \Wordlift_Jsonld_Service} instance. |
|
| 30 | - */ |
|
| 31 | - private $jsonld_service; |
|
| 24 | + /** |
|
| 25 | + * The {@link \Wordlift_Jsonld_Service} instance. |
|
| 26 | + * |
|
| 27 | + * @since 3.19.1 |
|
| 28 | + * @access private |
|
| 29 | + * @var \Wordlift_Jsonld_Service $jsonld_service The {@link \Wordlift_Jsonld_Service} instance. |
|
| 30 | + */ |
|
| 31 | + private $jsonld_service; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Create a {@link Wordlift_AMP_Service} instance. |
|
| 35 | - * @since 3.19.1 |
|
| 36 | - * |
|
| 37 | - * @param \Wordlift_Jsonld_Service $jsonld_service |
|
| 38 | - */ |
|
| 39 | - function __construct( $jsonld_service ) { |
|
| 33 | + /** |
|
| 34 | + * Create a {@link Wordlift_AMP_Service} instance. |
|
| 35 | + * @since 3.19.1 |
|
| 36 | + * |
|
| 37 | + * @param \Wordlift_Jsonld_Service $jsonld_service |
|
| 38 | + */ |
|
| 39 | + function __construct( $jsonld_service ) { |
|
| 40 | 40 | |
| 41 | - $this->jsonld_service = $jsonld_service; |
|
| 41 | + $this->jsonld_service = $jsonld_service; |
|
| 42 | 42 | |
| 43 | - add_action( 'amp_init', array( $this, 'register_entity_cpt_with_amp_plugin', ) ); |
|
| 44 | - add_filter( 'amp_post_template_metadata', array( $this, 'amp_post_template_metadata', ), 99, 2 ); |
|
| 43 | + add_action( 'amp_init', array( $this, 'register_entity_cpt_with_amp_plugin', ) ); |
|
| 44 | + add_filter( 'amp_post_template_metadata', array( $this, 'amp_post_template_metadata', ), 99, 2 ); |
|
| 45 | 45 | |
| 46 | - } |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Register the `entity` post type with the AMP plugin. |
|
| 50 | - * |
|
| 51 | - * @since 3.12.0 |
|
| 52 | - */ |
|
| 53 | - function register_entity_cpt_with_amp_plugin() { |
|
| 48 | + /** |
|
| 49 | + * Register the `entity` post type with the AMP plugin. |
|
| 50 | + * |
|
| 51 | + * @since 3.12.0 |
|
| 52 | + */ |
|
| 53 | + function register_entity_cpt_with_amp_plugin() { |
|
| 54 | 54 | |
| 55 | - if ( ! defined( 'AMP_QUERY_VAR' ) ) { |
|
| 56 | - return; |
|
| 57 | - } |
|
| 55 | + if ( ! defined( 'AMP_QUERY_VAR' ) ) { |
|
| 56 | + return; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $post_type ) { |
|
| 60 | - // Do not change anything for posts and pages. |
|
| 61 | - if ( 'post' === $post_type || 'page' === $post_type ) { |
|
| 62 | - continue; |
|
| 63 | - } |
|
| 64 | - add_post_type_support( $post_type, AMP_QUERY_VAR ); |
|
| 65 | - } |
|
| 59 | + foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $post_type ) { |
|
| 60 | + // Do not change anything for posts and pages. |
|
| 61 | + if ( 'post' === $post_type || 'page' === $post_type ) { |
|
| 62 | + continue; |
|
| 63 | + } |
|
| 64 | + add_post_type_support( $post_type, AMP_QUERY_VAR ); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - } |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Filters Schema.org metadata for a post. |
|
| 71 | - * |
|
| 72 | - * @since 3.19.1 |
|
| 73 | - * |
|
| 74 | - * @param array $metadata Metadata. |
|
| 75 | - * @param WP_Post $post Post. |
|
| 76 | - * |
|
| 77 | - * @return array Return WordLift's generated JSON-LD. |
|
| 78 | - */ |
|
| 79 | - function amp_post_template_metadata( $metadata, $post ) { |
|
| 69 | + /** |
|
| 70 | + * Filters Schema.org metadata for a post. |
|
| 71 | + * |
|
| 72 | + * @since 3.19.1 |
|
| 73 | + * |
|
| 74 | + * @param array $metadata Metadata. |
|
| 75 | + * @param WP_Post $post Post. |
|
| 76 | + * |
|
| 77 | + * @return array Return WordLift's generated JSON-LD. |
|
| 78 | + */ |
|
| 79 | + function amp_post_template_metadata( $metadata, $post ) { |
|
| 80 | 80 | |
| 81 | - return $this->jsonld_service->get_jsonld( false, $post->ID ); |
|
| 82 | - } |
|
| 81 | + return $this->jsonld_service->get_jsonld( false, $post->ID ); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | 84 | } |
@@ -36,12 +36,12 @@ discard block |
||
| 36 | 36 | * |
| 37 | 37 | * @param \Wordlift_Jsonld_Service $jsonld_service |
| 38 | 38 | */ |
| 39 | - function __construct( $jsonld_service ) { |
|
| 39 | + function __construct($jsonld_service) { |
|
| 40 | 40 | |
| 41 | 41 | $this->jsonld_service = $jsonld_service; |
| 42 | 42 | |
| 43 | - add_action( 'amp_init', array( $this, 'register_entity_cpt_with_amp_plugin', ) ); |
|
| 44 | - add_filter( 'amp_post_template_metadata', array( $this, 'amp_post_template_metadata', ), 99, 2 ); |
|
| 43 | + add_action('amp_init', array($this, 'register_entity_cpt_with_amp_plugin',)); |
|
| 44 | + add_filter('amp_post_template_metadata', array($this, 'amp_post_template_metadata',), 99, 2); |
|
| 45 | 45 | |
| 46 | 46 | } |
| 47 | 47 | |
@@ -52,16 +52,16 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | function register_entity_cpt_with_amp_plugin() { |
| 54 | 54 | |
| 55 | - if ( ! defined( 'AMP_QUERY_VAR' ) ) { |
|
| 55 | + if ( ! defined('AMP_QUERY_VAR')) { |
|
| 56 | 56 | return; |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | - foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $post_type ) { |
|
| 59 | + foreach (Wordlift_Entity_Service::valid_entity_post_types() as $post_type) { |
|
| 60 | 60 | // Do not change anything for posts and pages. |
| 61 | - if ( 'post' === $post_type || 'page' === $post_type ) { |
|
| 61 | + if ('post' === $post_type || 'page' === $post_type) { |
|
| 62 | 62 | continue; |
| 63 | 63 | } |
| 64 | - add_post_type_support( $post_type, AMP_QUERY_VAR ); |
|
| 64 | + add_post_type_support($post_type, AMP_QUERY_VAR); |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | } |
@@ -76,9 +76,9 @@ discard block |
||
| 76 | 76 | * |
| 77 | 77 | * @return array Return WordLift's generated JSON-LD. |
| 78 | 78 | */ |
| 79 | - function amp_post_template_metadata( $metadata, $post ) { |
|
| 79 | + function amp_post_template_metadata($metadata, $post) { |
|
| 80 | 80 | |
| 81 | - return $this->jsonld_service->get_jsonld( false, $post->ID ); |
|
| 81 | + return $this->jsonld_service->get_jsonld(false, $post->ID); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | } |
@@ -17,111 +17,111 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Wordlift_ShareThis_Service { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * The ShareThis function which prints the buttons. |
|
| 22 | - * |
|
| 23 | - * @since 3.2.0 |
|
| 24 | - */ |
|
| 25 | - const ADD_WIDGET_FUNCTION_NAME = 'st_add_widget'; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * The Log service. |
|
| 29 | - * |
|
| 30 | - * @since 3.2.0 |
|
| 31 | - * @access private |
|
| 32 | - * @var \Wordlift_Log_Service $log_service The Log service. |
|
| 33 | - */ |
|
| 34 | - private $log_service; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Create an instance of the ShareThis service. |
|
| 38 | - * |
|
| 39 | - * @since 3.2.0 |
|
| 40 | - */ |
|
| 41 | - public function __construct() { |
|
| 42 | - |
|
| 43 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_ShareThis_Service' ); |
|
| 44 | - |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Receive <em>the_content</em> filter calls from WordPress. |
|
| 49 | - * |
|
| 50 | - * @since 3.2.0 |
|
| 51 | - * |
|
| 52 | - * @param string $content The post content. |
|
| 53 | - * |
|
| 54 | - * @return string The updated post content. |
|
| 55 | - */ |
|
| 56 | - public function the_content( $content ) { |
|
| 57 | - |
|
| 58 | - return $this->call_sharethis( 'the_content', $content ); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Receive <em>the_excerpt</em> filter calls from WordPress. |
|
| 63 | - * |
|
| 64 | - * @since 3.2.0 |
|
| 65 | - * |
|
| 66 | - * @param string $content The post excerpt. |
|
| 67 | - * |
|
| 68 | - * @return string The updated post excerpt. |
|
| 69 | - */ |
|
| 70 | - public function the_excerpt( $content ) { |
|
| 71 | - |
|
| 72 | - return $this->call_sharethis( 'the_excerpt', $content ); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Call the ShareThis function. |
|
| 77 | - * |
|
| 78 | - * @since 3.2.0 |
|
| 79 | - * |
|
| 80 | - * @param string $tag The filter tag. |
|
| 81 | - * @param string $content The post content. |
|
| 82 | - * |
|
| 83 | - * @return string The updated post content. |
|
| 84 | - */ |
|
| 85 | - private function call_sharethis( $tag, $content ) { |
|
| 86 | - |
|
| 87 | - // Get the current post. |
|
| 88 | - global $post; |
|
| 89 | - |
|
| 90 | - // Bail out if the current entity is a post/page since this is already handled by ShareThis. |
|
| 91 | - // |
|
| 92 | - // See https://github.com/insideout10/wordlift-plugin/issues/819 |
|
| 93 | - if ( 'post' === $post->post_type || 'page' === $post->post_type ) { |
|
| 94 | - return $content; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - // If it's not the entity type, return. |
|
| 98 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 99 | - if ( null === $post || ! $entity_service->is_entity( get_the_ID() ) ) { |
|
| 100 | - return $content; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - // If the ShareThis function doesn't exist, return. |
|
| 104 | - if ( ! function_exists( self::ADD_WIDGET_FUNCTION_NAME ) ) { |
|
| 105 | - return $content; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // If ShareThis hasn't been added as a filter, return. |
|
| 109 | - if ( ! has_filter( $tag, self::ADD_WIDGET_FUNCTION_NAME ) ) { |
|
| 110 | - return $content; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - // Temporary pop the post type and replace it with post. |
|
| 114 | - $post_type = $post->post_type; |
|
| 115 | - $post->post_type = 'post'; |
|
| 116 | - |
|
| 117 | - // Call ShareThis (disguised as a post). |
|
| 118 | - $content = call_user_func_array( self::ADD_WIDGET_FUNCTION_NAME, array( $content ) ); |
|
| 119 | - |
|
| 120 | - // Restore our post type. |
|
| 121 | - $post->post_type = $post_type; |
|
| 122 | - |
|
| 123 | - // Finally return the content. |
|
| 124 | - return $content; |
|
| 125 | - } |
|
| 20 | + /** |
|
| 21 | + * The ShareThis function which prints the buttons. |
|
| 22 | + * |
|
| 23 | + * @since 3.2.0 |
|
| 24 | + */ |
|
| 25 | + const ADD_WIDGET_FUNCTION_NAME = 'st_add_widget'; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * The Log service. |
|
| 29 | + * |
|
| 30 | + * @since 3.2.0 |
|
| 31 | + * @access private |
|
| 32 | + * @var \Wordlift_Log_Service $log_service The Log service. |
|
| 33 | + */ |
|
| 34 | + private $log_service; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Create an instance of the ShareThis service. |
|
| 38 | + * |
|
| 39 | + * @since 3.2.0 |
|
| 40 | + */ |
|
| 41 | + public function __construct() { |
|
| 42 | + |
|
| 43 | + $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_ShareThis_Service' ); |
|
| 44 | + |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Receive <em>the_content</em> filter calls from WordPress. |
|
| 49 | + * |
|
| 50 | + * @since 3.2.0 |
|
| 51 | + * |
|
| 52 | + * @param string $content The post content. |
|
| 53 | + * |
|
| 54 | + * @return string The updated post content. |
|
| 55 | + */ |
|
| 56 | + public function the_content( $content ) { |
|
| 57 | + |
|
| 58 | + return $this->call_sharethis( 'the_content', $content ); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Receive <em>the_excerpt</em> filter calls from WordPress. |
|
| 63 | + * |
|
| 64 | + * @since 3.2.0 |
|
| 65 | + * |
|
| 66 | + * @param string $content The post excerpt. |
|
| 67 | + * |
|
| 68 | + * @return string The updated post excerpt. |
|
| 69 | + */ |
|
| 70 | + public function the_excerpt( $content ) { |
|
| 71 | + |
|
| 72 | + return $this->call_sharethis( 'the_excerpt', $content ); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Call the ShareThis function. |
|
| 77 | + * |
|
| 78 | + * @since 3.2.0 |
|
| 79 | + * |
|
| 80 | + * @param string $tag The filter tag. |
|
| 81 | + * @param string $content The post content. |
|
| 82 | + * |
|
| 83 | + * @return string The updated post content. |
|
| 84 | + */ |
|
| 85 | + private function call_sharethis( $tag, $content ) { |
|
| 86 | + |
|
| 87 | + // Get the current post. |
|
| 88 | + global $post; |
|
| 89 | + |
|
| 90 | + // Bail out if the current entity is a post/page since this is already handled by ShareThis. |
|
| 91 | + // |
|
| 92 | + // See https://github.com/insideout10/wordlift-plugin/issues/819 |
|
| 93 | + if ( 'post' === $post->post_type || 'page' === $post->post_type ) { |
|
| 94 | + return $content; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + // If it's not the entity type, return. |
|
| 98 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 99 | + if ( null === $post || ! $entity_service->is_entity( get_the_ID() ) ) { |
|
| 100 | + return $content; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + // If the ShareThis function doesn't exist, return. |
|
| 104 | + if ( ! function_exists( self::ADD_WIDGET_FUNCTION_NAME ) ) { |
|
| 105 | + return $content; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // If ShareThis hasn't been added as a filter, return. |
|
| 109 | + if ( ! has_filter( $tag, self::ADD_WIDGET_FUNCTION_NAME ) ) { |
|
| 110 | + return $content; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + // Temporary pop the post type and replace it with post. |
|
| 114 | + $post_type = $post->post_type; |
|
| 115 | + $post->post_type = 'post'; |
|
| 116 | + |
|
| 117 | + // Call ShareThis (disguised as a post). |
|
| 118 | + $content = call_user_func_array( self::ADD_WIDGET_FUNCTION_NAME, array( $content ) ); |
|
| 119 | + |
|
| 120 | + // Restore our post type. |
|
| 121 | + $post->post_type = $post_type; |
|
| 122 | + |
|
| 123 | + // Finally return the content. |
|
| 124 | + return $content; |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | 127 | } |
@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | */ |
| 41 | 41 | public function __construct() { |
| 42 | 42 | |
| 43 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_ShareThis_Service' ); |
|
| 43 | + $this->log_service = Wordlift_Log_Service::get_logger('Wordlift_ShareThis_Service'); |
|
| 44 | 44 | |
| 45 | 45 | } |
| 46 | 46 | |
@@ -53,9 +53,9 @@ discard block |
||
| 53 | 53 | * |
| 54 | 54 | * @return string The updated post content. |
| 55 | 55 | */ |
| 56 | - public function the_content( $content ) { |
|
| 56 | + public function the_content($content) { |
|
| 57 | 57 | |
| 58 | - return $this->call_sharethis( 'the_content', $content ); |
|
| 58 | + return $this->call_sharethis('the_content', $content); |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | /** |
@@ -67,9 +67,9 @@ discard block |
||
| 67 | 67 | * |
| 68 | 68 | * @return string The updated post excerpt. |
| 69 | 69 | */ |
| 70 | - public function the_excerpt( $content ) { |
|
| 70 | + public function the_excerpt($content) { |
|
| 71 | 71 | |
| 72 | - return $this->call_sharethis( 'the_excerpt', $content ); |
|
| 72 | + return $this->call_sharethis('the_excerpt', $content); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | * |
| 83 | 83 | * @return string The updated post content. |
| 84 | 84 | */ |
| 85 | - private function call_sharethis( $tag, $content ) { |
|
| 85 | + private function call_sharethis($tag, $content) { |
|
| 86 | 86 | |
| 87 | 87 | // Get the current post. |
| 88 | 88 | global $post; |
@@ -90,23 +90,23 @@ discard block |
||
| 90 | 90 | // Bail out if the current entity is a post/page since this is already handled by ShareThis. |
| 91 | 91 | // |
| 92 | 92 | // See https://github.com/insideout10/wordlift-plugin/issues/819 |
| 93 | - if ( 'post' === $post->post_type || 'page' === $post->post_type ) { |
|
| 93 | + if ('post' === $post->post_type || 'page' === $post->post_type) { |
|
| 94 | 94 | return $content; |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | // If it's not the entity type, return. |
| 98 | 98 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 99 | - if ( null === $post || ! $entity_service->is_entity( get_the_ID() ) ) { |
|
| 99 | + if (null === $post || ! $entity_service->is_entity(get_the_ID())) { |
|
| 100 | 100 | return $content; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | // If the ShareThis function doesn't exist, return. |
| 104 | - if ( ! function_exists( self::ADD_WIDGET_FUNCTION_NAME ) ) { |
|
| 104 | + if ( ! function_exists(self::ADD_WIDGET_FUNCTION_NAME)) { |
|
| 105 | 105 | return $content; |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | // If ShareThis hasn't been added as a filter, return. |
| 109 | - if ( ! has_filter( $tag, self::ADD_WIDGET_FUNCTION_NAME ) ) { |
|
| 109 | + if ( ! has_filter($tag, self::ADD_WIDGET_FUNCTION_NAME)) { |
|
| 110 | 110 | return $content; |
| 111 | 111 | } |
| 112 | 112 | |
@@ -115,7 +115,7 @@ discard block |
||
| 115 | 115 | $post->post_type = 'post'; |
| 116 | 116 | |
| 117 | 117 | // Call ShareThis (disguised as a post). |
| 118 | - $content = call_user_func_array( self::ADD_WIDGET_FUNCTION_NAME, array( $content ) ); |
|
| 118 | + $content = call_user_func_array(self::ADD_WIDGET_FUNCTION_NAME, array($content)); |
|
| 119 | 119 | |
| 120 | 120 | // Restore our post type. |
| 121 | 121 | $post->post_type = $post_type; |
@@ -19,420 +19,420 @@ discard block |
||
| 19 | 19 | */ |
| 20 | 20 | class WL_Metabox_Field { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * A {@link Wordlift_Log_Service} instance. |
|
| 24 | - * |
|
| 25 | - * @since 3.15.0 |
|
| 26 | - * @access protected |
|
| 27 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 28 | - */ |
|
| 29 | - protected $log; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * The meta name for this field's value. |
|
| 33 | - * |
|
| 34 | - * @var string $meta_name The meta name for this field's value. |
|
| 35 | - */ |
|
| 36 | - public $meta_name; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * The custom field settings. |
|
| 40 | - * |
|
| 41 | - * @var null|array $raw_custom_field The custom field settings. |
|
| 42 | - */ |
|
| 43 | - public $raw_custom_field; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * The schema.org predicate. |
|
| 47 | - * |
|
| 48 | - * @var string $predicate The schema.org predicate. |
|
| 49 | - */ |
|
| 50 | - public $predicate; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * The field's label. |
|
| 54 | - * |
|
| 55 | - * @var string $label The field's label. |
|
| 56 | - */ |
|
| 57 | - public $label; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * The WordLift data type. |
|
| 61 | - * |
|
| 62 | - * @var string $expected_wl_type The WordLift data type. |
|
| 63 | - */ |
|
| 64 | - public $expected_wl_type; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The RDF data type. |
|
| 68 | - * |
|
| 69 | - * @var string $expected_uri_type The RDF data type. |
|
| 70 | - */ |
|
| 71 | - public $expected_uri_type; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * The cardinality. |
|
| 75 | - * |
|
| 76 | - * @var int $cardinality The cardinality. |
|
| 77 | - */ |
|
| 78 | - public $cardinality; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * The current value. |
|
| 82 | - * |
|
| 83 | - * @var array $data The current value. |
|
| 84 | - */ |
|
| 85 | - public $data; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * The current {@link WP_Post} id. |
|
| 89 | - * |
|
| 90 | - * @since 3.15.3 |
|
| 91 | - * |
|
| 92 | - * @var int The current {@link WP_Post} id. |
|
| 93 | - */ |
|
| 94 | - private $post_id; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Create a {@link WL_Metabox_Field} instance. |
|
| 98 | - * |
|
| 99 | - * @param array $args An array of parameters. |
|
| 100 | - */ |
|
| 101 | - public function __construct( $args ) { |
|
| 102 | - |
|
| 103 | - $this->log = Wordlift_Log_Service::get_logger( 'WL_Metabox_Field' ); |
|
| 104 | - |
|
| 105 | - if ( empty( $args ) ) { |
|
| 106 | - return; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - // Save a copy of the custom field's params. |
|
| 110 | - $this->raw_custom_field = reset( $args ); |
|
| 111 | - |
|
| 112 | - // Extract meta name (post_meta key for the DB). |
|
| 113 | - $this->meta_name = key( $args ); |
|
| 114 | - |
|
| 115 | - // Extract linked data predicate. |
|
| 116 | - if ( isset( $this->raw_custom_field['predicate'] ) ) { |
|
| 117 | - $this->predicate = $this->raw_custom_field['predicate']; |
|
| 118 | - } else { |
|
| 119 | - return; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - // Extract human readable label. |
|
| 123 | - $exploded_predicate = explode( '/', $this->predicate ); |
|
| 124 | - |
|
| 125 | - // Use the label defined for the property if set, otherwise the last part of the schema.org/xyz predicate. |
|
| 126 | - $this->label = isset( $this->raw_custom_field['metabox']['label'] ) ? $this->raw_custom_field['metabox']['label'] : end( $exploded_predicate ); |
|
| 127 | - |
|
| 128 | - // Extract field constraints (numerosity, expected type). |
|
| 129 | - // Default constaints: accept one string.. |
|
| 130 | - if ( isset( $this->raw_custom_field['type'] ) ) { |
|
| 131 | - $this->expected_wl_type = $this->raw_custom_field['type']; |
|
| 132 | - } else { |
|
| 133 | - $this->expected_wl_type = Wordlift_Schema_Service::DATA_TYPE_STRING; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - $this->cardinality = 1; |
|
| 137 | - if ( isset( $this->raw_custom_field['constraints'] ) ) { |
|
| 138 | - |
|
| 139 | - $constraints = $this->raw_custom_field['constraints']; |
|
| 140 | - |
|
| 141 | - // Extract cardinality. |
|
| 142 | - if ( isset( $constraints['cardinality'] ) ) { |
|
| 143 | - $this->cardinality = $constraints['cardinality']; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - // Which type of entity can we accept (e.g. Place, Event, ecc.)? . |
|
| 147 | - if ( Wordlift_Schema_Service::DATA_TYPE_URI === $this->expected_wl_type && isset( $constraints['uri_type'] ) ) { |
|
| 148 | - $this->expected_uri_type = is_array( $constraints['uri_type'] ) |
|
| 149 | - ? $constraints['uri_type'] |
|
| 150 | - : array( $constraints['uri_type'] ); |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - // Save early the post id to avoid other plugins messing up with it. |
|
| 155 | - // |
|
| 156 | - // See https://github.com/insideout10/wordlift-plugin/issues/665. |
|
| 157 | - $this->post_id = get_the_ID(); |
|
| 158 | - |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Return nonce HTML. |
|
| 163 | - * |
|
| 164 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 165 | - */ |
|
| 166 | - public function html_nonce() { |
|
| 167 | - |
|
| 168 | - return wp_nonce_field( 'wordlift_' . $this->meta_name . '_entity_box', 'wordlift_' . $this->meta_name . '_entity_box_nonce', true, false ); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Verify nonce. |
|
| 173 | - * |
|
| 174 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 175 | - * |
|
| 176 | - * @return boolean Nonce verification. |
|
| 177 | - */ |
|
| 178 | - public function verify_nonce() { |
|
| 179 | - |
|
| 180 | - $nonce_name = 'wordlift_' . $this->meta_name . '_entity_box_nonce'; |
|
| 181 | - $nonce_verify = 'wordlift_' . $this->meta_name . '_entity_box'; |
|
| 182 | - |
|
| 183 | - if ( ! isset( $_POST[ $nonce_name ] ) ) { |
|
| 184 | - return false; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - // Verify that the nonce is valid. |
|
| 188 | - return wp_verify_nonce( $_POST[ $nonce_name ], $nonce_verify ); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Load data from DB and store the resulting array in $this->data. |
|
| 193 | - * |
|
| 194 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 195 | - */ |
|
| 196 | - public function get_data() { |
|
| 197 | - |
|
| 198 | - // Get the post id and load the data. |
|
| 199 | - $post_id = $this->post_id; |
|
| 200 | - $this->data = get_post_meta( $post_id, $this->meta_name ); |
|
| 201 | - |
|
| 202 | - $this->log->debug( 'Found ' . count( $this->data ) . " value(s) for meta $this->meta_name, post $post_id." ); |
|
| 203 | - |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Sanitizes data before saving to DB. Default sanitization trashes empty |
|
| 208 | - * values. |
|
| 209 | - * |
|
| 210 | - * Stores the sanitized values into $this->data so they can be later processed. |
|
| 211 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 212 | - * |
|
| 213 | - * @param array $values Array of values to be sanitized and then stored into |
|
| 214 | - * $this->data. |
|
| 215 | - */ |
|
| 216 | - public function sanitize_data( $values ) { |
|
| 217 | - |
|
| 218 | - $sanitized_data = array(); |
|
| 219 | - |
|
| 220 | - if ( ! is_array( $values ) ) { |
|
| 221 | - $values = array( $values ); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - foreach ( $values as $value ) { |
|
| 225 | - $sanitized_value = $this->sanitize_data_filter( $value ); |
|
| 226 | - if ( ! is_null( $sanitized_value ) ) { |
|
| 227 | - $sanitized_data[] = $sanitized_value; |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - $this->data = $sanitized_data; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * Sanitize a single value. Called from $this->sanitize_data. Default |
|
| 236 | - * sanitization excludes empty values. |
|
| 237 | - * |
|
| 238 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 239 | - * |
|
| 240 | - * @param string $value The value to sanitize. |
|
| 241 | - * |
|
| 242 | - * @return mixed Returns sanitized value, or null. |
|
| 243 | - */ |
|
| 244 | - public function sanitize_data_filter( $value ) { |
|
| 245 | - |
|
| 246 | - // TODO: all fields should provide their own sanitize which shouldn't |
|
| 247 | - // be part of a UI class. |
|
| 248 | - |
|
| 249 | - // If the field provides its own validation, use it. |
|
| 250 | - if ( isset( $this->raw_custom_field['sanitize'] ) ) { |
|
| 251 | - return call_user_func( $this->raw_custom_field['sanitize'], $value ); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - if ( ! is_null( $value ) && '' !== $value ) { // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ . |
|
| 255 | - return $value; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - return null; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Save data to DB. |
|
| 263 | - * |
|
| 264 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 265 | - * |
|
| 266 | - * @param array $values Array of values to be sanitized and then stored into $this->data. |
|
| 267 | - */ |
|
| 268 | - public function save_data( $values ) { |
|
| 269 | - |
|
| 270 | - // Will sanitize data and store them in $field->data. |
|
| 271 | - $this->sanitize_data( $values ); |
|
| 272 | - |
|
| 273 | - // Bail out, if the post id isn't set in the request or isn't numeric. |
|
| 274 | - // |
|
| 275 | - // See https://github.com/insideout10/wordlift-plugin/issues/665. |
|
| 276 | - if ( ! isset( $_POST['post_ID'] ) || ! is_numeric( $_POST['post_ID'] ) ) { |
|
| 277 | - return; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - $entity_id = intval( $_POST['post_ID'] ); |
|
| 281 | - |
|
| 282 | - // Take away old values. |
|
| 283 | - delete_post_meta( $entity_id, $this->meta_name ); |
|
| 284 | - |
|
| 285 | - // insert new values, respecting cardinality. |
|
| 286 | - $single = ( 1 === $this->cardinality ); |
|
| 287 | - foreach ( $this->data as $value ) { |
|
| 288 | - $this->log->trace( "Saving $value to $this->meta_name for entity $entity_id..." ); |
|
| 289 | - $meta_id = add_post_meta( $entity_id, $this->meta_name, $value, $single ); |
|
| 290 | - $this->log->debug( "$value to $this->meta_name for entity $entity_id saved with id $meta_id." ); |
|
| 291 | - } |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * Returns the HTML tag that will contain the Field. By default the we |
|
| 296 | - * return a <div> with data- attributes on cardinality and expected types. |
|
| 297 | - * |
|
| 298 | - * It is useful to provide data- attributes for the JS scripts. |
|
| 299 | - * |
|
| 300 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 301 | - */ |
|
| 302 | - public function html_wrapper_open() { |
|
| 303 | - |
|
| 304 | - return "<div class='wl-field' data-cardinality='$this->cardinality'>"; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * Returns Field HTML (nonce included). |
|
| 309 | - * |
|
| 310 | - * Overwrite this method (or methods called from this method) in a child |
|
| 311 | - * class to obtain custom behaviour. |
|
| 312 | - * |
|
| 313 | - * The HTML fragment includes the following parts: |
|
| 314 | - * * html wrapper open. |
|
| 315 | - * * heading. |
|
| 316 | - * * nonce. |
|
| 317 | - * * stored values. |
|
| 318 | - * * an empty input when there are no stored values. |
|
| 319 | - * * an add button to add more values. |
|
| 320 | - * * html wrapper close. |
|
| 321 | - */ |
|
| 322 | - public function html() { |
|
| 323 | - |
|
| 324 | - // Open main <div> for the Field. |
|
| 325 | - $html = $this->html_wrapper_open(); |
|
| 326 | - |
|
| 327 | - // Label. |
|
| 328 | - $html .= $this->get_heading_html(); |
|
| 329 | - |
|
| 330 | - // print nonce. |
|
| 331 | - $html .= $this->html_nonce(); |
|
| 332 | - |
|
| 333 | - // print data loaded from DB. |
|
| 334 | - $count = 0; |
|
| 335 | - $html .= $this->get_stored_values_html( $count ); |
|
| 336 | - |
|
| 337 | - // Print the empty <input> to add new values. |
|
| 338 | - if ( 0 === $count ) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings. |
|
| 339 | - $this->log->debug( 'Going to print an empty HTML input...' ); |
|
| 340 | - $html .= $this->html_input( '' ); // Will print an empty <input>. |
|
| 341 | - $count ++; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - // If cardinality allows it, print button to add new values. |
|
| 345 | - $html .= $this->get_add_button_html( $count ); |
|
| 346 | - |
|
| 347 | - // Close the HTML wrapper. |
|
| 348 | - $html .= $this->html_wrapper_close(); |
|
| 349 | - |
|
| 350 | - return $html; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * Print the heading with the label for the metabox. |
|
| 355 | - * |
|
| 356 | - * @since 3.15.0 |
|
| 357 | - * @return string The heading html fragment. |
|
| 358 | - */ |
|
| 359 | - protected function get_heading_html() { |
|
| 360 | - |
|
| 361 | - return "<h3>$this->label</h3>"; |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - /** |
|
| 365 | - * Print the stored values. |
|
| 366 | - * |
|
| 367 | - * @since 3.15.0 |
|
| 368 | - * |
|
| 369 | - * @param int $count An output value: the number of printed values. |
|
| 370 | - * |
|
| 371 | - * @return string The html fragment. |
|
| 372 | - */ |
|
| 373 | - protected function get_stored_values_html( &$count ) { |
|
| 374 | - |
|
| 375 | - $html = ''; |
|
| 376 | - |
|
| 377 | - // print data loaded from DB. |
|
| 378 | - $count = 0; |
|
| 379 | - if ( $this->data ) { |
|
| 380 | - foreach ( $this->data as $value ) { |
|
| 381 | - if ( $count < $this->cardinality ) { |
|
| 382 | - $this->log->debug( "Going to print an HTML input #$count with $value..." ); |
|
| 383 | - $fragment = $this->html_input( $value ); |
|
| 384 | - |
|
| 385 | - // If the fragment is empty, continue to the next one. This is necessary because the |
|
| 22 | + /** |
|
| 23 | + * A {@link Wordlift_Log_Service} instance. |
|
| 24 | + * |
|
| 25 | + * @since 3.15.0 |
|
| 26 | + * @access protected |
|
| 27 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 28 | + */ |
|
| 29 | + protected $log; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * The meta name for this field's value. |
|
| 33 | + * |
|
| 34 | + * @var string $meta_name The meta name for this field's value. |
|
| 35 | + */ |
|
| 36 | + public $meta_name; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * The custom field settings. |
|
| 40 | + * |
|
| 41 | + * @var null|array $raw_custom_field The custom field settings. |
|
| 42 | + */ |
|
| 43 | + public $raw_custom_field; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * The schema.org predicate. |
|
| 47 | + * |
|
| 48 | + * @var string $predicate The schema.org predicate. |
|
| 49 | + */ |
|
| 50 | + public $predicate; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * The field's label. |
|
| 54 | + * |
|
| 55 | + * @var string $label The field's label. |
|
| 56 | + */ |
|
| 57 | + public $label; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * The WordLift data type. |
|
| 61 | + * |
|
| 62 | + * @var string $expected_wl_type The WordLift data type. |
|
| 63 | + */ |
|
| 64 | + public $expected_wl_type; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The RDF data type. |
|
| 68 | + * |
|
| 69 | + * @var string $expected_uri_type The RDF data type. |
|
| 70 | + */ |
|
| 71 | + public $expected_uri_type; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * The cardinality. |
|
| 75 | + * |
|
| 76 | + * @var int $cardinality The cardinality. |
|
| 77 | + */ |
|
| 78 | + public $cardinality; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * The current value. |
|
| 82 | + * |
|
| 83 | + * @var array $data The current value. |
|
| 84 | + */ |
|
| 85 | + public $data; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * The current {@link WP_Post} id. |
|
| 89 | + * |
|
| 90 | + * @since 3.15.3 |
|
| 91 | + * |
|
| 92 | + * @var int The current {@link WP_Post} id. |
|
| 93 | + */ |
|
| 94 | + private $post_id; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Create a {@link WL_Metabox_Field} instance. |
|
| 98 | + * |
|
| 99 | + * @param array $args An array of parameters. |
|
| 100 | + */ |
|
| 101 | + public function __construct( $args ) { |
|
| 102 | + |
|
| 103 | + $this->log = Wordlift_Log_Service::get_logger( 'WL_Metabox_Field' ); |
|
| 104 | + |
|
| 105 | + if ( empty( $args ) ) { |
|
| 106 | + return; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + // Save a copy of the custom field's params. |
|
| 110 | + $this->raw_custom_field = reset( $args ); |
|
| 111 | + |
|
| 112 | + // Extract meta name (post_meta key for the DB). |
|
| 113 | + $this->meta_name = key( $args ); |
|
| 114 | + |
|
| 115 | + // Extract linked data predicate. |
|
| 116 | + if ( isset( $this->raw_custom_field['predicate'] ) ) { |
|
| 117 | + $this->predicate = $this->raw_custom_field['predicate']; |
|
| 118 | + } else { |
|
| 119 | + return; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + // Extract human readable label. |
|
| 123 | + $exploded_predicate = explode( '/', $this->predicate ); |
|
| 124 | + |
|
| 125 | + // Use the label defined for the property if set, otherwise the last part of the schema.org/xyz predicate. |
|
| 126 | + $this->label = isset( $this->raw_custom_field['metabox']['label'] ) ? $this->raw_custom_field['metabox']['label'] : end( $exploded_predicate ); |
|
| 127 | + |
|
| 128 | + // Extract field constraints (numerosity, expected type). |
|
| 129 | + // Default constaints: accept one string.. |
|
| 130 | + if ( isset( $this->raw_custom_field['type'] ) ) { |
|
| 131 | + $this->expected_wl_type = $this->raw_custom_field['type']; |
|
| 132 | + } else { |
|
| 133 | + $this->expected_wl_type = Wordlift_Schema_Service::DATA_TYPE_STRING; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + $this->cardinality = 1; |
|
| 137 | + if ( isset( $this->raw_custom_field['constraints'] ) ) { |
|
| 138 | + |
|
| 139 | + $constraints = $this->raw_custom_field['constraints']; |
|
| 140 | + |
|
| 141 | + // Extract cardinality. |
|
| 142 | + if ( isset( $constraints['cardinality'] ) ) { |
|
| 143 | + $this->cardinality = $constraints['cardinality']; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + // Which type of entity can we accept (e.g. Place, Event, ecc.)? . |
|
| 147 | + if ( Wordlift_Schema_Service::DATA_TYPE_URI === $this->expected_wl_type && isset( $constraints['uri_type'] ) ) { |
|
| 148 | + $this->expected_uri_type = is_array( $constraints['uri_type'] ) |
|
| 149 | + ? $constraints['uri_type'] |
|
| 150 | + : array( $constraints['uri_type'] ); |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + // Save early the post id to avoid other plugins messing up with it. |
|
| 155 | + // |
|
| 156 | + // See https://github.com/insideout10/wordlift-plugin/issues/665. |
|
| 157 | + $this->post_id = get_the_ID(); |
|
| 158 | + |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Return nonce HTML. |
|
| 163 | + * |
|
| 164 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 165 | + */ |
|
| 166 | + public function html_nonce() { |
|
| 167 | + |
|
| 168 | + return wp_nonce_field( 'wordlift_' . $this->meta_name . '_entity_box', 'wordlift_' . $this->meta_name . '_entity_box_nonce', true, false ); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Verify nonce. |
|
| 173 | + * |
|
| 174 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 175 | + * |
|
| 176 | + * @return boolean Nonce verification. |
|
| 177 | + */ |
|
| 178 | + public function verify_nonce() { |
|
| 179 | + |
|
| 180 | + $nonce_name = 'wordlift_' . $this->meta_name . '_entity_box_nonce'; |
|
| 181 | + $nonce_verify = 'wordlift_' . $this->meta_name . '_entity_box'; |
|
| 182 | + |
|
| 183 | + if ( ! isset( $_POST[ $nonce_name ] ) ) { |
|
| 184 | + return false; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + // Verify that the nonce is valid. |
|
| 188 | + return wp_verify_nonce( $_POST[ $nonce_name ], $nonce_verify ); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Load data from DB and store the resulting array in $this->data. |
|
| 193 | + * |
|
| 194 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 195 | + */ |
|
| 196 | + public function get_data() { |
|
| 197 | + |
|
| 198 | + // Get the post id and load the data. |
|
| 199 | + $post_id = $this->post_id; |
|
| 200 | + $this->data = get_post_meta( $post_id, $this->meta_name ); |
|
| 201 | + |
|
| 202 | + $this->log->debug( 'Found ' . count( $this->data ) . " value(s) for meta $this->meta_name, post $post_id." ); |
|
| 203 | + |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Sanitizes data before saving to DB. Default sanitization trashes empty |
|
| 208 | + * values. |
|
| 209 | + * |
|
| 210 | + * Stores the sanitized values into $this->data so they can be later processed. |
|
| 211 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 212 | + * |
|
| 213 | + * @param array $values Array of values to be sanitized and then stored into |
|
| 214 | + * $this->data. |
|
| 215 | + */ |
|
| 216 | + public function sanitize_data( $values ) { |
|
| 217 | + |
|
| 218 | + $sanitized_data = array(); |
|
| 219 | + |
|
| 220 | + if ( ! is_array( $values ) ) { |
|
| 221 | + $values = array( $values ); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + foreach ( $values as $value ) { |
|
| 225 | + $sanitized_value = $this->sanitize_data_filter( $value ); |
|
| 226 | + if ( ! is_null( $sanitized_value ) ) { |
|
| 227 | + $sanitized_data[] = $sanitized_value; |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + $this->data = $sanitized_data; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * Sanitize a single value. Called from $this->sanitize_data. Default |
|
| 236 | + * sanitization excludes empty values. |
|
| 237 | + * |
|
| 238 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 239 | + * |
|
| 240 | + * @param string $value The value to sanitize. |
|
| 241 | + * |
|
| 242 | + * @return mixed Returns sanitized value, or null. |
|
| 243 | + */ |
|
| 244 | + public function sanitize_data_filter( $value ) { |
|
| 245 | + |
|
| 246 | + // TODO: all fields should provide their own sanitize which shouldn't |
|
| 247 | + // be part of a UI class. |
|
| 248 | + |
|
| 249 | + // If the field provides its own validation, use it. |
|
| 250 | + if ( isset( $this->raw_custom_field['sanitize'] ) ) { |
|
| 251 | + return call_user_func( $this->raw_custom_field['sanitize'], $value ); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + if ( ! is_null( $value ) && '' !== $value ) { // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ . |
|
| 255 | + return $value; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + return null; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Save data to DB. |
|
| 263 | + * |
|
| 264 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 265 | + * |
|
| 266 | + * @param array $values Array of values to be sanitized and then stored into $this->data. |
|
| 267 | + */ |
|
| 268 | + public function save_data( $values ) { |
|
| 269 | + |
|
| 270 | + // Will sanitize data and store them in $field->data. |
|
| 271 | + $this->sanitize_data( $values ); |
|
| 272 | + |
|
| 273 | + // Bail out, if the post id isn't set in the request or isn't numeric. |
|
| 274 | + // |
|
| 275 | + // See https://github.com/insideout10/wordlift-plugin/issues/665. |
|
| 276 | + if ( ! isset( $_POST['post_ID'] ) || ! is_numeric( $_POST['post_ID'] ) ) { |
|
| 277 | + return; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + $entity_id = intval( $_POST['post_ID'] ); |
|
| 281 | + |
|
| 282 | + // Take away old values. |
|
| 283 | + delete_post_meta( $entity_id, $this->meta_name ); |
|
| 284 | + |
|
| 285 | + // insert new values, respecting cardinality. |
|
| 286 | + $single = ( 1 === $this->cardinality ); |
|
| 287 | + foreach ( $this->data as $value ) { |
|
| 288 | + $this->log->trace( "Saving $value to $this->meta_name for entity $entity_id..." ); |
|
| 289 | + $meta_id = add_post_meta( $entity_id, $this->meta_name, $value, $single ); |
|
| 290 | + $this->log->debug( "$value to $this->meta_name for entity $entity_id saved with id $meta_id." ); |
|
| 291 | + } |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * Returns the HTML tag that will contain the Field. By default the we |
|
| 296 | + * return a <div> with data- attributes on cardinality and expected types. |
|
| 297 | + * |
|
| 298 | + * It is useful to provide data- attributes for the JS scripts. |
|
| 299 | + * |
|
| 300 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 301 | + */ |
|
| 302 | + public function html_wrapper_open() { |
|
| 303 | + |
|
| 304 | + return "<div class='wl-field' data-cardinality='$this->cardinality'>"; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * Returns Field HTML (nonce included). |
|
| 309 | + * |
|
| 310 | + * Overwrite this method (or methods called from this method) in a child |
|
| 311 | + * class to obtain custom behaviour. |
|
| 312 | + * |
|
| 313 | + * The HTML fragment includes the following parts: |
|
| 314 | + * * html wrapper open. |
|
| 315 | + * * heading. |
|
| 316 | + * * nonce. |
|
| 317 | + * * stored values. |
|
| 318 | + * * an empty input when there are no stored values. |
|
| 319 | + * * an add button to add more values. |
|
| 320 | + * * html wrapper close. |
|
| 321 | + */ |
|
| 322 | + public function html() { |
|
| 323 | + |
|
| 324 | + // Open main <div> for the Field. |
|
| 325 | + $html = $this->html_wrapper_open(); |
|
| 326 | + |
|
| 327 | + // Label. |
|
| 328 | + $html .= $this->get_heading_html(); |
|
| 329 | + |
|
| 330 | + // print nonce. |
|
| 331 | + $html .= $this->html_nonce(); |
|
| 332 | + |
|
| 333 | + // print data loaded from DB. |
|
| 334 | + $count = 0; |
|
| 335 | + $html .= $this->get_stored_values_html( $count ); |
|
| 336 | + |
|
| 337 | + // Print the empty <input> to add new values. |
|
| 338 | + if ( 0 === $count ) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings. |
|
| 339 | + $this->log->debug( 'Going to print an empty HTML input...' ); |
|
| 340 | + $html .= $this->html_input( '' ); // Will print an empty <input>. |
|
| 341 | + $count ++; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + // If cardinality allows it, print button to add new values. |
|
| 345 | + $html .= $this->get_add_button_html( $count ); |
|
| 346 | + |
|
| 347 | + // Close the HTML wrapper. |
|
| 348 | + $html .= $this->html_wrapper_close(); |
|
| 349 | + |
|
| 350 | + return $html; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * Print the heading with the label for the metabox. |
|
| 355 | + * |
|
| 356 | + * @since 3.15.0 |
|
| 357 | + * @return string The heading html fragment. |
|
| 358 | + */ |
|
| 359 | + protected function get_heading_html() { |
|
| 360 | + |
|
| 361 | + return "<h3>$this->label</h3>"; |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + /** |
|
| 365 | + * Print the stored values. |
|
| 366 | + * |
|
| 367 | + * @since 3.15.0 |
|
| 368 | + * |
|
| 369 | + * @param int $count An output value: the number of printed values. |
|
| 370 | + * |
|
| 371 | + * @return string The html fragment. |
|
| 372 | + */ |
|
| 373 | + protected function get_stored_values_html( &$count ) { |
|
| 374 | + |
|
| 375 | + $html = ''; |
|
| 376 | + |
|
| 377 | + // print data loaded from DB. |
|
| 378 | + $count = 0; |
|
| 379 | + if ( $this->data ) { |
|
| 380 | + foreach ( $this->data as $value ) { |
|
| 381 | + if ( $count < $this->cardinality ) { |
|
| 382 | + $this->log->debug( "Going to print an HTML input #$count with $value..." ); |
|
| 383 | + $fragment = $this->html_input( $value ); |
|
| 384 | + |
|
| 385 | + // If the fragment is empty, continue to the next one. This is necessary because the |
|
| 386 | 386 | // metabox may reference an invalid value which would cause the metabox not to print, |
| 387 | 387 | // returning an empty html fragment. |
| 388 | 388 | // |
| 389 | 389 | // See https://github.com/insideout10/wordlift-plugin/issues/818 |
| 390 | - if ( '' === $fragment ) { |
|
| 391 | - continue; |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - $html .= $fragment; |
|
| 395 | - $count ++; |
|
| 396 | - } |
|
| 397 | - } |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - return $html; |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * Get the add button html. |
|
| 405 | - * |
|
| 406 | - * This function is protected, allowing extending class to further customize |
|
| 407 | - * the add button html code. |
|
| 408 | - * |
|
| 409 | - * @since 3.15.0 |
|
| 410 | - * |
|
| 411 | - * @param int $count The current number of values. |
|
| 412 | - * |
|
| 413 | - * @return string The add button html code. |
|
| 414 | - */ |
|
| 415 | - protected function get_add_button_html( $count ) { |
|
| 416 | - |
|
| 417 | - // If cardinality allows it, print button to add new values. |
|
| 418 | - if ( $count < $this->cardinality ) { |
|
| 419 | - return '<button class="button wl-add-input wl-button" type="button">' . esc_html__( 'Add' ) . '</button>'; |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - // Return an empty string. |
|
| 423 | - return ''; |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * Return a single <input> tag for the Field. |
|
| 428 | - * |
|
| 429 | - * @param mixed $value Input value. |
|
| 430 | - * |
|
| 431 | - * @return string The html code fragment. |
|
| 432 | - */ |
|
| 433 | - public function html_input( $value ) { |
|
| 434 | - @ob_start(); |
|
| 435 | - ?> |
|
| 390 | + if ( '' === $fragment ) { |
|
| 391 | + continue; |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + $html .= $fragment; |
|
| 395 | + $count ++; |
|
| 396 | + } |
|
| 397 | + } |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + return $html; |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * Get the add button html. |
|
| 405 | + * |
|
| 406 | + * This function is protected, allowing extending class to further customize |
|
| 407 | + * the add button html code. |
|
| 408 | + * |
|
| 409 | + * @since 3.15.0 |
|
| 410 | + * |
|
| 411 | + * @param int $count The current number of values. |
|
| 412 | + * |
|
| 413 | + * @return string The add button html code. |
|
| 414 | + */ |
|
| 415 | + protected function get_add_button_html( $count ) { |
|
| 416 | + |
|
| 417 | + // If cardinality allows it, print button to add new values. |
|
| 418 | + if ( $count < $this->cardinality ) { |
|
| 419 | + return '<button class="button wl-add-input wl-button" type="button">' . esc_html__( 'Add' ) . '</button>'; |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + // Return an empty string. |
|
| 423 | + return ''; |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * Return a single <input> tag for the Field. |
|
| 428 | + * |
|
| 429 | + * @param mixed $value Input value. |
|
| 430 | + * |
|
| 431 | + * @return string The html code fragment. |
|
| 432 | + */ |
|
| 433 | + public function html_input( $value ) { |
|
| 434 | + @ob_start(); |
|
| 435 | + ?> |
|
| 436 | 436 | <div class="wl-input-wrapper"> |
| 437 | 437 | <input |
| 438 | 438 | type="text" |
@@ -447,17 +447,17 @@ discard block |
||
| 447 | 447 | </button> |
| 448 | 448 | </div> |
| 449 | 449 | <?php |
| 450 | - $html = ob_get_clean(); |
|
| 450 | + $html = ob_get_clean(); |
|
| 451 | 451 | |
| 452 | - return $html; |
|
| 453 | - } |
|
| 452 | + return $html; |
|
| 453 | + } |
|
| 454 | 454 | |
| 455 | - /** |
|
| 456 | - * Returns closing for the wrapper HTML tag. |
|
| 457 | - */ |
|
| 458 | - public function html_wrapper_close() { |
|
| 455 | + /** |
|
| 456 | + * Returns closing for the wrapper HTML tag. |
|
| 457 | + */ |
|
| 458 | + public function html_wrapper_close() { |
|
| 459 | 459 | |
| 460 | - return '</div><hr>'; |
|
| 461 | - } |
|
| 460 | + return '</div><hr>'; |
|
| 461 | + } |
|
| 462 | 462 | |
| 463 | 463 | } |
@@ -98,56 +98,56 @@ discard block |
||
| 98 | 98 | * |
| 99 | 99 | * @param array $args An array of parameters. |
| 100 | 100 | */ |
| 101 | - public function __construct( $args ) { |
|
| 101 | + public function __construct($args) { |
|
| 102 | 102 | |
| 103 | - $this->log = Wordlift_Log_Service::get_logger( 'WL_Metabox_Field' ); |
|
| 103 | + $this->log = Wordlift_Log_Service::get_logger('WL_Metabox_Field'); |
|
| 104 | 104 | |
| 105 | - if ( empty( $args ) ) { |
|
| 105 | + if (empty($args)) { |
|
| 106 | 106 | return; |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | // Save a copy of the custom field's params. |
| 110 | - $this->raw_custom_field = reset( $args ); |
|
| 110 | + $this->raw_custom_field = reset($args); |
|
| 111 | 111 | |
| 112 | 112 | // Extract meta name (post_meta key for the DB). |
| 113 | - $this->meta_name = key( $args ); |
|
| 113 | + $this->meta_name = key($args); |
|
| 114 | 114 | |
| 115 | 115 | // Extract linked data predicate. |
| 116 | - if ( isset( $this->raw_custom_field['predicate'] ) ) { |
|
| 116 | + if (isset($this->raw_custom_field['predicate'])) { |
|
| 117 | 117 | $this->predicate = $this->raw_custom_field['predicate']; |
| 118 | 118 | } else { |
| 119 | 119 | return; |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | // Extract human readable label. |
| 123 | - $exploded_predicate = explode( '/', $this->predicate ); |
|
| 123 | + $exploded_predicate = explode('/', $this->predicate); |
|
| 124 | 124 | |
| 125 | 125 | // Use the label defined for the property if set, otherwise the last part of the schema.org/xyz predicate. |
| 126 | - $this->label = isset( $this->raw_custom_field['metabox']['label'] ) ? $this->raw_custom_field['metabox']['label'] : end( $exploded_predicate ); |
|
| 126 | + $this->label = isset($this->raw_custom_field['metabox']['label']) ? $this->raw_custom_field['metabox']['label'] : end($exploded_predicate); |
|
| 127 | 127 | |
| 128 | 128 | // Extract field constraints (numerosity, expected type). |
| 129 | 129 | // Default constaints: accept one string.. |
| 130 | - if ( isset( $this->raw_custom_field['type'] ) ) { |
|
| 130 | + if (isset($this->raw_custom_field['type'])) { |
|
| 131 | 131 | $this->expected_wl_type = $this->raw_custom_field['type']; |
| 132 | 132 | } else { |
| 133 | 133 | $this->expected_wl_type = Wordlift_Schema_Service::DATA_TYPE_STRING; |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | $this->cardinality = 1; |
| 137 | - if ( isset( $this->raw_custom_field['constraints'] ) ) { |
|
| 137 | + if (isset($this->raw_custom_field['constraints'])) { |
|
| 138 | 138 | |
| 139 | 139 | $constraints = $this->raw_custom_field['constraints']; |
| 140 | 140 | |
| 141 | 141 | // Extract cardinality. |
| 142 | - if ( isset( $constraints['cardinality'] ) ) { |
|
| 142 | + if (isset($constraints['cardinality'])) { |
|
| 143 | 143 | $this->cardinality = $constraints['cardinality']; |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | // Which type of entity can we accept (e.g. Place, Event, ecc.)? . |
| 147 | - if ( Wordlift_Schema_Service::DATA_TYPE_URI === $this->expected_wl_type && isset( $constraints['uri_type'] ) ) { |
|
| 148 | - $this->expected_uri_type = is_array( $constraints['uri_type'] ) |
|
| 147 | + if (Wordlift_Schema_Service::DATA_TYPE_URI === $this->expected_wl_type && isset($constraints['uri_type'])) { |
|
| 148 | + $this->expected_uri_type = is_array($constraints['uri_type']) |
|
| 149 | 149 | ? $constraints['uri_type'] |
| 150 | - : array( $constraints['uri_type'] ); |
|
| 150 | + : array($constraints['uri_type']); |
|
| 151 | 151 | } |
| 152 | 152 | } |
| 153 | 153 | |
@@ -165,7 +165,7 @@ discard block |
||
| 165 | 165 | */ |
| 166 | 166 | public function html_nonce() { |
| 167 | 167 | |
| 168 | - return wp_nonce_field( 'wordlift_' . $this->meta_name . '_entity_box', 'wordlift_' . $this->meta_name . '_entity_box_nonce', true, false ); |
|
| 168 | + return wp_nonce_field('wordlift_'.$this->meta_name.'_entity_box', 'wordlift_'.$this->meta_name.'_entity_box_nonce', true, false); |
|
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | /** |
@@ -177,15 +177,15 @@ discard block |
||
| 177 | 177 | */ |
| 178 | 178 | public function verify_nonce() { |
| 179 | 179 | |
| 180 | - $nonce_name = 'wordlift_' . $this->meta_name . '_entity_box_nonce'; |
|
| 181 | - $nonce_verify = 'wordlift_' . $this->meta_name . '_entity_box'; |
|
| 180 | + $nonce_name = 'wordlift_'.$this->meta_name.'_entity_box_nonce'; |
|
| 181 | + $nonce_verify = 'wordlift_'.$this->meta_name.'_entity_box'; |
|
| 182 | 182 | |
| 183 | - if ( ! isset( $_POST[ $nonce_name ] ) ) { |
|
| 183 | + if ( ! isset($_POST[$nonce_name])) { |
|
| 184 | 184 | return false; |
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | // Verify that the nonce is valid. |
| 188 | - return wp_verify_nonce( $_POST[ $nonce_name ], $nonce_verify ); |
|
| 188 | + return wp_verify_nonce($_POST[$nonce_name], $nonce_verify); |
|
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | /** |
@@ -197,9 +197,9 @@ discard block |
||
| 197 | 197 | |
| 198 | 198 | // Get the post id and load the data. |
| 199 | 199 | $post_id = $this->post_id; |
| 200 | - $this->data = get_post_meta( $post_id, $this->meta_name ); |
|
| 200 | + $this->data = get_post_meta($post_id, $this->meta_name); |
|
| 201 | 201 | |
| 202 | - $this->log->debug( 'Found ' . count( $this->data ) . " value(s) for meta $this->meta_name, post $post_id." ); |
|
| 202 | + $this->log->debug('Found '.count($this->data)." value(s) for meta $this->meta_name, post $post_id."); |
|
| 203 | 203 | |
| 204 | 204 | } |
| 205 | 205 | |
@@ -213,17 +213,17 @@ discard block |
||
| 213 | 213 | * @param array $values Array of values to be sanitized and then stored into |
| 214 | 214 | * $this->data. |
| 215 | 215 | */ |
| 216 | - public function sanitize_data( $values ) { |
|
| 216 | + public function sanitize_data($values) { |
|
| 217 | 217 | |
| 218 | 218 | $sanitized_data = array(); |
| 219 | 219 | |
| 220 | - if ( ! is_array( $values ) ) { |
|
| 221 | - $values = array( $values ); |
|
| 220 | + if ( ! is_array($values)) { |
|
| 221 | + $values = array($values); |
|
| 222 | 222 | } |
| 223 | 223 | |
| 224 | - foreach ( $values as $value ) { |
|
| 225 | - $sanitized_value = $this->sanitize_data_filter( $value ); |
|
| 226 | - if ( ! is_null( $sanitized_value ) ) { |
|
| 224 | + foreach ($values as $value) { |
|
| 225 | + $sanitized_value = $this->sanitize_data_filter($value); |
|
| 226 | + if ( ! is_null($sanitized_value)) { |
|
| 227 | 227 | $sanitized_data[] = $sanitized_value; |
| 228 | 228 | } |
| 229 | 229 | } |
@@ -241,17 +241,17 @@ discard block |
||
| 241 | 241 | * |
| 242 | 242 | * @return mixed Returns sanitized value, or null. |
| 243 | 243 | */ |
| 244 | - public function sanitize_data_filter( $value ) { |
|
| 244 | + public function sanitize_data_filter($value) { |
|
| 245 | 245 | |
| 246 | 246 | // TODO: all fields should provide their own sanitize which shouldn't |
| 247 | 247 | // be part of a UI class. |
| 248 | 248 | |
| 249 | 249 | // If the field provides its own validation, use it. |
| 250 | - if ( isset( $this->raw_custom_field['sanitize'] ) ) { |
|
| 251 | - return call_user_func( $this->raw_custom_field['sanitize'], $value ); |
|
| 250 | + if (isset($this->raw_custom_field['sanitize'])) { |
|
| 251 | + return call_user_func($this->raw_custom_field['sanitize'], $value); |
|
| 252 | 252 | } |
| 253 | 253 | |
| 254 | - if ( ! is_null( $value ) && '' !== $value ) { // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ . |
|
| 254 | + if ( ! is_null($value) && '' !== $value) { // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ . |
|
| 255 | 255 | return $value; |
| 256 | 256 | } |
| 257 | 257 | |
@@ -265,29 +265,29 @@ discard block |
||
| 265 | 265 | * |
| 266 | 266 | * @param array $values Array of values to be sanitized and then stored into $this->data. |
| 267 | 267 | */ |
| 268 | - public function save_data( $values ) { |
|
| 268 | + public function save_data($values) { |
|
| 269 | 269 | |
| 270 | 270 | // Will sanitize data and store them in $field->data. |
| 271 | - $this->sanitize_data( $values ); |
|
| 271 | + $this->sanitize_data($values); |
|
| 272 | 272 | |
| 273 | 273 | // Bail out, if the post id isn't set in the request or isn't numeric. |
| 274 | 274 | // |
| 275 | 275 | // See https://github.com/insideout10/wordlift-plugin/issues/665. |
| 276 | - if ( ! isset( $_POST['post_ID'] ) || ! is_numeric( $_POST['post_ID'] ) ) { |
|
| 276 | + if ( ! isset($_POST['post_ID']) || ! is_numeric($_POST['post_ID'])) { |
|
| 277 | 277 | return; |
| 278 | 278 | } |
| 279 | 279 | |
| 280 | - $entity_id = intval( $_POST['post_ID'] ); |
|
| 280 | + $entity_id = intval($_POST['post_ID']); |
|
| 281 | 281 | |
| 282 | 282 | // Take away old values. |
| 283 | - delete_post_meta( $entity_id, $this->meta_name ); |
|
| 283 | + delete_post_meta($entity_id, $this->meta_name); |
|
| 284 | 284 | |
| 285 | 285 | // insert new values, respecting cardinality. |
| 286 | - $single = ( 1 === $this->cardinality ); |
|
| 287 | - foreach ( $this->data as $value ) { |
|
| 288 | - $this->log->trace( "Saving $value to $this->meta_name for entity $entity_id..." ); |
|
| 289 | - $meta_id = add_post_meta( $entity_id, $this->meta_name, $value, $single ); |
|
| 290 | - $this->log->debug( "$value to $this->meta_name for entity $entity_id saved with id $meta_id." ); |
|
| 286 | + $single = (1 === $this->cardinality); |
|
| 287 | + foreach ($this->data as $value) { |
|
| 288 | + $this->log->trace("Saving $value to $this->meta_name for entity $entity_id..."); |
|
| 289 | + $meta_id = add_post_meta($entity_id, $this->meta_name, $value, $single); |
|
| 290 | + $this->log->debug("$value to $this->meta_name for entity $entity_id saved with id $meta_id."); |
|
| 291 | 291 | } |
| 292 | 292 | } |
| 293 | 293 | |
@@ -332,17 +332,17 @@ discard block |
||
| 332 | 332 | |
| 333 | 333 | // print data loaded from DB. |
| 334 | 334 | $count = 0; |
| 335 | - $html .= $this->get_stored_values_html( $count ); |
|
| 335 | + $html .= $this->get_stored_values_html($count); |
|
| 336 | 336 | |
| 337 | 337 | // Print the empty <input> to add new values. |
| 338 | - if ( 0 === $count ) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings. |
|
| 339 | - $this->log->debug( 'Going to print an empty HTML input...' ); |
|
| 340 | - $html .= $this->html_input( '' ); // Will print an empty <input>. |
|
| 341 | - $count ++; |
|
| 338 | + if (0 === $count) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings. |
|
| 339 | + $this->log->debug('Going to print an empty HTML input...'); |
|
| 340 | + $html .= $this->html_input(''); // Will print an empty <input>. |
|
| 341 | + $count++; |
|
| 342 | 342 | } |
| 343 | 343 | |
| 344 | 344 | // If cardinality allows it, print button to add new values. |
| 345 | - $html .= $this->get_add_button_html( $count ); |
|
| 345 | + $html .= $this->get_add_button_html($count); |
|
| 346 | 346 | |
| 347 | 347 | // Close the HTML wrapper. |
| 348 | 348 | $html .= $this->html_wrapper_close(); |
@@ -370,29 +370,29 @@ discard block |
||
| 370 | 370 | * |
| 371 | 371 | * @return string The html fragment. |
| 372 | 372 | */ |
| 373 | - protected function get_stored_values_html( &$count ) { |
|
| 373 | + protected function get_stored_values_html(&$count) { |
|
| 374 | 374 | |
| 375 | 375 | $html = ''; |
| 376 | 376 | |
| 377 | 377 | // print data loaded from DB. |
| 378 | 378 | $count = 0; |
| 379 | - if ( $this->data ) { |
|
| 380 | - foreach ( $this->data as $value ) { |
|
| 381 | - if ( $count < $this->cardinality ) { |
|
| 382 | - $this->log->debug( "Going to print an HTML input #$count with $value..." ); |
|
| 383 | - $fragment = $this->html_input( $value ); |
|
| 379 | + if ($this->data) { |
|
| 380 | + foreach ($this->data as $value) { |
|
| 381 | + if ($count < $this->cardinality) { |
|
| 382 | + $this->log->debug("Going to print an HTML input #$count with $value..."); |
|
| 383 | + $fragment = $this->html_input($value); |
|
| 384 | 384 | |
| 385 | 385 | // If the fragment is empty, continue to the next one. This is necessary because the |
| 386 | 386 | // metabox may reference an invalid value which would cause the metabox not to print, |
| 387 | 387 | // returning an empty html fragment. |
| 388 | 388 | // |
| 389 | 389 | // See https://github.com/insideout10/wordlift-plugin/issues/818 |
| 390 | - if ( '' === $fragment ) { |
|
| 390 | + if ('' === $fragment) { |
|
| 391 | 391 | continue; |
| 392 | 392 | } |
| 393 | 393 | |
| 394 | 394 | $html .= $fragment; |
| 395 | - $count ++; |
|
| 395 | + $count++; |
|
| 396 | 396 | } |
| 397 | 397 | } |
| 398 | 398 | } |
@@ -412,11 +412,11 @@ discard block |
||
| 412 | 412 | * |
| 413 | 413 | * @return string The add button html code. |
| 414 | 414 | */ |
| 415 | - protected function get_add_button_html( $count ) { |
|
| 415 | + protected function get_add_button_html($count) { |
|
| 416 | 416 | |
| 417 | 417 | // If cardinality allows it, print button to add new values. |
| 418 | - if ( $count < $this->cardinality ) { |
|
| 419 | - return '<button class="button wl-add-input wl-button" type="button">' . esc_html__( 'Add' ) . '</button>'; |
|
| 418 | + if ($count < $this->cardinality) { |
|
| 419 | + return '<button class="button wl-add-input wl-button" type="button">'.esc_html__('Add').'</button>'; |
|
| 420 | 420 | } |
| 421 | 421 | |
| 422 | 422 | // Return an empty string. |
@@ -430,20 +430,20 @@ discard block |
||
| 430 | 430 | * |
| 431 | 431 | * @return string The html code fragment. |
| 432 | 432 | */ |
| 433 | - public function html_input( $value ) { |
|
| 433 | + public function html_input($value) { |
|
| 434 | 434 | @ob_start(); |
| 435 | 435 | ?> |
| 436 | 436 | <div class="wl-input-wrapper"> |
| 437 | 437 | <input |
| 438 | 438 | type="text" |
| 439 | - id="<?php echo esc_attr( $this->meta_name ); ?>" |
|
| 439 | + id="<?php echo esc_attr($this->meta_name); ?>" |
|
| 440 | 440 | name="wl_metaboxes[<?php echo $this->meta_name ?>][]" |
| 441 | - value="<?php echo esc_attr( $value ); ?>" |
|
| 441 | + value="<?php echo esc_attr($value); ?>" |
|
| 442 | 442 | style="width:88%" |
| 443 | 443 | /> |
| 444 | 444 | |
| 445 | 445 | <button class="button wl-remove-input wl-button" type="button"> |
| 446 | - <?php esc_html_e( 'Remove', 'wordlift' ); ?> |
|
| 446 | + <?php esc_html_e('Remove', 'wordlift'); ?> |
|
| 447 | 447 | </button> |
| 448 | 448 | </div> |
| 449 | 449 | <?php |
@@ -16,146 +16,146 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | class WL_Metabox_Field_uri extends WL_Metabox_Field { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Only accept URIs or local entity IDs. |
|
| 21 | - * Build new entity if the user inputted a name that is not present in DB. |
|
| 22 | - * |
|
| 23 | - * @param mixed $value The value to sanitize. |
|
| 24 | - * |
|
| 25 | - * @return int|mixed|WP_Error |
|
| 26 | - */ |
|
| 27 | - public function sanitize_data_filter( $value ) { |
|
| 28 | - |
|
| 29 | - if ( empty( $value ) ) { |
|
| 30 | - return null; |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - // Check that the inserted URI, ID or name does not point to a saved |
|
| 34 | - // entity or when the editor types a string in the input box, we try to |
|
| 35 | - // find an entity with that title and, if not found, we create that entity. |
|
| 36 | - $absent_from_db = is_numeric( $value ) |
|
| 37 | - ? is_null( get_post( $value ) ) |
|
| 38 | - : ! $this->exists( $value ); |
|
| 39 | - |
|
| 40 | - // Is it an URI? |
|
| 41 | - $name_is_uri = strpos( $value, 'http' ) === 0; |
|
| 42 | - |
|
| 43 | - // We create a new entity only if the entity is not present in the DB. |
|
| 44 | - // In the case of an external uri, we just save the uri. |
|
| 45 | - if ( $absent_from_db && ! $name_is_uri ) { |
|
| 46 | - |
|
| 47 | - // ...we create a new entity! |
|
| 48 | - $new_entity_id = wp_insert_post( array( |
|
| 49 | - 'post_status' => 'publish', |
|
| 50 | - 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 51 | - 'post_title' => $value, |
|
| 52 | - ) ); |
|
| 53 | - |
|
| 54 | - $type = 'http://schema.org/' . ( isset( $this->expected_uri_type ) ? $this->expected_uri_type[0] : 'Thing' ); |
|
| 55 | - |
|
| 56 | - wl_set_entity_main_type( $new_entity_id, $type ); |
|
| 57 | - |
|
| 58 | - // Build uri for this entity. |
|
| 59 | - $new_uri = wl_build_entity_uri( $new_entity_id ); |
|
| 60 | - wl_set_entity_uri( $new_entity_id, $new_uri ); |
|
| 61 | - |
|
| 62 | - Wordlift_Linked_Data_Service::get_instance()->push( $new_entity_id ); |
|
| 63 | - |
|
| 64 | - // Update the value that will be saved as meta. |
|
| 65 | - $value = $new_entity_id; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - return $value; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Check whether an entity exists given a value. |
|
| 73 | - * |
|
| 74 | - * @since 3.15.0 |
|
| 75 | - * |
|
| 76 | - * @param string $value An entity URI or a title string.. |
|
| 77 | - * |
|
| 78 | - * @return bool True if the entity exists otherwise false. |
|
| 79 | - */ |
|
| 80 | - private function exists( $value ) { |
|
| 81 | - |
|
| 82 | - // When the editor types a string in the input box, we try to find |
|
| 83 | - // an entity with that title and, if not found, we create that entity. |
|
| 84 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 85 | - |
|
| 86 | - // Try looking for an entity by URI. |
|
| 87 | - $found_by_uri = null !== $entity_service->get_entity_post_by_uri( $value ); |
|
| 88 | - |
|
| 89 | - // Return true if found. |
|
| 90 | - if ( $found_by_uri ) { |
|
| 91 | - $this->log->debug( "Found entity for $value." ); |
|
| 92 | - |
|
| 93 | - return true; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - // Try looking for an entity by title, get any potential candidate. |
|
| 97 | - $candidate = get_page_by_title( $value, OBJECT, Wordlift_Entity_Service::valid_entity_post_types() ); |
|
| 98 | - |
|
| 99 | - // If a candidate has been found and it's an entity. |
|
| 100 | - return null !== $candidate && $entity_service->is_entity( $candidate->ID ); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @inheritdoc |
|
| 105 | - */ |
|
| 106 | - public function html_wrapper_open() { |
|
| 107 | - |
|
| 108 | - // The containing <div> contains info on cardinality and expected types. |
|
| 109 | - $html = "<div class='wl-field' data-cardinality='$this->cardinality'"; |
|
| 110 | - |
|
| 111 | - if ( isset( $this->expected_uri_type ) && ! is_null( $this->expected_uri_type ) ) { |
|
| 112 | - |
|
| 113 | - if ( is_array( $this->expected_uri_type ) ) { |
|
| 114 | - $html .= " data-expected-types='" . implode( ',', $this->expected_uri_type ) . "'"; |
|
| 115 | - } else { |
|
| 116 | - $html .= " data-expected-types='$this->expected_uri_type'"; |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - $html .= '>'; |
|
| 121 | - |
|
| 122 | - return $html; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * @inheritdoc |
|
| 127 | - */ |
|
| 128 | - public function html_input( $default_entity_identifier ) { |
|
| 129 | - |
|
| 130 | - if ( empty( $default_entity_identifier ) ) { |
|
| 131 | - $entity = null; |
|
| 132 | - } elseif ( is_numeric( $default_entity_identifier ) ) { |
|
| 133 | - $entity = get_post( $default_entity_identifier ); |
|
| 134 | - } else { |
|
| 135 | - // @todo: we cannot be so sure this is a URI. |
|
| 136 | - // It is an URI |
|
| 137 | - $entity = Wordlift_Entity_Service |
|
| 138 | - ::get_instance() |
|
| 139 | - ->get_entity_post_by_uri( $default_entity_identifier ); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - // Bail out if an entity id has been provided by the entity is not found. |
|
| 143 | - // |
|
| 144 | - // See https://github.com/insideout10/wordlift-plugin/issues/818 |
|
| 145 | - if ( ! empty( $default_entity_identifier ) && is_null( $entity ) ) { |
|
| 146 | - return ''; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - $label = is_null( $entity ) ? '' : $entity->post_title; |
|
| 150 | - $value = is_null( $entity ) ? '' : $entity->ID; |
|
| 151 | - |
|
| 152 | - // Write saved value in page |
|
| 153 | - // The <input> tags host the meta value. |
|
| 154 | - // The visible <input> has the human readable value (i.e. entity name or uri) |
|
| 155 | - // and is accompained by an hidden <input> tag, passed to the server, |
|
| 156 | - // that contains the raw value (i.e. the uri or entity id). |
|
| 157 | - @ob_start(); |
|
| 158 | - ?> |
|
| 19 | + /** |
|
| 20 | + * Only accept URIs or local entity IDs. |
|
| 21 | + * Build new entity if the user inputted a name that is not present in DB. |
|
| 22 | + * |
|
| 23 | + * @param mixed $value The value to sanitize. |
|
| 24 | + * |
|
| 25 | + * @return int|mixed|WP_Error |
|
| 26 | + */ |
|
| 27 | + public function sanitize_data_filter( $value ) { |
|
| 28 | + |
|
| 29 | + if ( empty( $value ) ) { |
|
| 30 | + return null; |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + // Check that the inserted URI, ID or name does not point to a saved |
|
| 34 | + // entity or when the editor types a string in the input box, we try to |
|
| 35 | + // find an entity with that title and, if not found, we create that entity. |
|
| 36 | + $absent_from_db = is_numeric( $value ) |
|
| 37 | + ? is_null( get_post( $value ) ) |
|
| 38 | + : ! $this->exists( $value ); |
|
| 39 | + |
|
| 40 | + // Is it an URI? |
|
| 41 | + $name_is_uri = strpos( $value, 'http' ) === 0; |
|
| 42 | + |
|
| 43 | + // We create a new entity only if the entity is not present in the DB. |
|
| 44 | + // In the case of an external uri, we just save the uri. |
|
| 45 | + if ( $absent_from_db && ! $name_is_uri ) { |
|
| 46 | + |
|
| 47 | + // ...we create a new entity! |
|
| 48 | + $new_entity_id = wp_insert_post( array( |
|
| 49 | + 'post_status' => 'publish', |
|
| 50 | + 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 51 | + 'post_title' => $value, |
|
| 52 | + ) ); |
|
| 53 | + |
|
| 54 | + $type = 'http://schema.org/' . ( isset( $this->expected_uri_type ) ? $this->expected_uri_type[0] : 'Thing' ); |
|
| 55 | + |
|
| 56 | + wl_set_entity_main_type( $new_entity_id, $type ); |
|
| 57 | + |
|
| 58 | + // Build uri for this entity. |
|
| 59 | + $new_uri = wl_build_entity_uri( $new_entity_id ); |
|
| 60 | + wl_set_entity_uri( $new_entity_id, $new_uri ); |
|
| 61 | + |
|
| 62 | + Wordlift_Linked_Data_Service::get_instance()->push( $new_entity_id ); |
|
| 63 | + |
|
| 64 | + // Update the value that will be saved as meta. |
|
| 65 | + $value = $new_entity_id; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + return $value; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Check whether an entity exists given a value. |
|
| 73 | + * |
|
| 74 | + * @since 3.15.0 |
|
| 75 | + * |
|
| 76 | + * @param string $value An entity URI or a title string.. |
|
| 77 | + * |
|
| 78 | + * @return bool True if the entity exists otherwise false. |
|
| 79 | + */ |
|
| 80 | + private function exists( $value ) { |
|
| 81 | + |
|
| 82 | + // When the editor types a string in the input box, we try to find |
|
| 83 | + // an entity with that title and, if not found, we create that entity. |
|
| 84 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 85 | + |
|
| 86 | + // Try looking for an entity by URI. |
|
| 87 | + $found_by_uri = null !== $entity_service->get_entity_post_by_uri( $value ); |
|
| 88 | + |
|
| 89 | + // Return true if found. |
|
| 90 | + if ( $found_by_uri ) { |
|
| 91 | + $this->log->debug( "Found entity for $value." ); |
|
| 92 | + |
|
| 93 | + return true; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + // Try looking for an entity by title, get any potential candidate. |
|
| 97 | + $candidate = get_page_by_title( $value, OBJECT, Wordlift_Entity_Service::valid_entity_post_types() ); |
|
| 98 | + |
|
| 99 | + // If a candidate has been found and it's an entity. |
|
| 100 | + return null !== $candidate && $entity_service->is_entity( $candidate->ID ); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @inheritdoc |
|
| 105 | + */ |
|
| 106 | + public function html_wrapper_open() { |
|
| 107 | + |
|
| 108 | + // The containing <div> contains info on cardinality and expected types. |
|
| 109 | + $html = "<div class='wl-field' data-cardinality='$this->cardinality'"; |
|
| 110 | + |
|
| 111 | + if ( isset( $this->expected_uri_type ) && ! is_null( $this->expected_uri_type ) ) { |
|
| 112 | + |
|
| 113 | + if ( is_array( $this->expected_uri_type ) ) { |
|
| 114 | + $html .= " data-expected-types='" . implode( ',', $this->expected_uri_type ) . "'"; |
|
| 115 | + } else { |
|
| 116 | + $html .= " data-expected-types='$this->expected_uri_type'"; |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + $html .= '>'; |
|
| 121 | + |
|
| 122 | + return $html; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * @inheritdoc |
|
| 127 | + */ |
|
| 128 | + public function html_input( $default_entity_identifier ) { |
|
| 129 | + |
|
| 130 | + if ( empty( $default_entity_identifier ) ) { |
|
| 131 | + $entity = null; |
|
| 132 | + } elseif ( is_numeric( $default_entity_identifier ) ) { |
|
| 133 | + $entity = get_post( $default_entity_identifier ); |
|
| 134 | + } else { |
|
| 135 | + // @todo: we cannot be so sure this is a URI. |
|
| 136 | + // It is an URI |
|
| 137 | + $entity = Wordlift_Entity_Service |
|
| 138 | + ::get_instance() |
|
| 139 | + ->get_entity_post_by_uri( $default_entity_identifier ); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + // Bail out if an entity id has been provided by the entity is not found. |
|
| 143 | + // |
|
| 144 | + // See https://github.com/insideout10/wordlift-plugin/issues/818 |
|
| 145 | + if ( ! empty( $default_entity_identifier ) && is_null( $entity ) ) { |
|
| 146 | + return ''; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + $label = is_null( $entity ) ? '' : $entity->post_title; |
|
| 150 | + $value = is_null( $entity ) ? '' : $entity->ID; |
|
| 151 | + |
|
| 152 | + // Write saved value in page |
|
| 153 | + // The <input> tags host the meta value. |
|
| 154 | + // The visible <input> has the human readable value (i.e. entity name or uri) |
|
| 155 | + // and is accompained by an hidden <input> tag, passed to the server, |
|
| 156 | + // that contains the raw value (i.e. the uri or entity id). |
|
| 157 | + @ob_start(); |
|
| 158 | + ?> |
|
| 159 | 159 | <div class="wl-input-wrapper wl-autocomplete-wrapper"> |
| 160 | 160 | <input |
| 161 | 161 | type="text" |
@@ -177,9 +177,9 @@ discard block |
||
| 177 | 177 | <div class="wl-input-notice"></div> |
| 178 | 178 | </div> |
| 179 | 179 | <?php |
| 180 | - $html = ob_get_clean(); |
|
| 180 | + $html = ob_get_clean(); |
|
| 181 | 181 | |
| 182 | - return $html; |
|
| 183 | - } |
|
| 182 | + return $html; |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | 185 | } |
@@ -24,42 +24,42 @@ discard block |
||
| 24 | 24 | * |
| 25 | 25 | * @return int|mixed|WP_Error |
| 26 | 26 | */ |
| 27 | - public function sanitize_data_filter( $value ) { |
|
| 27 | + public function sanitize_data_filter($value) { |
|
| 28 | 28 | |
| 29 | - if ( empty( $value ) ) { |
|
| 29 | + if (empty($value)) { |
|
| 30 | 30 | return null; |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | // Check that the inserted URI, ID or name does not point to a saved |
| 34 | 34 | // entity or when the editor types a string in the input box, we try to |
| 35 | 35 | // find an entity with that title and, if not found, we create that entity. |
| 36 | - $absent_from_db = is_numeric( $value ) |
|
| 37 | - ? is_null( get_post( $value ) ) |
|
| 38 | - : ! $this->exists( $value ); |
|
| 36 | + $absent_from_db = is_numeric($value) |
|
| 37 | + ? is_null(get_post($value)) |
|
| 38 | + : ! $this->exists($value); |
|
| 39 | 39 | |
| 40 | 40 | // Is it an URI? |
| 41 | - $name_is_uri = strpos( $value, 'http' ) === 0; |
|
| 41 | + $name_is_uri = strpos($value, 'http') === 0; |
|
| 42 | 42 | |
| 43 | 43 | // We create a new entity only if the entity is not present in the DB. |
| 44 | 44 | // In the case of an external uri, we just save the uri. |
| 45 | - if ( $absent_from_db && ! $name_is_uri ) { |
|
| 45 | + if ($absent_from_db && ! $name_is_uri) { |
|
| 46 | 46 | |
| 47 | 47 | // ...we create a new entity! |
| 48 | - $new_entity_id = wp_insert_post( array( |
|
| 48 | + $new_entity_id = wp_insert_post(array( |
|
| 49 | 49 | 'post_status' => 'publish', |
| 50 | 50 | 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
| 51 | 51 | 'post_title' => $value, |
| 52 | - ) ); |
|
| 52 | + )); |
|
| 53 | 53 | |
| 54 | - $type = 'http://schema.org/' . ( isset( $this->expected_uri_type ) ? $this->expected_uri_type[0] : 'Thing' ); |
|
| 54 | + $type = 'http://schema.org/'.(isset($this->expected_uri_type) ? $this->expected_uri_type[0] : 'Thing'); |
|
| 55 | 55 | |
| 56 | - wl_set_entity_main_type( $new_entity_id, $type ); |
|
| 56 | + wl_set_entity_main_type($new_entity_id, $type); |
|
| 57 | 57 | |
| 58 | 58 | // Build uri for this entity. |
| 59 | - $new_uri = wl_build_entity_uri( $new_entity_id ); |
|
| 60 | - wl_set_entity_uri( $new_entity_id, $new_uri ); |
|
| 59 | + $new_uri = wl_build_entity_uri($new_entity_id); |
|
| 60 | + wl_set_entity_uri($new_entity_id, $new_uri); |
|
| 61 | 61 | |
| 62 | - Wordlift_Linked_Data_Service::get_instance()->push( $new_entity_id ); |
|
| 62 | + Wordlift_Linked_Data_Service::get_instance()->push($new_entity_id); |
|
| 63 | 63 | |
| 64 | 64 | // Update the value that will be saved as meta. |
| 65 | 65 | $value = $new_entity_id; |
@@ -77,27 +77,27 @@ discard block |
||
| 77 | 77 | * |
| 78 | 78 | * @return bool True if the entity exists otherwise false. |
| 79 | 79 | */ |
| 80 | - private function exists( $value ) { |
|
| 80 | + private function exists($value) { |
|
| 81 | 81 | |
| 82 | 82 | // When the editor types a string in the input box, we try to find |
| 83 | 83 | // an entity with that title and, if not found, we create that entity. |
| 84 | 84 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 85 | 85 | |
| 86 | 86 | // Try looking for an entity by URI. |
| 87 | - $found_by_uri = null !== $entity_service->get_entity_post_by_uri( $value ); |
|
| 87 | + $found_by_uri = null !== $entity_service->get_entity_post_by_uri($value); |
|
| 88 | 88 | |
| 89 | 89 | // Return true if found. |
| 90 | - if ( $found_by_uri ) { |
|
| 91 | - $this->log->debug( "Found entity for $value." ); |
|
| 90 | + if ($found_by_uri) { |
|
| 91 | + $this->log->debug("Found entity for $value."); |
|
| 92 | 92 | |
| 93 | 93 | return true; |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | // Try looking for an entity by title, get any potential candidate. |
| 97 | - $candidate = get_page_by_title( $value, OBJECT, Wordlift_Entity_Service::valid_entity_post_types() ); |
|
| 97 | + $candidate = get_page_by_title($value, OBJECT, Wordlift_Entity_Service::valid_entity_post_types()); |
|
| 98 | 98 | |
| 99 | 99 | // If a candidate has been found and it's an entity. |
| 100 | - return null !== $candidate && $entity_service->is_entity( $candidate->ID ); |
|
| 100 | + return null !== $candidate && $entity_service->is_entity($candidate->ID); |
|
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | /** |
@@ -108,10 +108,10 @@ discard block |
||
| 108 | 108 | // The containing <div> contains info on cardinality and expected types. |
| 109 | 109 | $html = "<div class='wl-field' data-cardinality='$this->cardinality'"; |
| 110 | 110 | |
| 111 | - if ( isset( $this->expected_uri_type ) && ! is_null( $this->expected_uri_type ) ) { |
|
| 111 | + if (isset($this->expected_uri_type) && ! is_null($this->expected_uri_type)) { |
|
| 112 | 112 | |
| 113 | - if ( is_array( $this->expected_uri_type ) ) { |
|
| 114 | - $html .= " data-expected-types='" . implode( ',', $this->expected_uri_type ) . "'"; |
|
| 113 | + if (is_array($this->expected_uri_type)) { |
|
| 114 | + $html .= " data-expected-types='".implode(',', $this->expected_uri_type)."'"; |
|
| 115 | 115 | } else { |
| 116 | 116 | $html .= " data-expected-types='$this->expected_uri_type'"; |
| 117 | 117 | } |
@@ -125,29 +125,29 @@ discard block |
||
| 125 | 125 | /** |
| 126 | 126 | * @inheritdoc |
| 127 | 127 | */ |
| 128 | - public function html_input( $default_entity_identifier ) { |
|
| 128 | + public function html_input($default_entity_identifier) { |
|
| 129 | 129 | |
| 130 | - if ( empty( $default_entity_identifier ) ) { |
|
| 130 | + if (empty($default_entity_identifier)) { |
|
| 131 | 131 | $entity = null; |
| 132 | - } elseif ( is_numeric( $default_entity_identifier ) ) { |
|
| 133 | - $entity = get_post( $default_entity_identifier ); |
|
| 132 | + } elseif (is_numeric($default_entity_identifier)) { |
|
| 133 | + $entity = get_post($default_entity_identifier); |
|
| 134 | 134 | } else { |
| 135 | 135 | // @todo: we cannot be so sure this is a URI. |
| 136 | 136 | // It is an URI |
| 137 | 137 | $entity = Wordlift_Entity_Service |
| 138 | 138 | ::get_instance() |
| 139 | - ->get_entity_post_by_uri( $default_entity_identifier ); |
|
| 139 | + ->get_entity_post_by_uri($default_entity_identifier); |
|
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | // Bail out if an entity id has been provided by the entity is not found. |
| 143 | 143 | // |
| 144 | 144 | // See https://github.com/insideout10/wordlift-plugin/issues/818 |
| 145 | - if ( ! empty( $default_entity_identifier ) && is_null( $entity ) ) { |
|
| 145 | + if ( ! empty($default_entity_identifier) && is_null($entity)) { |
|
| 146 | 146 | return ''; |
| 147 | 147 | } |
| 148 | 148 | |
| 149 | - $label = is_null( $entity ) ? '' : $entity->post_title; |
|
| 150 | - $value = is_null( $entity ) ? '' : $entity->ID; |
|
| 149 | + $label = is_null($entity) ? '' : $entity->post_title; |
|
| 150 | + $value = is_null($entity) ? '' : $entity->ID; |
|
| 151 | 151 | |
| 152 | 152 | // Write saved value in page |
| 153 | 153 | // The <input> tags host the meta value. |
@@ -159,19 +159,19 @@ discard block |
||
| 159 | 159 | <div class="wl-input-wrapper wl-autocomplete-wrapper"> |
| 160 | 160 | <input |
| 161 | 161 | type="text" |
| 162 | - class="<?php echo esc_attr( $this->meta_name ); ?> wl-autocomplete" |
|
| 163 | - value="<?php echo esc_attr( $label ); ?>" |
|
| 162 | + class="<?php echo esc_attr($this->meta_name); ?> wl-autocomplete" |
|
| 163 | + value="<?php echo esc_attr($label); ?>" |
|
| 164 | 164 | style="width:88%" |
| 165 | 165 | /> |
| 166 | 166 | <input |
| 167 | 167 | type="hidden" |
| 168 | - class="<?php echo esc_attr( $this->meta_name ); ?>" |
|
| 168 | + class="<?php echo esc_attr($this->meta_name); ?>" |
|
| 169 | 169 | name="wl_metaboxes[<?php echo $this->meta_name ?>][]" |
| 170 | - value="<?php echo esc_attr( $value ); ?>" |
|
| 170 | + value="<?php echo esc_attr($value); ?>" |
|
| 171 | 171 | /> |
| 172 | 172 | |
| 173 | 173 | <button class="button wl-remove-input wl-button" type="button"> |
| 174 | - <?php esc_html_e( 'Remove', 'wordlift' ); ?> |
|
| 174 | + <?php esc_html_e('Remove', 'wordlift'); ?> |
|
| 175 | 175 | </button> |
| 176 | 176 | |
| 177 | 177 | <div class="wl-input-notice"></div> |
@@ -9,293 +9,293 @@ |
||
| 9 | 9 | |
| 10 | 10 | abstract class Wordlift_Async_Task { |
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * Constant identifier for a task that should be available to logged-in users |
|
| 14 | - * |
|
| 15 | - * See constructor documentation for more details. |
|
| 16 | - */ |
|
| 17 | - const LOGGED_IN = 1; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Constant identifier for a task that should be available to logged-out users |
|
| 21 | - * |
|
| 22 | - * See constructor documentation for more details. |
|
| 23 | - */ |
|
| 24 | - const LOGGED_OUT = 2; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Constant identifier for a task that should be available to all users regardless of auth status |
|
| 28 | - * |
|
| 29 | - * See constructor documentation for more details. |
|
| 30 | - */ |
|
| 31 | - const BOTH = 3; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * This is the argument count for the main action set in the constructor. It |
|
| 35 | - * is set to an arbitrarily high value of twenty, but can be overridden if |
|
| 36 | - * necessary |
|
| 37 | - * |
|
| 38 | - * @var int |
|
| 39 | - */ |
|
| 40 | - protected $argument_count = 20; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Priority to fire intermediate action. |
|
| 44 | - * |
|
| 45 | - * @var int |
|
| 46 | - */ |
|
| 47 | - protected $priority = 10; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - protected $action; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @var array |
|
| 56 | - */ |
|
| 57 | - protected $_body_data; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * A {@link Wordlift_Log_Service} instance. |
|
| 61 | - * |
|
| 62 | - * @since 3.15.0 |
|
| 63 | - * @access private |
|
| 64 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 65 | - */ |
|
| 66 | - private $log; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Constructor to wire up the necessary actions |
|
| 70 | - * |
|
| 71 | - * Which hooks the asynchronous postback happens on can be set by the |
|
| 72 | - * $auth_level parameter. There are essentially three options: logged in users |
|
| 73 | - * only, logged out users only, or both. Set this when you instantiate an |
|
| 74 | - * object by using one of the three class constants to do so: |
|
| 75 | - * - LOGGED_IN |
|
| 76 | - * - LOGGED_OUT |
|
| 77 | - * - BOTH |
|
| 78 | - * $auth_level defaults to BOTH |
|
| 79 | - * |
|
| 80 | - * @throws Exception If the class' $action value hasn't been set |
|
| 81 | - * |
|
| 82 | - * @param int $auth_level The authentication level to use (see above) |
|
| 83 | - */ |
|
| 84 | - public function __construct( $auth_level = self::BOTH ) { |
|
| 85 | - |
|
| 86 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 87 | - |
|
| 88 | - if ( empty( $this->action ) ) { |
|
| 89 | - throw new Exception( 'Action not defined for class ' . __CLASS__ ); |
|
| 90 | - } |
|
| 91 | - add_action( $this->action, array( |
|
| 92 | - $this, |
|
| 93 | - 'launch', |
|
| 94 | - ), (int) $this->priority, (int) $this->argument_count ); |
|
| 95 | - if ( $auth_level & self::LOGGED_IN ) { |
|
| 96 | - add_action( "admin_post_wl_async_$this->action", array( |
|
| 97 | - $this, |
|
| 98 | - 'handle_postback', |
|
| 99 | - ) ); |
|
| 100 | - } |
|
| 101 | - if ( $auth_level & self::LOGGED_OUT ) { |
|
| 102 | - add_action( "admin_post_nopriv_wl_async_$this->action", array( |
|
| 103 | - $this, |
|
| 104 | - 'handle_postback', |
|
| 105 | - ) ); |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Add the shutdown action for launching the real postback if we don't |
|
| 111 | - * get an exception thrown by prepare_data(). |
|
| 112 | - * |
|
| 113 | - * @uses func_get_args() To grab any arguments passed by the action |
|
| 114 | - */ |
|
| 115 | - public function launch() { |
|
| 116 | - $data = func_get_args(); |
|
| 117 | - try { |
|
| 118 | - $data = $this->prepare_data( $data ); |
|
| 119 | - } catch ( Exception $e ) { |
|
| 120 | - return; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - $data['action'] = "wl_async_$this->action"; |
|
| 124 | - $data['_nonce'] = $this->create_async_nonce(); |
|
| 125 | - |
|
| 126 | - $this->_body_data = $data; |
|
| 127 | - |
|
| 128 | - if ( ! has_action( 'shutdown', array( |
|
| 129 | - $this, |
|
| 130 | - 'launch_on_shutdown', |
|
| 131 | - ) ) |
|
| 132 | - ) { |
|
| 133 | - add_action( 'shutdown', array( $this, 'launch_on_shutdown' ) ); |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Launch the request on the WordPress shutdown hook |
|
| 139 | - * |
|
| 140 | - * On VIP we got into data races due to the postback sometimes completing |
|
| 141 | - * faster than the data could propogate to the database server cluster. |
|
| 142 | - * This made WordPress get empty data sets from the database without |
|
| 143 | - * failing. On their advice, we're moving the actual firing of the async |
|
| 144 | - * postback to the shutdown hook. Supposedly that will ensure that the |
|
| 145 | - * data at least has time to get into the object cache. |
|
| 146 | - * |
|
| 147 | - * @uses $_COOKIE To send a cookie header for async postback |
|
| 148 | - * @uses apply_filters() |
|
| 149 | - * @uses admin_url() |
|
| 150 | - * @uses wp_remote_post() |
|
| 151 | - */ |
|
| 152 | - public function launch_on_shutdown() { |
|
| 153 | - |
|
| 154 | - $this->log->debug( "Launching Async Task [ action :: $this->action ]..." ); |
|
| 155 | - |
|
| 156 | - if ( ! empty( $this->_body_data ) ) { |
|
| 157 | - $cookies = array(); |
|
| 158 | - foreach ( $_COOKIE as $name => $value ) { |
|
| 159 | - $cookies[] = "$name=" . urlencode( is_array( $value ) ? serialize( $value ) : $value ); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - $request_args = array( |
|
| 163 | - 'timeout' => 0.01, |
|
| 164 | - 'blocking' => false, |
|
| 165 | - 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), |
|
| 166 | - 'body' => $this->_body_data, |
|
| 167 | - 'headers' => array( |
|
| 168 | - 'cookie' => implode( '; ', $cookies ), |
|
| 169 | - ), |
|
| 170 | - ); |
|
| 171 | - |
|
| 172 | - $url = get_site_url( null, 'wl-api' ); |
|
| 173 | - |
|
| 174 | - $this->log->debug( "Posting URL $url..." ); |
|
| 175 | - |
|
| 176 | - $result = wp_remote_post( $url, $request_args ); |
|
| 177 | - |
|
| 178 | - if ( is_wp_error( $result ) ) { |
|
| 179 | - $this->log->error( 'Posting URL returned an error: ' . $result->get_error_message() ); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Verify the postback is valid, then fire any scheduled events. |
|
| 187 | - * |
|
| 188 | - * @uses $_POST['_nonce'] |
|
| 189 | - * @uses is_user_logged_in() |
|
| 190 | - * @uses add_filter() |
|
| 191 | - * @uses wp_die() |
|
| 192 | - */ |
|
| 193 | - public function handle_postback() { |
|
| 194 | - if ( isset( $_POST['_nonce'] ) && $this->verify_async_nonce( $_POST['_nonce'] ) ) { |
|
| 195 | - if ( ! is_user_logged_in() ) { |
|
| 196 | - $this->action = "nopriv_$this->action"; |
|
| 197 | - } |
|
| 198 | - $this->run_action(); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - add_filter( 'wp_die_handler', function () { |
|
| 202 | - die(); |
|
| 203 | - } ); |
|
| 204 | - wp_die(); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Create a random, one time use token. |
|
| 209 | - * |
|
| 210 | - * Based entirely on wp_create_nonce() but does not tie the nonce to the |
|
| 211 | - * current logged-in user. |
|
| 212 | - * |
|
| 213 | - * @uses wp_nonce_tick() |
|
| 214 | - * @uses wp_hash() |
|
| 215 | - * |
|
| 216 | - * @return string The one-time use token |
|
| 217 | - */ |
|
| 218 | - protected function create_async_nonce() { |
|
| 219 | - $action = $this->get_nonce_action(); |
|
| 220 | - $i = wp_nonce_tick(); |
|
| 221 | - |
|
| 222 | - return substr( wp_hash( $i . $action . get_class( $this ), 'nonce' ), - 12, 10 ); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Verify that the correct nonce was used within the time limit. |
|
| 227 | - * |
|
| 228 | - * @uses wp_nonce_tick() |
|
| 229 | - * @uses wp_hash() |
|
| 230 | - * |
|
| 231 | - * @param string $nonce Nonce to be verified |
|
| 232 | - * |
|
| 233 | - * @return bool Whether the nonce check passed or failed |
|
| 234 | - */ |
|
| 235 | - protected function verify_async_nonce( $nonce ) { |
|
| 236 | - $action = $this->get_nonce_action(); |
|
| 237 | - $i = wp_nonce_tick(); |
|
| 238 | - |
|
| 239 | - // Nonce generated 0-12 hours ago |
|
| 240 | - if ( substr( wp_hash( $i . $action . get_class( $this ), 'nonce' ), - 12, 10 ) == $nonce ) { |
|
| 241 | - return 1; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - // Nonce generated 12-24 hours ago |
|
| 245 | - if ( substr( wp_hash( ( $i - 1 ) . $action . get_class( $this ), 'nonce' ), - 12, 10 ) == $nonce ) { |
|
| 246 | - return 2; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - // Invalid nonce |
|
| 250 | - return false; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Get a nonce action based on the $action property of the class |
|
| 255 | - * |
|
| 256 | - * @return string The nonce action for the current instance |
|
| 257 | - */ |
|
| 258 | - protected function get_nonce_action() { |
|
| 259 | - $action = $this->action; |
|
| 260 | - if ( substr( $action, 0, 7 ) === 'nopriv_' ) { |
|
| 261 | - $action = substr( $action, 7 ); |
|
| 262 | - } |
|
| 263 | - $action = "wl_async_$action"; |
|
| 264 | - |
|
| 265 | - return $action; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Prepare any data to be passed to the asynchronous postback |
|
| 270 | - * |
|
| 271 | - * The array this function receives will be a numerically keyed array from |
|
| 272 | - * func_get_args(). It is expected that you will return an associative array |
|
| 273 | - * so that the $_POST values used in the asynchronous call will make sense. |
|
| 274 | - * |
|
| 275 | - * The array you send back may or may not have anything to do with the data |
|
| 276 | - * passed into this method. It all depends on the implementation details and |
|
| 277 | - * what data is needed in the asynchronous postback. |
|
| 278 | - * |
|
| 279 | - * Do not set values for 'action' or '_nonce', as those will get overwritten |
|
| 280 | - * later in launch(). |
|
| 281 | - * |
|
| 282 | - * @throws Exception If the postback should not occur for any reason |
|
| 283 | - * |
|
| 284 | - * @param array $data The raw data received by the launch method |
|
| 285 | - * |
|
| 286 | - * @return array The prepared data |
|
| 287 | - */ |
|
| 288 | - abstract protected function prepare_data( $data ); |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Run the do_action function for the asynchronous postback. |
|
| 292 | - * |
|
| 293 | - * This method needs to fetch and sanitize any and all data from the $_POST |
|
| 294 | - * superglobal and provide them to the do_action call. |
|
| 295 | - * |
|
| 296 | - * The action should be constructed as "wl_async_task_$this->action" |
|
| 297 | - */ |
|
| 298 | - abstract protected function run_action(); |
|
| 12 | + /** |
|
| 13 | + * Constant identifier for a task that should be available to logged-in users |
|
| 14 | + * |
|
| 15 | + * See constructor documentation for more details. |
|
| 16 | + */ |
|
| 17 | + const LOGGED_IN = 1; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Constant identifier for a task that should be available to logged-out users |
|
| 21 | + * |
|
| 22 | + * See constructor documentation for more details. |
|
| 23 | + */ |
|
| 24 | + const LOGGED_OUT = 2; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Constant identifier for a task that should be available to all users regardless of auth status |
|
| 28 | + * |
|
| 29 | + * See constructor documentation for more details. |
|
| 30 | + */ |
|
| 31 | + const BOTH = 3; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * This is the argument count for the main action set in the constructor. It |
|
| 35 | + * is set to an arbitrarily high value of twenty, but can be overridden if |
|
| 36 | + * necessary |
|
| 37 | + * |
|
| 38 | + * @var int |
|
| 39 | + */ |
|
| 40 | + protected $argument_count = 20; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Priority to fire intermediate action. |
|
| 44 | + * |
|
| 45 | + * @var int |
|
| 46 | + */ |
|
| 47 | + protected $priority = 10; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + protected $action; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @var array |
|
| 56 | + */ |
|
| 57 | + protected $_body_data; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * A {@link Wordlift_Log_Service} instance. |
|
| 61 | + * |
|
| 62 | + * @since 3.15.0 |
|
| 63 | + * @access private |
|
| 64 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 65 | + */ |
|
| 66 | + private $log; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Constructor to wire up the necessary actions |
|
| 70 | + * |
|
| 71 | + * Which hooks the asynchronous postback happens on can be set by the |
|
| 72 | + * $auth_level parameter. There are essentially three options: logged in users |
|
| 73 | + * only, logged out users only, or both. Set this when you instantiate an |
|
| 74 | + * object by using one of the three class constants to do so: |
|
| 75 | + * - LOGGED_IN |
|
| 76 | + * - LOGGED_OUT |
|
| 77 | + * - BOTH |
|
| 78 | + * $auth_level defaults to BOTH |
|
| 79 | + * |
|
| 80 | + * @throws Exception If the class' $action value hasn't been set |
|
| 81 | + * |
|
| 82 | + * @param int $auth_level The authentication level to use (see above) |
|
| 83 | + */ |
|
| 84 | + public function __construct( $auth_level = self::BOTH ) { |
|
| 85 | + |
|
| 86 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 87 | + |
|
| 88 | + if ( empty( $this->action ) ) { |
|
| 89 | + throw new Exception( 'Action not defined for class ' . __CLASS__ ); |
|
| 90 | + } |
|
| 91 | + add_action( $this->action, array( |
|
| 92 | + $this, |
|
| 93 | + 'launch', |
|
| 94 | + ), (int) $this->priority, (int) $this->argument_count ); |
|
| 95 | + if ( $auth_level & self::LOGGED_IN ) { |
|
| 96 | + add_action( "admin_post_wl_async_$this->action", array( |
|
| 97 | + $this, |
|
| 98 | + 'handle_postback', |
|
| 99 | + ) ); |
|
| 100 | + } |
|
| 101 | + if ( $auth_level & self::LOGGED_OUT ) { |
|
| 102 | + add_action( "admin_post_nopriv_wl_async_$this->action", array( |
|
| 103 | + $this, |
|
| 104 | + 'handle_postback', |
|
| 105 | + ) ); |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Add the shutdown action for launching the real postback if we don't |
|
| 111 | + * get an exception thrown by prepare_data(). |
|
| 112 | + * |
|
| 113 | + * @uses func_get_args() To grab any arguments passed by the action |
|
| 114 | + */ |
|
| 115 | + public function launch() { |
|
| 116 | + $data = func_get_args(); |
|
| 117 | + try { |
|
| 118 | + $data = $this->prepare_data( $data ); |
|
| 119 | + } catch ( Exception $e ) { |
|
| 120 | + return; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + $data['action'] = "wl_async_$this->action"; |
|
| 124 | + $data['_nonce'] = $this->create_async_nonce(); |
|
| 125 | + |
|
| 126 | + $this->_body_data = $data; |
|
| 127 | + |
|
| 128 | + if ( ! has_action( 'shutdown', array( |
|
| 129 | + $this, |
|
| 130 | + 'launch_on_shutdown', |
|
| 131 | + ) ) |
|
| 132 | + ) { |
|
| 133 | + add_action( 'shutdown', array( $this, 'launch_on_shutdown' ) ); |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Launch the request on the WordPress shutdown hook |
|
| 139 | + * |
|
| 140 | + * On VIP we got into data races due to the postback sometimes completing |
|
| 141 | + * faster than the data could propogate to the database server cluster. |
|
| 142 | + * This made WordPress get empty data sets from the database without |
|
| 143 | + * failing. On their advice, we're moving the actual firing of the async |
|
| 144 | + * postback to the shutdown hook. Supposedly that will ensure that the |
|
| 145 | + * data at least has time to get into the object cache. |
|
| 146 | + * |
|
| 147 | + * @uses $_COOKIE To send a cookie header for async postback |
|
| 148 | + * @uses apply_filters() |
|
| 149 | + * @uses admin_url() |
|
| 150 | + * @uses wp_remote_post() |
|
| 151 | + */ |
|
| 152 | + public function launch_on_shutdown() { |
|
| 153 | + |
|
| 154 | + $this->log->debug( "Launching Async Task [ action :: $this->action ]..." ); |
|
| 155 | + |
|
| 156 | + if ( ! empty( $this->_body_data ) ) { |
|
| 157 | + $cookies = array(); |
|
| 158 | + foreach ( $_COOKIE as $name => $value ) { |
|
| 159 | + $cookies[] = "$name=" . urlencode( is_array( $value ) ? serialize( $value ) : $value ); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + $request_args = array( |
|
| 163 | + 'timeout' => 0.01, |
|
| 164 | + 'blocking' => false, |
|
| 165 | + 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), |
|
| 166 | + 'body' => $this->_body_data, |
|
| 167 | + 'headers' => array( |
|
| 168 | + 'cookie' => implode( '; ', $cookies ), |
|
| 169 | + ), |
|
| 170 | + ); |
|
| 171 | + |
|
| 172 | + $url = get_site_url( null, 'wl-api' ); |
|
| 173 | + |
|
| 174 | + $this->log->debug( "Posting URL $url..." ); |
|
| 175 | + |
|
| 176 | + $result = wp_remote_post( $url, $request_args ); |
|
| 177 | + |
|
| 178 | + if ( is_wp_error( $result ) ) { |
|
| 179 | + $this->log->error( 'Posting URL returned an error: ' . $result->get_error_message() ); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Verify the postback is valid, then fire any scheduled events. |
|
| 187 | + * |
|
| 188 | + * @uses $_POST['_nonce'] |
|
| 189 | + * @uses is_user_logged_in() |
|
| 190 | + * @uses add_filter() |
|
| 191 | + * @uses wp_die() |
|
| 192 | + */ |
|
| 193 | + public function handle_postback() { |
|
| 194 | + if ( isset( $_POST['_nonce'] ) && $this->verify_async_nonce( $_POST['_nonce'] ) ) { |
|
| 195 | + if ( ! is_user_logged_in() ) { |
|
| 196 | + $this->action = "nopriv_$this->action"; |
|
| 197 | + } |
|
| 198 | + $this->run_action(); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + add_filter( 'wp_die_handler', function () { |
|
| 202 | + die(); |
|
| 203 | + } ); |
|
| 204 | + wp_die(); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Create a random, one time use token. |
|
| 209 | + * |
|
| 210 | + * Based entirely on wp_create_nonce() but does not tie the nonce to the |
|
| 211 | + * current logged-in user. |
|
| 212 | + * |
|
| 213 | + * @uses wp_nonce_tick() |
|
| 214 | + * @uses wp_hash() |
|
| 215 | + * |
|
| 216 | + * @return string The one-time use token |
|
| 217 | + */ |
|
| 218 | + protected function create_async_nonce() { |
|
| 219 | + $action = $this->get_nonce_action(); |
|
| 220 | + $i = wp_nonce_tick(); |
|
| 221 | + |
|
| 222 | + return substr( wp_hash( $i . $action . get_class( $this ), 'nonce' ), - 12, 10 ); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Verify that the correct nonce was used within the time limit. |
|
| 227 | + * |
|
| 228 | + * @uses wp_nonce_tick() |
|
| 229 | + * @uses wp_hash() |
|
| 230 | + * |
|
| 231 | + * @param string $nonce Nonce to be verified |
|
| 232 | + * |
|
| 233 | + * @return bool Whether the nonce check passed or failed |
|
| 234 | + */ |
|
| 235 | + protected function verify_async_nonce( $nonce ) { |
|
| 236 | + $action = $this->get_nonce_action(); |
|
| 237 | + $i = wp_nonce_tick(); |
|
| 238 | + |
|
| 239 | + // Nonce generated 0-12 hours ago |
|
| 240 | + if ( substr( wp_hash( $i . $action . get_class( $this ), 'nonce' ), - 12, 10 ) == $nonce ) { |
|
| 241 | + return 1; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + // Nonce generated 12-24 hours ago |
|
| 245 | + if ( substr( wp_hash( ( $i - 1 ) . $action . get_class( $this ), 'nonce' ), - 12, 10 ) == $nonce ) { |
|
| 246 | + return 2; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + // Invalid nonce |
|
| 250 | + return false; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Get a nonce action based on the $action property of the class |
|
| 255 | + * |
|
| 256 | + * @return string The nonce action for the current instance |
|
| 257 | + */ |
|
| 258 | + protected function get_nonce_action() { |
|
| 259 | + $action = $this->action; |
|
| 260 | + if ( substr( $action, 0, 7 ) === 'nopriv_' ) { |
|
| 261 | + $action = substr( $action, 7 ); |
|
| 262 | + } |
|
| 263 | + $action = "wl_async_$action"; |
|
| 264 | + |
|
| 265 | + return $action; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Prepare any data to be passed to the asynchronous postback |
|
| 270 | + * |
|
| 271 | + * The array this function receives will be a numerically keyed array from |
|
| 272 | + * func_get_args(). It is expected that you will return an associative array |
|
| 273 | + * so that the $_POST values used in the asynchronous call will make sense. |
|
| 274 | + * |
|
| 275 | + * The array you send back may or may not have anything to do with the data |
|
| 276 | + * passed into this method. It all depends on the implementation details and |
|
| 277 | + * what data is needed in the asynchronous postback. |
|
| 278 | + * |
|
| 279 | + * Do not set values for 'action' or '_nonce', as those will get overwritten |
|
| 280 | + * later in launch(). |
|
| 281 | + * |
|
| 282 | + * @throws Exception If the postback should not occur for any reason |
|
| 283 | + * |
|
| 284 | + * @param array $data The raw data received by the launch method |
|
| 285 | + * |
|
| 286 | + * @return array The prepared data |
|
| 287 | + */ |
|
| 288 | + abstract protected function prepare_data( $data ); |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Run the do_action function for the asynchronous postback. |
|
| 292 | + * |
|
| 293 | + * This method needs to fetch and sanitize any and all data from the $_POST |
|
| 294 | + * superglobal and provide them to the do_action call. |
|
| 295 | + * |
|
| 296 | + * The action should be constructed as "wl_async_task_$this->action" |
|
| 297 | + */ |
|
| 298 | + abstract protected function run_action(); |
|
| 299 | 299 | |
| 300 | 300 | } |
| 301 | 301 | |
@@ -81,28 +81,28 @@ discard block |
||
| 81 | 81 | * |
| 82 | 82 | * @param int $auth_level The authentication level to use (see above) |
| 83 | 83 | */ |
| 84 | - public function __construct( $auth_level = self::BOTH ) { |
|
| 84 | + public function __construct($auth_level = self::BOTH) { |
|
| 85 | 85 | |
| 86 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 86 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 87 | 87 | |
| 88 | - if ( empty( $this->action ) ) { |
|
| 89 | - throw new Exception( 'Action not defined for class ' . __CLASS__ ); |
|
| 88 | + if (empty($this->action)) { |
|
| 89 | + throw new Exception('Action not defined for class '.__CLASS__); |
|
| 90 | 90 | } |
| 91 | - add_action( $this->action, array( |
|
| 91 | + add_action($this->action, array( |
|
| 92 | 92 | $this, |
| 93 | 93 | 'launch', |
| 94 | - ), (int) $this->priority, (int) $this->argument_count ); |
|
| 95 | - if ( $auth_level & self::LOGGED_IN ) { |
|
| 96 | - add_action( "admin_post_wl_async_$this->action", array( |
|
| 94 | + ), (int) $this->priority, (int) $this->argument_count); |
|
| 95 | + if ($auth_level & self::LOGGED_IN) { |
|
| 96 | + add_action("admin_post_wl_async_$this->action", array( |
|
| 97 | 97 | $this, |
| 98 | 98 | 'handle_postback', |
| 99 | - ) ); |
|
| 99 | + )); |
|
| 100 | 100 | } |
| 101 | - if ( $auth_level & self::LOGGED_OUT ) { |
|
| 102 | - add_action( "admin_post_nopriv_wl_async_$this->action", array( |
|
| 101 | + if ($auth_level & self::LOGGED_OUT) { |
|
| 102 | + add_action("admin_post_nopriv_wl_async_$this->action", array( |
|
| 103 | 103 | $this, |
| 104 | 104 | 'handle_postback', |
| 105 | - ) ); |
|
| 105 | + )); |
|
| 106 | 106 | } |
| 107 | 107 | } |
| 108 | 108 | |
@@ -115,8 +115,8 @@ discard block |
||
| 115 | 115 | public function launch() { |
| 116 | 116 | $data = func_get_args(); |
| 117 | 117 | try { |
| 118 | - $data = $this->prepare_data( $data ); |
|
| 119 | - } catch ( Exception $e ) { |
|
| 118 | + $data = $this->prepare_data($data); |
|
| 119 | + } catch (Exception $e) { |
|
| 120 | 120 | return; |
| 121 | 121 | } |
| 122 | 122 | |
@@ -125,12 +125,12 @@ discard block |
||
| 125 | 125 | |
| 126 | 126 | $this->_body_data = $data; |
| 127 | 127 | |
| 128 | - if ( ! has_action( 'shutdown', array( |
|
| 128 | + if ( ! has_action('shutdown', array( |
|
| 129 | 129 | $this, |
| 130 | 130 | 'launch_on_shutdown', |
| 131 | - ) ) |
|
| 131 | + )) |
|
| 132 | 132 | ) { |
| 133 | - add_action( 'shutdown', array( $this, 'launch_on_shutdown' ) ); |
|
| 133 | + add_action('shutdown', array($this, 'launch_on_shutdown')); |
|
| 134 | 134 | } |
| 135 | 135 | } |
| 136 | 136 | |
@@ -151,32 +151,32 @@ discard block |
||
| 151 | 151 | */ |
| 152 | 152 | public function launch_on_shutdown() { |
| 153 | 153 | |
| 154 | - $this->log->debug( "Launching Async Task [ action :: $this->action ]..." ); |
|
| 154 | + $this->log->debug("Launching Async Task [ action :: $this->action ]..."); |
|
| 155 | 155 | |
| 156 | - if ( ! empty( $this->_body_data ) ) { |
|
| 156 | + if ( ! empty($this->_body_data)) { |
|
| 157 | 157 | $cookies = array(); |
| 158 | - foreach ( $_COOKIE as $name => $value ) { |
|
| 159 | - $cookies[] = "$name=" . urlencode( is_array( $value ) ? serialize( $value ) : $value ); |
|
| 158 | + foreach ($_COOKIE as $name => $value) { |
|
| 159 | + $cookies[] = "$name=".urlencode(is_array($value) ? serialize($value) : $value); |
|
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | $request_args = array( |
| 163 | 163 | 'timeout' => 0.01, |
| 164 | 164 | 'blocking' => false, |
| 165 | - 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), |
|
| 165 | + 'sslverify' => apply_filters('https_local_ssl_verify', false), |
|
| 166 | 166 | 'body' => $this->_body_data, |
| 167 | 167 | 'headers' => array( |
| 168 | - 'cookie' => implode( '; ', $cookies ), |
|
| 168 | + 'cookie' => implode('; ', $cookies), |
|
| 169 | 169 | ), |
| 170 | 170 | ); |
| 171 | 171 | |
| 172 | - $url = get_site_url( null, 'wl-api' ); |
|
| 172 | + $url = get_site_url(null, 'wl-api'); |
|
| 173 | 173 | |
| 174 | - $this->log->debug( "Posting URL $url..." ); |
|
| 174 | + $this->log->debug("Posting URL $url..."); |
|
| 175 | 175 | |
| 176 | - $result = wp_remote_post( $url, $request_args ); |
|
| 176 | + $result = wp_remote_post($url, $request_args); |
|
| 177 | 177 | |
| 178 | - if ( is_wp_error( $result ) ) { |
|
| 179 | - $this->log->error( 'Posting URL returned an error: ' . $result->get_error_message() ); |
|
| 178 | + if (is_wp_error($result)) { |
|
| 179 | + $this->log->error('Posting URL returned an error: '.$result->get_error_message()); |
|
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | } |
@@ -191,14 +191,14 @@ discard block |
||
| 191 | 191 | * @uses wp_die() |
| 192 | 192 | */ |
| 193 | 193 | public function handle_postback() { |
| 194 | - if ( isset( $_POST['_nonce'] ) && $this->verify_async_nonce( $_POST['_nonce'] ) ) { |
|
| 195 | - if ( ! is_user_logged_in() ) { |
|
| 194 | + if (isset($_POST['_nonce']) && $this->verify_async_nonce($_POST['_nonce'])) { |
|
| 195 | + if ( ! is_user_logged_in()) { |
|
| 196 | 196 | $this->action = "nopriv_$this->action"; |
| 197 | 197 | } |
| 198 | 198 | $this->run_action(); |
| 199 | 199 | } |
| 200 | 200 | |
| 201 | - add_filter( 'wp_die_handler', function () { |
|
| 201 | + add_filter('wp_die_handler', function() { |
|
| 202 | 202 | die(); |
| 203 | 203 | } ); |
| 204 | 204 | wp_die(); |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | $action = $this->get_nonce_action(); |
| 220 | 220 | $i = wp_nonce_tick(); |
| 221 | 221 | |
| 222 | - return substr( wp_hash( $i . $action . get_class( $this ), 'nonce' ), - 12, 10 ); |
|
| 222 | + return substr(wp_hash($i.$action.get_class($this), 'nonce'), - 12, 10); |
|
| 223 | 223 | } |
| 224 | 224 | |
| 225 | 225 | /** |
@@ -232,17 +232,17 @@ discard block |
||
| 232 | 232 | * |
| 233 | 233 | * @return bool Whether the nonce check passed or failed |
| 234 | 234 | */ |
| 235 | - protected function verify_async_nonce( $nonce ) { |
|
| 235 | + protected function verify_async_nonce($nonce) { |
|
| 236 | 236 | $action = $this->get_nonce_action(); |
| 237 | 237 | $i = wp_nonce_tick(); |
| 238 | 238 | |
| 239 | 239 | // Nonce generated 0-12 hours ago |
| 240 | - if ( substr( wp_hash( $i . $action . get_class( $this ), 'nonce' ), - 12, 10 ) == $nonce ) { |
|
| 240 | + if (substr(wp_hash($i.$action.get_class($this), 'nonce'), - 12, 10) == $nonce) { |
|
| 241 | 241 | return 1; |
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | // Nonce generated 12-24 hours ago |
| 245 | - if ( substr( wp_hash( ( $i - 1 ) . $action . get_class( $this ), 'nonce' ), - 12, 10 ) == $nonce ) { |
|
| 245 | + if (substr(wp_hash(($i - 1).$action.get_class($this), 'nonce'), - 12, 10) == $nonce) { |
|
| 246 | 246 | return 2; |
| 247 | 247 | } |
| 248 | 248 | |
@@ -257,8 +257,8 @@ discard block |
||
| 257 | 257 | */ |
| 258 | 258 | protected function get_nonce_action() { |
| 259 | 259 | $action = $this->action; |
| 260 | - if ( substr( $action, 0, 7 ) === 'nopriv_' ) { |
|
| 261 | - $action = substr( $action, 7 ); |
|
| 260 | + if (substr($action, 0, 7) === 'nopriv_') { |
|
| 261 | + $action = substr($action, 7); |
|
| 262 | 262 | } |
| 263 | 263 | $action = "wl_async_$action"; |
| 264 | 264 | |
@@ -285,7 +285,7 @@ discard block |
||
| 285 | 285 | * |
| 286 | 286 | * @return array The prepared data |
| 287 | 287 | */ |
| 288 | - abstract protected function prepare_data( $data ); |
|
| 288 | + abstract protected function prepare_data($data); |
|
| 289 | 289 | |
| 290 | 290 | /** |
| 291 | 291 | * Run the do_action function for the asynchronous postback. |
@@ -13,196 +13,196 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Wordlift_Jsonld_Service { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 18 | - * |
|
| 19 | - * @since 3.8.0 |
|
| 20 | - * @access private |
|
| 21 | - * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 22 | - */ |
|
| 23 | - private $entity_service; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * A {@link Wordlift_Post_Converter} instance. |
|
| 27 | - * |
|
| 28 | - * @since 3.8.0 |
|
| 29 | - * @access private |
|
| 30 | - * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance. |
|
| 31 | - */ |
|
| 32 | - private $converter; |
|
| 33 | - |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 37 | - * |
|
| 38 | - * @since 3.14.0 |
|
| 39 | - * @access private |
|
| 40 | - * @var \Wordlift_Website_Jsonld_Converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 41 | - */ |
|
| 42 | - private $website_converter; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * The singleton instance for the JSON-LD service. |
|
| 46 | - * |
|
| 47 | - * @since 3.15.1 |
|
| 48 | - * |
|
| 49 | - * @var \Wordlift_Jsonld_Service $instance The singleton instance for the JSON-LD service. |
|
| 50 | - */ |
|
| 51 | - private static $instance; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Create a JSON-LD service. |
|
| 55 | - * |
|
| 56 | - * @since 3.8.0 |
|
| 57 | - * |
|
| 58 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 59 | - * @param \Wordlift_Post_Converter $converter A {@link Wordlift_Uri_To_Jsonld_Converter} instance. |
|
| 60 | - * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 61 | - */ |
|
| 62 | - public function __construct( $entity_service, $converter, $website_converter ) { |
|
| 63 | - |
|
| 64 | - $this->entity_service = $entity_service; |
|
| 65 | - $this->converter = $converter; |
|
| 66 | - $this->website_converter = $website_converter; |
|
| 67 | - |
|
| 68 | - self::$instance = $this; |
|
| 69 | - |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Get the singleton instance for the JSON-LD service. |
|
| 74 | - * |
|
| 75 | - * @since 3.15.1 |
|
| 76 | - * |
|
| 77 | - * @return \Wordlift_Jsonld_Service The singleton instance for the JSON-LD service. |
|
| 78 | - */ |
|
| 79 | - public static function get_instance() { |
|
| 80 | - |
|
| 81 | - return self::$instance; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Process calls to the AJAX 'wl_jsonld' endpoint. |
|
| 86 | - * |
|
| 87 | - * @since 3.8.0 |
|
| 88 | - */ |
|
| 89 | - public function get() { |
|
| 90 | - // Clear the buffer to be sure someone doesn't mess with our response. |
|
| 91 | - // |
|
| 92 | - // See https://github.com/insideout10/wordlift-plugin/issues/406. |
|
| 93 | - // See https://codex.wordpress.org/AJAX_in_Plugins. |
|
| 94 | - @ob_clean(); |
|
| 95 | - |
|
| 96 | - // Get the parameter from the request. |
|
| 97 | - $is_homepage = isset( $_REQUEST['homepage'] ); |
|
| 98 | - $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; |
|
| 99 | - |
|
| 100 | - // Send the generated JSON-LD. |
|
| 101 | - $this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
| 102 | - |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * A close of WP's own `wp_send_json` function which uses `application/ld+json` as content type. |
|
| 107 | - * |
|
| 108 | - * @since 3.18.5 |
|
| 109 | - * |
|
| 110 | - * @param mixed $response Variable (usually an array or object) to encode as JSON, |
|
| 111 | - * then print and die. |
|
| 112 | - * @param int $status_code The HTTP status code to output. |
|
| 113 | - */ |
|
| 114 | - private function send_jsonld( $response, $status_code = null ) { |
|
| 115 | - @header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) ); |
|
| 116 | - echo wp_json_encode( $response ); |
|
| 117 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 118 | - wp_die(); |
|
| 119 | - } else { |
|
| 120 | - die; |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Get the JSON-LD. |
|
| 126 | - * |
|
| 127 | - * @since 3.15.1 |
|
| 128 | - * |
|
| 129 | - * @param bool $is_homepage Whether the JSON-LD for the homepage is being requested. |
|
| 130 | - * @param int|null $post_id The JSON-LD for the specified {@link WP_Post} id. |
|
| 131 | - * |
|
| 132 | - * @return array A JSON-LD structure. |
|
| 133 | - */ |
|
| 134 | - public function get_jsonld( $is_homepage = false, $post_id = null ) { |
|
| 135 | - |
|
| 136 | - // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
|
| 137 | - // e-mails with a low apdex score. |
|
| 138 | - // |
|
| 139 | - // See https://github.com/insideout10/wordlift-plugin/issues/521 |
|
| 140 | - Wordlift_NewRelic_Adapter::ignore_apdex(); |
|
| 141 | - |
|
| 142 | - // Switch to Website converter if is home page. |
|
| 143 | - if ( $is_homepage ) { |
|
| 144 | - /** |
|
| 145 | - * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
|
| 146 | - * |
|
| 147 | - * @since 3.14.0 |
|
| 148 | - * @api bool $display_search Whether or not to display json+ld search on the frontend. |
|
| 149 | - */ |
|
| 150 | - if ( ! apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
| 151 | - // Set a reference to the website_converter. |
|
| 152 | - $website_converter = $this->website_converter; |
|
| 153 | - |
|
| 154 | - // Send JSON-LD. |
|
| 155 | - return $website_converter->create_schema(); |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - // If no id has been provided return an empty array. |
|
| 160 | - if ( ! isset( $post_id ) ) { |
|
| 161 | - return array(); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - // An array of references which is captured when converting an URI to a |
|
| 165 | - // json which we gather to further expand our json-ld. |
|
| 166 | - $references = array(); |
|
| 167 | - |
|
| 168 | - // Set a reference to the entity_to_jsonld_converter to use in the closures. |
|
| 169 | - $entity_to_jsonld_converter = $this->converter; |
|
| 170 | - |
|
| 171 | - // Convert each URI to a JSON-LD array, while gathering referenced entities. |
|
| 172 | - // in the references array. |
|
| 173 | - $jsonld = array_merge( |
|
| 174 | - array( $entity_to_jsonld_converter->convert( $post_id, $references ) ), |
|
| 175 | - // Convert each URI in the references array to JSON-LD. We don't output |
|
| 176 | - // entities already output above (hence the array_diff). |
|
| 177 | - array_filter( array_map( function ( $item ) use ( $entity_to_jsonld_converter, $references ) { |
|
| 178 | - |
|
| 179 | - // "2nd level properties" may not output here, e.g. a post |
|
| 180 | - // mentioning an event, located in a place: the place is referenced |
|
| 181 | - // via the `@id` but no other properties are loaded. |
|
| 182 | - return $entity_to_jsonld_converter->convert( $item, $references ); |
|
| 183 | - }, $references ) ) ); |
|
| 184 | - |
|
| 185 | - // Finally send the JSON-LD. |
|
| 186 | - return $jsonld; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Write the JSON-LD in the head. |
|
| 191 | - * |
|
| 192 | - * This function isn't actually used, but may be used to quickly enable writing the JSON-LD synchronously to the |
|
| 193 | - * document head, using the `wp_head` hook. |
|
| 194 | - * |
|
| 195 | - * @since 3.18.5 |
|
| 196 | - */ |
|
| 197 | - public function wp_head() { |
|
| 198 | - |
|
| 199 | - // Determine whether this is the home page or whether we're displaying a single post. |
|
| 200 | - $is_homepage = is_home() || is_front_page(); |
|
| 201 | - $post_id = is_singular() ? get_the_ID() : null; |
|
| 202 | - |
|
| 203 | - $jsonld = json_encode( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
| 204 | - ?> |
|
| 16 | + /** |
|
| 17 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 18 | + * |
|
| 19 | + * @since 3.8.0 |
|
| 20 | + * @access private |
|
| 21 | + * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 22 | + */ |
|
| 23 | + private $entity_service; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * A {@link Wordlift_Post_Converter} instance. |
|
| 27 | + * |
|
| 28 | + * @since 3.8.0 |
|
| 29 | + * @access private |
|
| 30 | + * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance. |
|
| 31 | + */ |
|
| 32 | + private $converter; |
|
| 33 | + |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 37 | + * |
|
| 38 | + * @since 3.14.0 |
|
| 39 | + * @access private |
|
| 40 | + * @var \Wordlift_Website_Jsonld_Converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 41 | + */ |
|
| 42 | + private $website_converter; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * The singleton instance for the JSON-LD service. |
|
| 46 | + * |
|
| 47 | + * @since 3.15.1 |
|
| 48 | + * |
|
| 49 | + * @var \Wordlift_Jsonld_Service $instance The singleton instance for the JSON-LD service. |
|
| 50 | + */ |
|
| 51 | + private static $instance; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Create a JSON-LD service. |
|
| 55 | + * |
|
| 56 | + * @since 3.8.0 |
|
| 57 | + * |
|
| 58 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 59 | + * @param \Wordlift_Post_Converter $converter A {@link Wordlift_Uri_To_Jsonld_Converter} instance. |
|
| 60 | + * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 61 | + */ |
|
| 62 | + public function __construct( $entity_service, $converter, $website_converter ) { |
|
| 63 | + |
|
| 64 | + $this->entity_service = $entity_service; |
|
| 65 | + $this->converter = $converter; |
|
| 66 | + $this->website_converter = $website_converter; |
|
| 67 | + |
|
| 68 | + self::$instance = $this; |
|
| 69 | + |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Get the singleton instance for the JSON-LD service. |
|
| 74 | + * |
|
| 75 | + * @since 3.15.1 |
|
| 76 | + * |
|
| 77 | + * @return \Wordlift_Jsonld_Service The singleton instance for the JSON-LD service. |
|
| 78 | + */ |
|
| 79 | + public static function get_instance() { |
|
| 80 | + |
|
| 81 | + return self::$instance; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Process calls to the AJAX 'wl_jsonld' endpoint. |
|
| 86 | + * |
|
| 87 | + * @since 3.8.0 |
|
| 88 | + */ |
|
| 89 | + public function get() { |
|
| 90 | + // Clear the buffer to be sure someone doesn't mess with our response. |
|
| 91 | + // |
|
| 92 | + // See https://github.com/insideout10/wordlift-plugin/issues/406. |
|
| 93 | + // See https://codex.wordpress.org/AJAX_in_Plugins. |
|
| 94 | + @ob_clean(); |
|
| 95 | + |
|
| 96 | + // Get the parameter from the request. |
|
| 97 | + $is_homepage = isset( $_REQUEST['homepage'] ); |
|
| 98 | + $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; |
|
| 99 | + |
|
| 100 | + // Send the generated JSON-LD. |
|
| 101 | + $this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
| 102 | + |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * A close of WP's own `wp_send_json` function which uses `application/ld+json` as content type. |
|
| 107 | + * |
|
| 108 | + * @since 3.18.5 |
|
| 109 | + * |
|
| 110 | + * @param mixed $response Variable (usually an array or object) to encode as JSON, |
|
| 111 | + * then print and die. |
|
| 112 | + * @param int $status_code The HTTP status code to output. |
|
| 113 | + */ |
|
| 114 | + private function send_jsonld( $response, $status_code = null ) { |
|
| 115 | + @header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) ); |
|
| 116 | + echo wp_json_encode( $response ); |
|
| 117 | + if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 118 | + wp_die(); |
|
| 119 | + } else { |
|
| 120 | + die; |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Get the JSON-LD. |
|
| 126 | + * |
|
| 127 | + * @since 3.15.1 |
|
| 128 | + * |
|
| 129 | + * @param bool $is_homepage Whether the JSON-LD for the homepage is being requested. |
|
| 130 | + * @param int|null $post_id The JSON-LD for the specified {@link WP_Post} id. |
|
| 131 | + * |
|
| 132 | + * @return array A JSON-LD structure. |
|
| 133 | + */ |
|
| 134 | + public function get_jsonld( $is_homepage = false, $post_id = null ) { |
|
| 135 | + |
|
| 136 | + // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
|
| 137 | + // e-mails with a low apdex score. |
|
| 138 | + // |
|
| 139 | + // See https://github.com/insideout10/wordlift-plugin/issues/521 |
|
| 140 | + Wordlift_NewRelic_Adapter::ignore_apdex(); |
|
| 141 | + |
|
| 142 | + // Switch to Website converter if is home page. |
|
| 143 | + if ( $is_homepage ) { |
|
| 144 | + /** |
|
| 145 | + * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
|
| 146 | + * |
|
| 147 | + * @since 3.14.0 |
|
| 148 | + * @api bool $display_search Whether or not to display json+ld search on the frontend. |
|
| 149 | + */ |
|
| 150 | + if ( ! apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
| 151 | + // Set a reference to the website_converter. |
|
| 152 | + $website_converter = $this->website_converter; |
|
| 153 | + |
|
| 154 | + // Send JSON-LD. |
|
| 155 | + return $website_converter->create_schema(); |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + // If no id has been provided return an empty array. |
|
| 160 | + if ( ! isset( $post_id ) ) { |
|
| 161 | + return array(); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + // An array of references which is captured when converting an URI to a |
|
| 165 | + // json which we gather to further expand our json-ld. |
|
| 166 | + $references = array(); |
|
| 167 | + |
|
| 168 | + // Set a reference to the entity_to_jsonld_converter to use in the closures. |
|
| 169 | + $entity_to_jsonld_converter = $this->converter; |
|
| 170 | + |
|
| 171 | + // Convert each URI to a JSON-LD array, while gathering referenced entities. |
|
| 172 | + // in the references array. |
|
| 173 | + $jsonld = array_merge( |
|
| 174 | + array( $entity_to_jsonld_converter->convert( $post_id, $references ) ), |
|
| 175 | + // Convert each URI in the references array to JSON-LD. We don't output |
|
| 176 | + // entities already output above (hence the array_diff). |
|
| 177 | + array_filter( array_map( function ( $item ) use ( $entity_to_jsonld_converter, $references ) { |
|
| 178 | + |
|
| 179 | + // "2nd level properties" may not output here, e.g. a post |
|
| 180 | + // mentioning an event, located in a place: the place is referenced |
|
| 181 | + // via the `@id` but no other properties are loaded. |
|
| 182 | + return $entity_to_jsonld_converter->convert( $item, $references ); |
|
| 183 | + }, $references ) ) ); |
|
| 184 | + |
|
| 185 | + // Finally send the JSON-LD. |
|
| 186 | + return $jsonld; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Write the JSON-LD in the head. |
|
| 191 | + * |
|
| 192 | + * This function isn't actually used, but may be used to quickly enable writing the JSON-LD synchronously to the |
|
| 193 | + * document head, using the `wp_head` hook. |
|
| 194 | + * |
|
| 195 | + * @since 3.18.5 |
|
| 196 | + */ |
|
| 197 | + public function wp_head() { |
|
| 198 | + |
|
| 199 | + // Determine whether this is the home page or whether we're displaying a single post. |
|
| 200 | + $is_homepage = is_home() || is_front_page(); |
|
| 201 | + $post_id = is_singular() ? get_the_ID() : null; |
|
| 202 | + |
|
| 203 | + $jsonld = json_encode( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
| 204 | + ?> |
|
| 205 | 205 | <script type="application/ld+json"><?php echo $jsonld; ?></script><?php |
| 206 | - } |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | 208 | } |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | * @param \Wordlift_Post_Converter $converter A {@link Wordlift_Uri_To_Jsonld_Converter} instance. |
| 60 | 60 | * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
| 61 | 61 | */ |
| 62 | - public function __construct( $entity_service, $converter, $website_converter ) { |
|
| 62 | + public function __construct($entity_service, $converter, $website_converter) { |
|
| 63 | 63 | |
| 64 | 64 | $this->entity_service = $entity_service; |
| 65 | 65 | $this->converter = $converter; |
@@ -94,11 +94,11 @@ discard block |
||
| 94 | 94 | @ob_clean(); |
| 95 | 95 | |
| 96 | 96 | // Get the parameter from the request. |
| 97 | - $is_homepage = isset( $_REQUEST['homepage'] ); |
|
| 98 | - $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; |
|
| 97 | + $is_homepage = isset($_REQUEST['homepage']); |
|
| 98 | + $post_id = isset($_REQUEST['id']) && is_numeric($_REQUEST['id']) ? intval($_REQUEST['id']) : null; |
|
| 99 | 99 | |
| 100 | 100 | // Send the generated JSON-LD. |
| 101 | - $this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
| 101 | + $this->send_jsonld($this->get_jsonld($is_homepage, $post_id)); |
|
| 102 | 102 | |
| 103 | 103 | } |
| 104 | 104 | |
@@ -111,10 +111,10 @@ discard block |
||
| 111 | 111 | * then print and die. |
| 112 | 112 | * @param int $status_code The HTTP status code to output. |
| 113 | 113 | */ |
| 114 | - private function send_jsonld( $response, $status_code = null ) { |
|
| 115 | - @header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) ); |
|
| 116 | - echo wp_json_encode( $response ); |
|
| 117 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 114 | + private function send_jsonld($response, $status_code = null) { |
|
| 115 | + @header('Content-Type: application/ld+json; charset='.get_option('blog_charset')); |
|
| 116 | + echo wp_json_encode($response); |
|
| 117 | + if (apply_filters('wp_doing_ajax', defined('DOING_AJAX') && DOING_AJAX)) { |
|
| 118 | 118 | wp_die(); |
| 119 | 119 | } else { |
| 120 | 120 | die; |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | * |
| 132 | 132 | * @return array A JSON-LD structure. |
| 133 | 133 | */ |
| 134 | - public function get_jsonld( $is_homepage = false, $post_id = null ) { |
|
| 134 | + public function get_jsonld($is_homepage = false, $post_id = null) { |
|
| 135 | 135 | |
| 136 | 136 | // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
| 137 | 137 | // e-mails with a low apdex score. |
@@ -140,14 +140,14 @@ discard block |
||
| 140 | 140 | Wordlift_NewRelic_Adapter::ignore_apdex(); |
| 141 | 141 | |
| 142 | 142 | // Switch to Website converter if is home page. |
| 143 | - if ( $is_homepage ) { |
|
| 143 | + if ($is_homepage) { |
|
| 144 | 144 | /** |
| 145 | 145 | * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
| 146 | 146 | * |
| 147 | 147 | * @since 3.14.0 |
| 148 | 148 | * @api bool $display_search Whether or not to display json+ld search on the frontend. |
| 149 | 149 | */ |
| 150 | - if ( ! apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
| 150 | + if ( ! apply_filters('wordlift_disable_website_json_ld', false)) { |
|
| 151 | 151 | // Set a reference to the website_converter. |
| 152 | 152 | $website_converter = $this->website_converter; |
| 153 | 153 | |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | // If no id has been provided return an empty array. |
| 160 | - if ( ! isset( $post_id ) ) { |
|
| 160 | + if ( ! isset($post_id)) { |
|
| 161 | 161 | return array(); |
| 162 | 162 | } |
| 163 | 163 | |
@@ -171,16 +171,16 @@ discard block |
||
| 171 | 171 | // Convert each URI to a JSON-LD array, while gathering referenced entities. |
| 172 | 172 | // in the references array. |
| 173 | 173 | $jsonld = array_merge( |
| 174 | - array( $entity_to_jsonld_converter->convert( $post_id, $references ) ), |
|
| 174 | + array($entity_to_jsonld_converter->convert($post_id, $references)), |
|
| 175 | 175 | // Convert each URI in the references array to JSON-LD. We don't output |
| 176 | 176 | // entities already output above (hence the array_diff). |
| 177 | - array_filter( array_map( function ( $item ) use ( $entity_to_jsonld_converter, $references ) { |
|
| 177 | + array_filter(array_map(function($item) use ($entity_to_jsonld_converter, $references) { |
|
| 178 | 178 | |
| 179 | 179 | // "2nd level properties" may not output here, e.g. a post |
| 180 | 180 | // mentioning an event, located in a place: the place is referenced |
| 181 | 181 | // via the `@id` but no other properties are loaded. |
| 182 | - return $entity_to_jsonld_converter->convert( $item, $references ); |
|
| 183 | - }, $references ) ) ); |
|
| 182 | + return $entity_to_jsonld_converter->convert($item, $references); |
|
| 183 | + }, $references)) ); |
|
| 184 | 184 | |
| 185 | 185 | // Finally send the JSON-LD. |
| 186 | 186 | return $jsonld; |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | $is_homepage = is_home() || is_front_page(); |
| 201 | 201 | $post_id = is_singular() ? get_the_ID() : null; |
| 202 | 202 | |
| 203 | - $jsonld = json_encode( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
| 203 | + $jsonld = json_encode($this->get_jsonld($is_homepage, $post_id)); |
|
| 204 | 204 | ?> |
| 205 | 205 | <script type="application/ld+json"><?php echo $jsonld; ?></script><?php |
| 206 | 206 | } |
@@ -7,135 +7,135 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Wordlift_Log_Service { |
| 9 | 9 | |
| 10 | - const MESSAGE_TEMPLATE = '%-6s [%-40.40s] %s'; |
|
| 11 | - |
|
| 12 | - const ERROR = 4; |
|
| 13 | - const WARN = 3; |
|
| 14 | - const INFO = 2; |
|
| 15 | - const DEBUG = 1; |
|
| 16 | - const TRACE = 0; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * The class related to the logs. |
|
| 20 | - * |
|
| 21 | - * @since 1.0.0 |
|
| 22 | - * @access private |
|
| 23 | - * @var string $class_name The class related to the logs. |
|
| 24 | - */ |
|
| 25 | - private $class_name; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * The log levels for printing in log lines. |
|
| 29 | - * |
|
| 30 | - * @var array $levels An array of log levels. |
|
| 31 | - */ |
|
| 32 | - private static $levels = array( |
|
| 33 | - self::TRACE => 'TRACE', |
|
| 34 | - self::DEBUG => 'DEBUG', |
|
| 35 | - self::INFO => 'INFO', |
|
| 36 | - self::WARN => 'WARN', |
|
| 37 | - self::ERROR => 'ERROR', |
|
| 38 | - ); |
|
| 10 | + const MESSAGE_TEMPLATE = '%-6s [%-40.40s] %s'; |
|
| 11 | + |
|
| 12 | + const ERROR = 4; |
|
| 13 | + const WARN = 3; |
|
| 14 | + const INFO = 2; |
|
| 15 | + const DEBUG = 1; |
|
| 16 | + const TRACE = 0; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * The class related to the logs. |
|
| 20 | + * |
|
| 21 | + * @since 1.0.0 |
|
| 22 | + * @access private |
|
| 23 | + * @var string $class_name The class related to the logs. |
|
| 24 | + */ |
|
| 25 | + private $class_name; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * The log levels for printing in log lines. |
|
| 29 | + * |
|
| 30 | + * @var array $levels An array of log levels. |
|
| 31 | + */ |
|
| 32 | + private static $levels = array( |
|
| 33 | + self::TRACE => 'TRACE', |
|
| 34 | + self::DEBUG => 'DEBUG', |
|
| 35 | + self::INFO => 'INFO', |
|
| 36 | + self::WARN => 'WARN', |
|
| 37 | + self::ERROR => 'ERROR', |
|
| 38 | + ); |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * A singleton instance for legacy logging. |
|
| 42 | - * |
|
| 43 | - * @since 3.10.0 |
|
| 44 | - * @access private |
|
| 45 | - * @var \Wordlift_Log_Service $instance A singleton instance for legacy logging. |
|
| 46 | - */ |
|
| 47 | - private static $instance = null; |
|
| 40 | + /** |
|
| 41 | + * A singleton instance for legacy logging. |
|
| 42 | + * |
|
| 43 | + * @since 3.10.0 |
|
| 44 | + * @access private |
|
| 45 | + * @var \Wordlift_Log_Service $instance A singleton instance for legacy logging. |
|
| 46 | + */ |
|
| 47 | + private static $instance = null; |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Create an instance of the Log service. |
|
| 51 | - * @since 1.0.0 |
|
| 52 | - * |
|
| 53 | - * @param string $class_name The class related to the logs. |
|
| 54 | - */ |
|
| 55 | - public function __construct( $class_name ) { |
|
| 49 | + /** |
|
| 50 | + * Create an instance of the Log service. |
|
| 51 | + * @since 1.0.0 |
|
| 52 | + * |
|
| 53 | + * @param string $class_name The class related to the logs. |
|
| 54 | + */ |
|
| 55 | + public function __construct( $class_name ) { |
|
| 56 | 56 | |
| 57 | - $this->class_name = $class_name; |
|
| 57 | + $this->class_name = $class_name; |
|
| 58 | 58 | |
| 59 | - } |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Get the ROOT logger. |
|
| 63 | - * |
|
| 64 | - * @since 3.10.0 |
|
| 65 | - * |
|
| 66 | - * @return \Wordlift_Log_Service A singleton instance for legacy logging. |
|
| 67 | - */ |
|
| 68 | - public static function get_instance() { |
|
| 61 | + /** |
|
| 62 | + * Get the ROOT logger. |
|
| 63 | + * |
|
| 64 | + * @since 3.10.0 |
|
| 65 | + * |
|
| 66 | + * @return \Wordlift_Log_Service A singleton instance for legacy logging. |
|
| 67 | + */ |
|
| 68 | + public static function get_instance() { |
|
| 69 | 69 | |
| 70 | - return self::$instance ?: self::$instance = new Wordlift_Log_Service( 'ROOT' ); |
|
| 71 | - } |
|
| 70 | + return self::$instance ?: self::$instance = new Wordlift_Log_Service( 'ROOT' ); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | 73 | |
| 74 | - public static function get_logger( $class_name ) { |
|
| 74 | + public static function get_logger( $class_name ) { |
|
| 75 | 75 | |
| 76 | - return new Wordlift_Log_Service( $class_name ); |
|
| 76 | + return new Wordlift_Log_Service( $class_name ); |
|
| 77 | 77 | |
| 78 | - } |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Log a message. |
|
| 82 | - * |
|
| 83 | - * @since 1.0.0 |
|
| 84 | - * |
|
| 85 | - * @param string $level The log level. |
|
| 86 | - * @param string $message The message to log. |
|
| 87 | - */ |
|
| 88 | - public function log( $level, $message ) { |
|
| 80 | + /** |
|
| 81 | + * Log a message. |
|
| 82 | + * |
|
| 83 | + * @since 1.0.0 |
|
| 84 | + * |
|
| 85 | + * @param string $level The log level. |
|
| 86 | + * @param string $message The message to log. |
|
| 87 | + */ |
|
| 88 | + public function log( $level, $message ) { |
|
| 89 | 89 | |
| 90 | - // Bail out if WordLift log level isn't defined, and WP debug is disabled. |
|
| 91 | - if ( ! defined( 'WL_LOG_LEVEL' ) && $level < self::ERROR |
|
| 92 | - && ( ! defined( 'WP_DEBUG' ) || false === WP_DEBUG ) ) { |
|
| 93 | - return; |
|
| 94 | - } |
|
| 90 | + // Bail out if WordLift log level isn't defined, and WP debug is disabled. |
|
| 91 | + if ( ! defined( 'WL_LOG_LEVEL' ) && $level < self::ERROR |
|
| 92 | + && ( ! defined( 'WP_DEBUG' ) || false === WP_DEBUG ) ) { |
|
| 93 | + return; |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - // Bail out if the log message is below the minimum log level. |
|
| 97 | - if ( defined( 'WL_LOG_LEVEL' ) && $level < intval( WL_LOG_LEVEL ) ) { |
|
| 98 | - return; |
|
| 99 | - } |
|
| 96 | + // Bail out if the log message is below the minimum log level. |
|
| 97 | + if ( defined( 'WL_LOG_LEVEL' ) && $level < intval( WL_LOG_LEVEL ) ) { |
|
| 98 | + return; |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - // Bail out if there's a filter and we don't match it. |
|
| 102 | - if ( defined( 'WL_LOG_FILTER' ) && 1 !== preg_match( "/(^|,)$this->class_name($|,)/", WL_LOG_FILTER ) ) { |
|
| 103 | - return; |
|
| 104 | - } |
|
| 101 | + // Bail out if there's a filter and we don't match it. |
|
| 102 | + if ( defined( 'WL_LOG_FILTER' ) && 1 !== preg_match( "/(^|,)$this->class_name($|,)/", WL_LOG_FILTER ) ) { |
|
| 103 | + return; |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - // Finally log the message. |
|
| 107 | - error_log( sprintf( self::MESSAGE_TEMPLATE, self::$levels[ $level ], $this->class_name, is_array( $message ) ? implode( ', ', $message ) : $message ) ); |
|
| 106 | + // Finally log the message. |
|
| 107 | + error_log( sprintf( self::MESSAGE_TEMPLATE, self::$levels[ $level ], $this->class_name, is_array( $message ) ? implode( ', ', $message ) : $message ) ); |
|
| 108 | 108 | |
| 109 | - } |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - public function error( $message ) { |
|
| 111 | + public function error( $message ) { |
|
| 112 | 112 | |
| 113 | - $this->log( self::ERROR, $message ); |
|
| 113 | + $this->log( self::ERROR, $message ); |
|
| 114 | 114 | |
| 115 | - } |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - public function warn( $message ) { |
|
| 117 | + public function warn( $message ) { |
|
| 118 | 118 | |
| 119 | - $this->log( self::WARN, $message ); |
|
| 119 | + $this->log( self::WARN, $message ); |
|
| 120 | 120 | |
| 121 | - } |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - public function info( $message ) { |
|
| 123 | + public function info( $message ) { |
|
| 124 | 124 | |
| 125 | - $this->log( self::INFO, $message ); |
|
| 125 | + $this->log( self::INFO, $message ); |
|
| 126 | 126 | |
| 127 | - } |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - public function debug( $message ) { |
|
| 129 | + public function debug( $message ) { |
|
| 130 | 130 | |
| 131 | - $this->log( self::DEBUG, $message ); |
|
| 131 | + $this->log( self::DEBUG, $message ); |
|
| 132 | 132 | |
| 133 | - } |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - public function trace( $message ) { |
|
| 135 | + public function trace( $message ) { |
|
| 136 | 136 | |
| 137 | - $this->log( self::TRACE, $message ); |
|
| 137 | + $this->log( self::TRACE, $message ); |
|
| 138 | 138 | |
| 139 | - } |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | 141 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | * |
| 53 | 53 | * @param string $class_name The class related to the logs. |
| 54 | 54 | */ |
| 55 | - public function __construct( $class_name ) { |
|
| 55 | + public function __construct($class_name) { |
|
| 56 | 56 | |
| 57 | 57 | $this->class_name = $class_name; |
| 58 | 58 | |
@@ -67,13 +67,13 @@ discard block |
||
| 67 | 67 | */ |
| 68 | 68 | public static function get_instance() { |
| 69 | 69 | |
| 70 | - return self::$instance ?: self::$instance = new Wordlift_Log_Service( 'ROOT' ); |
|
| 70 | + return self::$instance ?: self::$instance = new Wordlift_Log_Service('ROOT'); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | |
| 74 | - public static function get_logger( $class_name ) { |
|
| 74 | + public static function get_logger($class_name) { |
|
| 75 | 75 | |
| 76 | - return new Wordlift_Log_Service( $class_name ); |
|
| 76 | + return new Wordlift_Log_Service($class_name); |
|
| 77 | 77 | |
| 78 | 78 | } |
| 79 | 79 | |
@@ -85,56 +85,56 @@ discard block |
||
| 85 | 85 | * @param string $level The log level. |
| 86 | 86 | * @param string $message The message to log. |
| 87 | 87 | */ |
| 88 | - public function log( $level, $message ) { |
|
| 88 | + public function log($level, $message) { |
|
| 89 | 89 | |
| 90 | 90 | // Bail out if WordLift log level isn't defined, and WP debug is disabled. |
| 91 | - if ( ! defined( 'WL_LOG_LEVEL' ) && $level < self::ERROR |
|
| 92 | - && ( ! defined( 'WP_DEBUG' ) || false === WP_DEBUG ) ) { |
|
| 91 | + if ( ! defined('WL_LOG_LEVEL') && $level < self::ERROR |
|
| 92 | + && ( ! defined('WP_DEBUG') || false === WP_DEBUG)) { |
|
| 93 | 93 | return; |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | // Bail out if the log message is below the minimum log level. |
| 97 | - if ( defined( 'WL_LOG_LEVEL' ) && $level < intval( WL_LOG_LEVEL ) ) { |
|
| 97 | + if (defined('WL_LOG_LEVEL') && $level < intval(WL_LOG_LEVEL)) { |
|
| 98 | 98 | return; |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | // Bail out if there's a filter and we don't match it. |
| 102 | - if ( defined( 'WL_LOG_FILTER' ) && 1 !== preg_match( "/(^|,)$this->class_name($|,)/", WL_LOG_FILTER ) ) { |
|
| 102 | + if (defined('WL_LOG_FILTER') && 1 !== preg_match("/(^|,)$this->class_name($|,)/", WL_LOG_FILTER)) { |
|
| 103 | 103 | return; |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | // Finally log the message. |
| 107 | - error_log( sprintf( self::MESSAGE_TEMPLATE, self::$levels[ $level ], $this->class_name, is_array( $message ) ? implode( ', ', $message ) : $message ) ); |
|
| 107 | + error_log(sprintf(self::MESSAGE_TEMPLATE, self::$levels[$level], $this->class_name, is_array($message) ? implode(', ', $message) : $message)); |
|
| 108 | 108 | |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | - public function error( $message ) { |
|
| 111 | + public function error($message) { |
|
| 112 | 112 | |
| 113 | - $this->log( self::ERROR, $message ); |
|
| 113 | + $this->log(self::ERROR, $message); |
|
| 114 | 114 | |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | - public function warn( $message ) { |
|
| 117 | + public function warn($message) { |
|
| 118 | 118 | |
| 119 | - $this->log( self::WARN, $message ); |
|
| 119 | + $this->log(self::WARN, $message); |
|
| 120 | 120 | |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | - public function info( $message ) { |
|
| 123 | + public function info($message) { |
|
| 124 | 124 | |
| 125 | - $this->log( self::INFO, $message ); |
|
| 125 | + $this->log(self::INFO, $message); |
|
| 126 | 126 | |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | - public function debug( $message ) { |
|
| 129 | + public function debug($message) { |
|
| 130 | 130 | |
| 131 | - $this->log( self::DEBUG, $message ); |
|
| 131 | + $this->log(self::DEBUG, $message); |
|
| 132 | 132 | |
| 133 | 133 | } |
| 134 | 134 | |
| 135 | - public function trace( $message ) { |
|
| 135 | + public function trace($message) { |
|
| 136 | 136 | |
| 137 | - $this->log( self::TRACE, $message ); |
|
| 137 | + $this->log(self::TRACE, $message); |
|
| 138 | 138 | |
| 139 | 139 | } |
| 140 | 140 | |