PluginProbe
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP / 1.2.25
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP v1.2.25
1.2.72 1.2.71 1.2.70 1.2.69 1.2.68 1.2.67 1.2.66 1.2.65 1.2.64 1.2.63 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 All 172 releases
userswp / includes / class-uwp-seo.php

class-uwp-seo.php in UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP 1.2.25, at includes/class-uwp-seo.php

343 lines 12.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 defined( 'ABSPATH' ) || exit;
4
5 class UsersWP_Seo {
6
7 public function __construct(){
8 add_action('pre_get_document_title', array($this,'output_title'));
9 add_action('uwp_profile_options', array($this,'profile_options'));
10 add_filter( 'plugins_loaded', array( $this, 'run_once_all_plugins_loaded' ) );
11 add_filter( 'get_canonical_url', array( $this, 'filter_profile_page_canonical' ) );
12
13 // Compatibility with SEOPress plugin
14 add_action( 'seopress_titles_title', array( $this, 'get_title' ) );
15 add_action( 'seopress_titles_desc', array( $this, 'get_description' ) );
16 }
17
18 public function run_once_all_plugins_loaded(){
19 if(UsersWP_Seo::has_yoast()) {
20 if ( UsersWP_Seo::has_yoast_14() ) {
21 add_filter( 'wpseo_opengraph_title', array( $this, 'get_title' ), 10);
22 add_filter( 'wpseo_opengraph_desc', array( $this, 'get_description' ), 10 );
23 add_filter( 'wpseo_opengraph_url', array( $this, 'get_opengraph_url' ), 20 );
24 }
25
26 add_filter( 'wpseo_title', array( $this, 'get_title' ), 10);
27 add_filter( 'wpseo_metadesc', array( $this, 'get_description' ), 10);
28 add_filter( 'wpseo_canonical', array( $this, 'filter_profile_page_canonical' ) );
29 }elseif ( defined( 'RANK_MATH_VERSION' ) ) {
30 add_filter( 'rank_math/frontend/description', array( $this,'get_description' ), 10, 1 );
31 add_filter( 'rank_math/frontend/title', array( $this, 'get_title' ), 10, 1 );
32 } else{
33 add_action( 'wp_head', array( $this, 'output_description' ) );
34 }
35
36 if ( uwp_is_wpml() ) {
37 add_filter( 'rewrite_rules_array', array( $this, 'rewrite_rules_array' ), 999, 1 );
38 }
39 }
40
41 public function profile_options($settings) {
42
43 $settings[] = array(
44 'title' => __( 'Profile SEO', 'userswp' ),
45 'type' => 'title',
46 'id' => 'profile_seo_options',
47 'advanced' => true,
48 );
49
50 $settings[] = array(
51 'id' => 'profile_seo_disable',
52 'name' => __( 'Disable meta tags', 'userswp' ),
53 'desc' => __( 'This will disable adding SEO meta tags on profile page.','userswp'),
54 'type' => 'checkbox',
55 'default' => 0,
56 'advanced' => false,
57 );
58
59 $settings[] = array(
60 'id' => 'profile_seo_meta_separator',
61 'name' => __( 'Title separator', 'userswp' ),
62 'type' => 'radio',
63 'default' => self::get_default_sep(),
64 'class' => 'uwp-seo-meta-separator',
65 'desc' => __('Choose the symbol to use as your title separator. This will display, for instance, between your user profile title and site name. Symbols are shown in the size they will appear in the search results.', 'uwp-groups'),
66 'desc_tip' => true,
67 'advanced' => true,
68 'placeholder' => '',
69 'options' => array(
70 '-' => '-',
71 '|' => '|',
72 '>' => '>',
73 '<' => '<',
74 '~' => '~',
75 ':' => ':',
76 '*' => '*',
77 ),
78 );
79
80 $settings[] = array(
81 'id' => 'profile_seo_meta_title',
82 'name' => __( 'Meta Title', 'userswp' ),
83 'type' => 'text',
84 'default' => '',
85 'class' => 'large-text',
86 'desc' => __('Available SEO tags:', 'uwp-groups') . ' '.self::get_seo_tags(true),
87 'desc_tip' => false,
88 'advanced' => true,
89 'placeholder' => $this->get_default_meta_title(),
90 );
91
92 $settings[] = array(
93 'id' => 'profile_seo_meta_description',
94 'name' => __( 'Meta Description', 'userswp' ),
95 'type' => 'textarea',
96 'default' => '',
97 'desc' => __( 'Enter the meta description to use for the page.', 'userswp' ),
98 'desc_tip' => true,
99 'advanced' => true,
100 'placeholder' => $this->get_default_meta_description(),
101 'custom_desc' => __('Available SEO tags:', 'uwp-groups') . ' '.self::get_seo_tags(true),
102 );
103
104 $settings[] = array(
105 'id' => 'profile_seo_meta_description_length',
106 'name' => __( 'Meta Description Length', 'userswp' ),
107 'type' => 'number',
108 'default' => 150,
109 'advanced' => true,
110 );
111
112 $settings[] = array( 'type' => 'sectionend', 'id' => 'profile_seo_options' );
113
114 return $settings;
115 }
116
117 public static function get_default_sep() {
118 return apply_filters('uwp_profile_seo_default_separator', '|');
119 }
120
121 public static function get_seo_tags( $inline = true ) {
122
123 $tags = array(
124 '[#site_name#]',
125 '[#user_name#]',
126 '[#display_name#]',
127 '[#first_name#]',
128 '[#last_name#]',
129 '[#email#]',
130 '[#user_bio#]',
131 '[#sep#]',
132 );
133
134 $tags = apply_filters('uwp_seo_tags',$tags);
135
136 if(!$inline) {
137 return $tags;
138 }
139
140 return '<code>' . implode( '</code> <code>', $tags ) . '</code>';
141 }
142
143 public function get_default_meta_title() {
144 return '[#site_name#] [#sep#] [#user_name#]';
145 }
146
147 public function get_default_meta_description() {
148 return '[#user_bio#]';
149 }
150
151 public function replace_tags($string) {
152
153 if(is_uwp_profile_page()) {
154 $displayed_user = uwp_get_displayed_user();
155 if(!$displayed_user){
156 return $string;
157 }
158 $site_name = esc_attr( get_bloginfo('name') );
159 $first_name = !empty($displayed_user->first_name) ? esc_attr( $displayed_user->first_name ) :'';
160 $last_name = !empty($displayed_user->last_name) ? esc_attr( $displayed_user->last_name ) :'';
161 $user_name = !empty($displayed_user->user_login) ? esc_attr( $displayed_user->user_login ) :'';
162 $display_name = !empty($displayed_user->display_name) ? esc_attr( $displayed_user->display_name ) :'';
163 $user_email = !empty($displayed_user->user_email) ? esc_url( $displayed_user->user_email ) :'';
164 $user_bio = !empty($displayed_user->description) ? strip_tags($displayed_user->description) :'';
165
166 $meta_separator = uwp_get_option('profile_seo_meta_separator');
167 $sep = !empty($meta_separator) ? $meta_separator : self::get_default_sep();
168
169 $string = str_replace('[#site_name#]', $site_name, $string);
170 $string = str_replace('[#user_name#]', $user_name, $string);
171 $string = str_replace('[#display_name#]', $display_name, $string);
172 $string = str_replace('[#first_name#]', $first_name, $string);
173 $string = str_replace('[#last_name#]', $last_name, $string);
174 $string = str_replace('[#email#]', $user_email, $string);
175 $string = str_replace('[#user_bio#]', $user_bio, $string);
176 $string = str_replace('[#sep#]', $sep, $string);
177 }
178
179 return $string;
180 }
181
182 public function get_meta_title() {
183
184 $meta_title = uwp_get_option('profile_seo_meta_title');
185 $meta_title = !empty($meta_title) ? $meta_title : $this->get_default_meta_title();
186 $meta_title = $this->replace_tags($meta_title);
187
188 return esc_html($meta_title);
189 }
190
191 public function get_meta_description() {
192
193 $meta_description = uwp_get_option('profile_seo_meta_description');
194 $meta_description = !empty($meta_description) ? $meta_description : $this->get_default_meta_description();
195 $meta_description = $this->replace_tags($meta_description);
196
197 return esc_html($meta_description);
198 }
199
200 public function output_title($title) {
201 if(1 == uwp_get_option('profile_seo_disable')){
202 return $title;
203 }
204
205 if(is_uwp_profile_page()) {
206 $title = $this->get_meta_title();
207 }
208
209 return $title;
210 }
211
212 public function output_description() {
213
214 if(is_uwp_profile_page()) {
215 $description = $this->get_description();
216 echo '<meta name="description" content="' . esc_attr( $description ) . '" />';
217 }
218 }
219
220 public static function has_yoast() {
221 return defined( 'WPSEO_VERSION' );
222 }
223
224 public static function has_yoast_14() {
225 return ( self::has_yoast() && version_compare( WPSEO_VERSION, '14.0', '>=' ) );
226 }
227
228 public function get_title($title) {
229 if(1 == uwp_get_option('profile_seo_disable')){
230 return $title;
231 }
232
233 if(is_uwp_profile_page()) {
234 $title = $this->get_meta_title();
235 }
236
237 return apply_filters('uwp_seo_profile_meta_title', $title);
238 }
239
240 public function get_description($description = '') {
241 if(1 == uwp_get_option('profile_seo_disable')){
242 return $description;
243 }
244
245 if(is_uwp_profile_page()) {
246 $description = $this->get_meta_description();
247 }
248
249 $length = uwp_get_option('profile_seo_meta_description_length', 150);
250
251 return apply_filters('uwp_seo_profile_meta_description', substr($description, 0, $length), $description);
252 }
253
254 public function get_opengraph_url($url) {
255 if(1 == uwp_get_option('profile_seo_disable')){
256 return $url;
257 }
258
259 if(is_uwp_profile_page()) {
260 $displayed_user = uwp_get_displayed_user();
261 $displayed_user_id = !empty($displayed_user->ID) ? $displayed_user->ID : 0;
262 $url = uwp_build_profile_tab_url($displayed_user_id);
263 }
264
265 return $url;
266 }
267
268 /**
269 * Filter the rewrite rules to work with translated profile slugs.
270 *
271 * @since 1.2.3.7
272 *
273 * @param array $rules Rewrite rules.
274 * @return array Filtered rewrite rules.
275 */
276 public function rewrite_rules_array( $rules ) {
277 global $sitepress;
278
279 $page_id = uwp_get_page_id( 'profile_page', false );
280 if ( empty( $page_id ) ) {
281 return $rules;
282 }
283
284 $trid = $sitepress->get_element_trid( $page_id, 'post_page' );
285 if ( empty( $trid ) ) {
286 return $rules;
287 }
288
289 $page_slug = get_post_field( 'post_name', $page_id );
290 $tr_page_slugs = array();
291
292 if ( $translations = $sitepress->get_element_translations( $trid, 'post_page' ) ) {
293 foreach ( $translations as $lang => $translation ) {
294 if ( ! empty( $translation->element_id ) ) {
295 $tr_page_slug = get_post_field( 'post_name', $translation->element_id );
296
297 if ( $tr_page_slug && $tr_page_slug != $page_slug && ! in_array( $tr_page_slug, $tr_page_slugs ) ) {
298 $tr_page_slugs[ $translation->element_id ] = $tr_page_slug;
299 }
300 }
301 }
302 }
303
304 if ( ! empty( $tr_page_slugs ) ) {
305 $_rules = $rules;
306 $rules = array();
307
308 foreach ( $_rules as $regex => $query ) {
309 $rules[ $regex ] = $query;
310
311 if ( strpos( $regex, '^' . $page_slug . '/' ) !== false && strpos( $query, 'page_id=' . $page_id . '&' ) !== false ) {
312 foreach ( $tr_page_slugs as $tr_page_id => $tr_page_slug ) {
313 $_regex = str_replace( '^' . $page_slug . '/', '^' . $tr_page_slug . '/', $regex );
314 $_query = str_replace( 'page_id=' . $page_id . '&', 'page_id=' . $tr_page_id . '&', $query );
315
316 $rules[ $_regex ] = $_query;
317 }
318 }
319 }
320 }
321
322 return $rules;
323 }
324
325 /**
326 * Filters the canonical URL.
327 *
328 * @param string $canonical The current page's generated canonical URL.
329 *
330 * @return string The filtered canonical URL.
331 */
332 function filter_profile_page_canonical( $canonical ) {
333 $page = new UsersWP_Pages();
334 if ( $page->is_profile_page() ) {
335 $canonical = get_author_posts_url( get_current_user_id() );
336 }
337
338 return $canonical;
339 }
340
341 }
342
343 new UsersWP_Seo();