/var/www/hkosl.com/aga/wp-content/plugins/wordpress-seo/frontend/schema/class-schema-article.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
 * WPSEO plugin file.
 *
 * @package WPSEO\Frontend\Schema
 */

/**
 * Returns schema Article data.
 *
 * @since 10.2
 */
class WPSEO_Schema_Article implements WPSEO_Graph_Piece {
    
/**
     * A value object with context variables.
     *
     * @var WPSEO_Schema_Context
     */
    
private $context;

    
/**
     * WPSEO_Schema_Article constructor.
     *
     * @param WPSEO_Schema_Context $context A value object with context variables.
     */
    
public function __constructWPSEO_Schema_Context $context ) {
        
$this->context $context;
    }

    
/**
     * Determines whether or not a piece should be added to the graph.
     *
     * @return bool
     */
    
public function is_needed() {
        if ( ! 
is_singular() ) {
            return 
false;
        }

        if ( 
$this->context->site_represents === false ) {
            return 
false;
        }

        return 
self::is_article_post_typeget_post_type() );
    }

    
/**
     * Returns Article data.
     *
     * @return array $data Article data.
     */
    
public function generate() {
        
$post          get_post$this->context->id );
        
$comment_count get_comment_count$this->context->id );
        
$data          = array(
            
'@type'            => 'Article',
            
'@id'              => $this->context->canonical WPSEO_Schema_IDs::ARTICLE_HASH,
            
'isPartOf'         => array( '@id' => $this->context->canonical WPSEO_Schema_IDs::WEBPAGE_HASH ),
            
'author'           => array( '@id' => WPSEO_Schema_Utils::get_user_schema_id$post->post_author$this->context ) ),
            
'headline'         => get_the_title(),
            
'datePublished'    => mysql2dateDATE_W3C$post->post_date_gmtfalse ),
            
'dateModified'     => mysql2dateDATE_W3C$post->post_modified_gmtfalse ),
            
'commentCount'     => $comment_count['approved'],
            
'mainEntityOfPage' => array( '@id' => $this->context->canonical WPSEO_Schema_IDs::WEBPAGE_HASH ),
        );

        if ( 
$this->context->site_represents_reference ) {
            
$data['publisher'] = $this->context->site_represents_reference;
        }

        
$data $this->add_image$data );
        
$data $this->add_keywords$data );
        
$data $this->add_sections$data );

        return 
$data;
    }

    
/**
     * Determines whether a given post type should have Article schema.
     *
     * @param string $post_type Post type to check.
     *
     * @return bool True if it has article schema, false if not.
     */
    
public static function is_article_post_type$post_type null ) {
        if ( 
is_null$post_type ) ) {
            
$post_type get_post_type();
        }

        
/**
         * Filter: 'wpseo_schema_article_post_types' - Allow changing for which post types we output Article schema.
         *
         * @api string[] $post_types The post types for which we output Article.
         */
        
$post_types apply_filters'wpseo_schema_article_post_types', array( 'post' ) );

        return 
in_array$post_type$post_types );
    }

    
/**
     * Adds tags as keywords, if tags are assigned.
     *
     * @param array $data Article data.
     *
     * @return array $data Article data.
     */
    
private function add_keywords$data ) {
        
/**
         * Filter: 'wpseo_schema_article_keywords_taxonomy' - Allow changing the taxonomy used to assign keywords to a post type Article data.
         *
         * @api string $taxonomy The chosen taxonomy.
         */
        
$taxonomy apply_filters'wpseo_schema_article_keywords_taxonomy''post_tag' );

        return 
$this->add_terms$data'keywords'$taxonomy );
    }

    
/**
     * Adds categories as sections, if categories are assigned.
     *
     * @param array $data Article data.
     *
     * @return array $data Article data.
     */
    
private function add_sections$data ) {
        
/**
         * Filter: 'wpseo_schema_article_sections_taxonomy' - Allow changing the taxonomy used to assign keywords to a post type Article data.
         *
         * @api string $taxonomy The chosen taxonomy.
         */
        
$taxonomy apply_filters'wpseo_schema_article_sections_taxonomy''category' );

        return 
$this->add_terms$data'articleSection'$taxonomy );
    }

    
/**
     * Adds a term or multiple terms, comma separated, to a field.
     *
     * @param array  $data     Article data.
     * @param string $key      The key in data to save the terms in.
     * @param string $taxonomy The taxonomy to retrieve the terms from.
     *
     * @return mixed array $data Article data.
     */
    
private function add_terms$data$key$taxonomy ) {
        
$terms get_the_terms$this->context->id$taxonomy );
        if ( 
is_array$terms ) ) {
            
$keywords = array();
            foreach ( 
$terms as $term ) {
                
// We are checking against the WordPress internal translation.
                // @codingStandardsIgnoreLine
                
if ( $term->name !== __'Uncategorized' ) ) {
                    
$keywords[] = $term->name;
                }
            }
            
$data$key ] = implode','$keywords );
        }

        return 
$data;
    }

    
/**
     * Adds an image node if the post has a featured image.
     *
     * @param array $data The Article data.
     *
     * @return array $data The Article data.
     */
    
private function add_image$data ) {
        if ( 
$this->context->has_image ) {
            
$data['image'] = array(
                
'@id' => $this->context->canonical WPSEO_Schema_IDs::PRIMARY_IMAGE_HASH,
            );
        }

        return 
$data;
    }
}