class-snippet-preview.php
4.15 KB
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
<?php
/**
* @package WPSEO\Admin
* @since 1.6.2
*/
/**
* Class WPSEO_Snippet_Preview
*
* Generates a Google Search snippet preview.
*
* Takes a $post, $title and $description
*/
class WPSEO_Snippet_Preview {
/**
* @var string The dynamically generated html for the snippet preview.
*/
protected $content;
/**
* @var array The WPSEO options.
*/
protected $options;
/**
* @var object The post for which we want to generate the snippet preview.
*/
protected $post;
/**
* @var string The title that is shown in the snippet.
*/
protected $title;
/**
* @var string The description that is shown in the snippet.
*/
protected $description;
/**
* @var string The date that is shown at the beginning of the description in the snippet.
*/
protected $date = '';
/**
* @var string The url that is shown in the snippet.
*/
protected $url;
/**
* @var string The slug of the url that is shown in the snippet.
*/
protected $slug = '';
/**
* Generates the html for the snippet preview containing dynamically generated text components.
* Those components are included as properties which are set in the constructor.
*
* @param object $post
* @param string $title
* @param string $description
*/
public function __construct( $post, $title, $description ) {
$this->options = WPSEO_Options::get_all();
$this->post = $post;
$this->title = esc_html( $title );
$this->description = esc_html( $description );
$this->set_date();
$this->set_url();
$this->set_content();
}
/**
* Getter for $this->content
* @return string html for snippet preview
*/
public function get_content() {
return $this->content;
}
/**
* Sets date if available
*/
protected function set_date() {
if ( is_object( $this->post ) && isset( $this->options[ 'showdate-' . $this->post->post_type ] ) && $this->options[ 'showdate-' . $this->post->post_type ] === true ) {
$date = $this->get_post_date();
$this->date = '<span class="date">' . $date . ' - </span>';
}
}
/**
* Retrieves a post date when post is published, or return current date when it's not.
*
* @return string
*/
protected function get_post_date() {
if ( isset( $this->post->post_date ) && $this->post->post_status == 'publish' ) {
$date = date_i18n( 'j M Y', strtotime( $this->post->post_date ) );
}
else {
$date = date_i18n( 'j M Y' );
}
return (string) $date;
}
/**
* Generates the url that is displayed in the snippet preview.
*/
protected function set_url() {
$this->url = str_replace( array( 'http://', 'https://' ), '', get_bloginfo( 'url' ) ) . '/';
$this->set_slug();
}
/**
* Sets the slug and adds it to the url if the post has been published and the post name exists.
*
* If the post is set to be the homepage the slug is also not included.
*/
protected function set_slug() {
$frontpage_post_id = (int) ( get_option( 'page_on_front' ) );
if ( is_object( $this->post ) && isset( $this->post->post_name ) && $this->post->post_name !== '' && $this->post->ID !== $frontpage_post_id ) {
$this->slug = sanitize_title( $this->title );
$this->url .= esc_html( $this->slug );
}
}
/**
* Generates the html for the snippet preview and assign it to $this->content.
*/
protected function set_content() {
$content = <<<HTML
<div id="wpseosnippet">
<a class="title" id="wpseosnippet_title" href="#">$this->title</a>
<span class="url">$this->url</span>
<p class="desc">$this->date<span class="autogen"></span><span class="content">$this->description</span></p>
</div>
HTML;
$this->set_content_through_filter( $content );
}
/**
* Sets the html for the snippet preview through a filter
*
* @param string $content
*/
protected function set_content_through_filter( $content ) {
$properties = get_object_vars( $this );
// Backward compatibility for functions hooking into the wpseo_snippet filter.
$properties['desc'] = $properties['description'];
/**
* Filter: 'wpseo_snippet' - Allow changing the html for the snippet preview.
*
* Passing in the post twice because of backwards compatibility.
*/
$this->content = apply_filters( 'wpseo_snippet', $content, $this->post, $properties );
}
}