| 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 |
|
| 11 |
// Compatibility with SEOPress plugin |
| 12 |
add_action( 'seopress_titles_title', array( $this, 'get_title' ) ); |
| 13 |
add_action( 'seopress_titles_desc', array( $this, 'get_description' ) ); |
| 14 |
|
| 15 |
if(UsersWP_Seo::has_yoast()) { |
| 16 |
if ( UsersWP_Seo::has_yoast_14() ) { |
| 17 |
add_filter( 'wpseo_opengraph_title', array( $this, 'get_title' ), 10); |
| 18 |
add_filter( 'wpseo_opengraph_desc', array( $this, 'get_description' ), 10 ); |
| 19 |
add_filter( 'wpseo_opengraph_url', array( $this, 'get_opengraph_url' ), 20 ); |
| 20 |
} |
| 21 |
|
| 22 |
add_filter( 'wpseo_title', array( $this, 'get_title' ), 10); |
| 23 |
add_filter( 'wpseo_metadesc', array( $this, 'get_description' ), 10); |
| 24 |
}elseif ( defined( 'RANK_MATH_VERSION' ) ) { |
| 25 |
add_filter( 'rank_math/frontend/description', array( $this,'get_description' ), 10, 1 ); |
| 26 |
add_filter( 'rank_math/frontend/title', array( $this, 'get_title' ), 10, 1 ); |
| 27 |
} else{ |
| 28 |
add_action( 'wp_head', array( $this, 'output_description' ) ); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
public function profile_options($settings) { |
| 33 |
|
| 34 |
$settings[] = array( |
| 35 |
'title' => __( 'Profile SEO', 'userswp' ), |
| 36 |
'type' => 'title', |
| 37 |
'id' => 'profile_seo_options', |
| 38 |
'advanced' => true, |
| 39 |
); |
| 40 |
|
| 41 |
$settings[] = array( |
| 42 |
'id' => 'profile_seo_disable', |
| 43 |
'name' => __( 'Disable meta tags', 'userswp' ), |
| 44 |
'desc' => __( 'This will disable adding SEO meta tags on profile page.','userswp'), |
| 45 |
'type' => 'checkbox', |
| 46 |
'default' => 0, |
| 47 |
'advanced' => false, |
| 48 |
); |
| 49 |
|
| 50 |
$settings[] = array( |
| 51 |
'id' => 'profile_seo_meta_separator', |
| 52 |
'name' => __( 'Title separator', 'userswp' ), |
| 53 |
'type' => 'radio', |
| 54 |
'default' => self::get_default_sep(), |
| 55 |
'class' => 'uwp-seo-meta-separator', |
| 56 |
'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'), |
| 57 |
'desc_tip' => true, |
| 58 |
'advanced' => true, |
| 59 |
'placeholder' => '', |
| 60 |
'options' => array( |
| 61 |
'-' => '-', |
| 62 |
'|' => '|', |
| 63 |
'>' => '>', |
| 64 |
'<' => '<', |
| 65 |
'~' => '~', |
| 66 |
':' => ':', |
| 67 |
'*' => '*', |
| 68 |
), |
| 69 |
); |
| 70 |
|
| 71 |
$settings[] = array( |
| 72 |
'id' => 'profile_seo_meta_title', |
| 73 |
'name' => __( 'Meta Title', 'userswp' ), |
| 74 |
'type' => 'text', |
| 75 |
'default' => '', |
| 76 |
'class' => 'large-text', |
| 77 |
'desc' => __('Available SEO tags:', 'uwp-groups') . ' '.self::get_seo_tags(true), |
| 78 |
'desc_tip' => false, |
| 79 |
'advanced' => true, |
| 80 |
'placeholder' => $this->get_default_meta_title(), |
| 81 |
); |
| 82 |
|
| 83 |
$settings[] = array( |
| 84 |
'id' => 'profile_seo_meta_description', |
| 85 |
'name' => __( 'Meta Description', 'userswp' ), |
| 86 |
'type' => 'textarea', |
| 87 |
'default' => '', |
| 88 |
'desc' => __( 'Enter the meta description to use for the page.', 'userswp' ), |
| 89 |
'desc_tip' => true, |
| 90 |
'advanced' => true, |
| 91 |
'placeholder' => $this->get_default_meta_description(), |
| 92 |
'custom_desc' => __('Available SEO tags:', 'uwp-groups') . ' '.self::get_seo_tags(true), |
| 93 |
); |
| 94 |
|
| 95 |
$settings[] = array( |
| 96 |
'id' => 'profile_seo_meta_description_length', |
| 97 |
'name' => __( 'Meta Description Length', 'userswp' ), |
| 98 |
'type' => 'number', |
| 99 |
'default' => 150, |
| 100 |
'advanced' => true, |
| 101 |
); |
| 102 |
|
| 103 |
$settings[] = array( 'type' => 'sectionend', 'id' => 'profile_seo_options' ); |
| 104 |
|
| 105 |
return $settings; |
| 106 |
} |
| 107 |
|
| 108 |
public static function get_default_sep() { |
| 109 |
return apply_filters('uwp_profile_seo_default_separator', '|'); |
| 110 |
} |
| 111 |
|
| 112 |
public static function get_seo_tags( $inline = true ) { |
| 113 |
|
| 114 |
$tags = array( |
| 115 |
'[#site_name#]', |
| 116 |
'[#user_name#]', |
| 117 |
'[#display_name#]', |
| 118 |
'[#first_name#]', |
| 119 |
'[#last_name#]', |
| 120 |
'[#email#]', |
| 121 |
'[#user_bio#]', |
| 122 |
'[#sep#]', |
| 123 |
); |
| 124 |
|
| 125 |
$tags = apply_filters('uwp_seo_tags',$tags); |
| 126 |
|
| 127 |
if(!$inline) { |
| 128 |
return $tags; |
| 129 |
} |
| 130 |
|
| 131 |
return '<code>' . implode( '</code> <code>', $tags ) . '</code>'; |
| 132 |
} |
| 133 |
|
| 134 |
public function get_default_meta_title() { |
| 135 |
return '[#site_name#] [#sep#] [#user_name#]'; |
| 136 |
} |
| 137 |
|
| 138 |
public function get_default_meta_description() { |
| 139 |
return '[#user_bio#]'; |
| 140 |
} |
| 141 |
|
| 142 |
public function replace_tags($string) { |
| 143 |
|
| 144 |
if(is_uwp_profile_page()) { |
| 145 |
$displayed_user = uwp_get_displayed_user(); |
| 146 |
if(!$displayed_user){ |
| 147 |
return $string; |
| 148 |
} |
| 149 |
$site_name = esc_attr( get_bloginfo('name') ); |
| 150 |
$first_name = !empty($displayed_user->first_name) ? esc_attr( $displayed_user->first_name ) :''; |
| 151 |
$last_name = !empty($displayed_user->last_name) ? esc_attr( $displayed_user->last_name ) :''; |
| 152 |
$user_name = !empty($displayed_user->user_login) ? esc_attr( $displayed_user->user_login ) :''; |
| 153 |
$display_name = !empty($displayed_user->display_name) ? esc_attr( $displayed_user->display_name ) :''; |
| 154 |
$user_email = !empty($displayed_user->user_email) ? esc_url( $displayed_user->user_email ) :''; |
| 155 |
$user_bio = !empty($displayed_user->description) ? strip_tags($displayed_user->description) :''; |
| 156 |
|
| 157 |
$meta_separator = uwp_get_option('profile_seo_meta_separator'); |
| 158 |
$sep = !empty($meta_separator) ? $meta_separator : self::get_default_sep(); |
| 159 |
|
| 160 |
$string = str_replace('[#site_name#]', $site_name, $string); |
| 161 |
$string = str_replace('[#user_name#]', $user_name, $string); |
| 162 |
$string = str_replace('[#display_name#]', $display_name, $string); |
| 163 |
$string = str_replace('[#first_name#]', $first_name, $string); |
| 164 |
$string = str_replace('[#last_name#]', $last_name, $string); |
| 165 |
$string = str_replace('[#email#]', $user_email, $string); |
| 166 |
$string = str_replace('[#user_bio#]', $user_bio, $string); |
| 167 |
$string = str_replace('[#sep#]', $sep, $string); |
| 168 |
} |
| 169 |
|
| 170 |
return $string; |
| 171 |
} |
| 172 |
|
| 173 |
public function get_meta_title() { |
| 174 |
|
| 175 |
$meta_title = uwp_get_option('profile_seo_meta_title'); |
| 176 |
$meta_title = !empty($meta_title) ? $meta_title : $this->get_default_meta_title(); |
| 177 |
$meta_title = $this->replace_tags($meta_title); |
| 178 |
|
| 179 |
return esc_html($meta_title); |
| 180 |
} |
| 181 |
|
| 182 |
public function get_meta_description() { |
| 183 |
|
| 184 |
$meta_description = uwp_get_option('profile_seo_meta_description'); |
| 185 |
$meta_description = !empty($meta_description) ? $meta_description : $this->get_default_meta_description(); |
| 186 |
$meta_description = $this->replace_tags($meta_description); |
| 187 |
|
| 188 |
return esc_html($meta_description); |
| 189 |
} |
| 190 |
|
| 191 |
public function output_title($title) { |
| 192 |
if(1 == uwp_get_option('profile_seo_disable')){ |
| 193 |
return $title; |
| 194 |
} |
| 195 |
|
| 196 |
if(is_uwp_profile_page()) { |
| 197 |
$title = $this->get_meta_title(); |
| 198 |
} |
| 199 |
|
| 200 |
return $title; |
| 201 |
} |
| 202 |
|
| 203 |
public function output_description() { |
| 204 |
|
| 205 |
if(is_uwp_profile_page()) { |
| 206 |
$description = $this->get_description(); |
| 207 |
echo '<meta name="description" content="' . $description . '" />'; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
public static function has_yoast() { |
| 212 |
return defined( 'WPSEO_VERSION' ); |
| 213 |
} |
| 214 |
|
| 215 |
public static function has_yoast_14() { |
| 216 |
return ( self::has_yoast() && version_compare( WPSEO_VERSION, '14.0', '>=' ) ); |
| 217 |
} |
| 218 |
|
| 219 |
public function get_title($title) { |
| 220 |
if(1 == uwp_get_option('profile_seo_disable')){ |
| 221 |
return $title; |
| 222 |
} |
| 223 |
|
| 224 |
if(is_uwp_profile_page()) { |
| 225 |
$title = $this->get_meta_title(); |
| 226 |
} |
| 227 |
|
| 228 |
return apply_filters('uwp_seo_profile_meta_title', $title); |
| 229 |
} |
| 230 |
|
| 231 |
public function get_description($description = '') { |
| 232 |
if(1 == uwp_get_option('profile_seo_disable')){ |
| 233 |
return $description; |
| 234 |
} |
| 235 |
|
| 236 |
if(is_uwp_profile_page()) { |
| 237 |
$description = $this->get_meta_description(); |
| 238 |
} |
| 239 |
|
| 240 |
$length = uwp_get_option('profile_seo_meta_description_length', 150); |
| 241 |
|
| 242 |
return apply_filters('uwp_seo_profile_meta_description', substr($description, 0, $length), $description); |
| 243 |
} |
| 244 |
|
| 245 |
public function get_opengraph_url($url) { |
| 246 |
if(1 == uwp_get_option('profile_seo_disable')){ |
| 247 |
return $url; |
| 248 |
} |
| 249 |
|
| 250 |
if(is_uwp_profile_page()) { |
| 251 |
$displayed_user = uwp_get_displayed_user(); |
| 252 |
$displayed_user_id = !empty($displayed_user->ID) ? $displayed_user->ID : 0; |
| 253 |
$url = uwp_build_profile_tab_url($displayed_user_id); |
| 254 |
} |
| 255 |
|
| 256 |
return $url; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
new UsersWP_Seo(); |