PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/seo-ultimate/modules/meta/meta-descriptions.php

https://gitlab.com/Gashler/sg
PHP | 240 lines | 183 code | 48 blank | 9 comment | 18 complexity | c3277dd4be063f2dd314de4e819afa07 MD5 | raw file
  1. <?php
  2. /**
  3. * Meta Description Editor Module
  4. *
  5. * @since 4.0
  6. */
  7. if (class_exists('SU_Module')) {
  8. function su_meta_descriptions_export_filter($all_settings) {
  9. unset($all_settings['meta']['taxonomy_descriptions']);
  10. return $all_settings;
  11. }
  12. add_filter('su_settings_export_array', 'su_meta_descriptions_export_filter');
  13. class SU_MetaDescriptions extends SU_Module {
  14. static function get_module_title() { return __('Meta Description Editor', 'seo-ultimate'); }
  15. static function get_menu_title() { return __('Meta Descriptions', 'seo-ultimate'); }
  16. function get_settings_key() { return 'meta'; }
  17. function init() {
  18. add_action('su_head', array(&$this, 'head_tag_output'));
  19. add_filter('su_postmeta_help', array(&$this, 'postmeta_help'), 20);
  20. }
  21. function get_admin_page_tabs() {
  22. return array_merge(
  23. array(
  24. array('title' => __('Default Formats', 'seo-ultimate'), 'id' => 'su-default-formats', 'callback' => 'formats_tab')
  25. , array('title' => __('Blog Homepage', 'seo-ultimate'), 'id' => 'su-blog-homepage', 'callback' => 'home_tab')
  26. )
  27. , $this->get_meta_edit_tabs(array(
  28. 'type' => 'textarea'
  29. , 'name' => 'description'
  30. , 'term_settings_key' => 'taxonomy_descriptions'
  31. , 'label' => __('Meta Description', 'seo-ultimate')
  32. ))
  33. );
  34. }
  35. function get_default_settings() {
  36. return array(
  37. 'home_description_tagline_default' => true
  38. , 'description_posttype_post' => '{excerpt}'
  39. , 'description_posttype_page' => ''
  40. , 'description_taxonomy_category' => '{description}'
  41. , 'description_taxonomy_post_tag' => '{description}'
  42. , 'description_paged' => '{meta_description} - Page {num}'
  43. );
  44. }
  45. function formats_tab() {
  46. $this->admin_form_table_start();
  47. $this->textboxes(array(
  48. 'description_posttype_post' => __('Post Description Format', 'seo-ultimate')
  49. , 'description_posttype_page' => __('Page Description Format', 'seo-ultimate')
  50. , 'description_taxonomy_category' => __('Category Description Format', 'seo-ultimate')
  51. , 'description_taxonomy_post_tag' => __('Post Tag Description Format', 'seo-ultimate')
  52. , 'description_paged' => __('Pagination Description Format', 'seo-ultimate')
  53. ), $this->get_default_settings());
  54. $this->admin_form_table_end();
  55. }
  56. function home_tab() {
  57. $this->admin_form_table_start();
  58. $this->textarea('home_description', __('Blog Homepage Meta Description', 'seo-ultimate'), 3);
  59. $this->checkboxes(array(
  60. 'home_description_tagline_default' => __('Use this blog&#8217s tagline as the default homepage description.', 'seo-ultimate')
  61. ), __('Default Value', 'seo-ultimate'));
  62. $this->admin_form_table_end();
  63. }
  64. function head_tag_output() {
  65. $desc = $this->get_meta_desc();
  66. //Do we have a description? If so, output it.
  67. if ($desc)
  68. echo "\t<meta name=\"description\" content=\"$desc\" />\n";
  69. }
  70. function get_meta_desc() {
  71. global $post;
  72. $desc = false;
  73. //If we're viewing the homepage, look for homepage meta data.
  74. if (is_home()) {
  75. $desc = $this->get_setting('home_description');
  76. if (!$desc && $this->get_setting('home_description_tagline_default')) $desc = get_bloginfo('description');
  77. //If we're viewing a post or page, look for its meta data.
  78. } elseif (is_singular()) {
  79. $desc = $this->get_postmeta('description');
  80. if (!trim($desc) && !post_password_required() && $format = $this->get_setting('description_posttype_'.get_post_type())) {
  81. $auto_excerpt = $post->post_content;
  82. $auto_excerpt = strip_shortcodes($auto_excerpt);
  83. $auto_excerpt = str_replace(']]>', ']]&gt;', $auto_excerpt);
  84. $auto_excerpt = strip_tags($auto_excerpt);
  85. $auto_excerpt = sustr::truncate($auto_excerpt, 150, '', true);
  86. $desc = str_replace(
  87. array('{excerpt::autogen}', '{excerpt}')
  88. , array($auto_excerpt, strip_tags($post->post_excerpt))
  89. , $format);
  90. }
  91. //If we're viewing a term, look for its meta data.
  92. } elseif (suwp::is_tax()) {
  93. global $wp_query;
  94. $tax_descriptions = $this->get_setting('taxonomy_descriptions');
  95. $term_id = $wp_query->get_queried_object_id();
  96. $term_obj = $wp_query->get_queried_object();
  97. $desc = isset($tax_descriptions[$term_id]) ? $tax_descriptions[$term_id] : '';
  98. if (!trim($desc) && $format = $this->get_setting('description_taxonomy_'.$term_obj->taxonomy)) {
  99. $desc = str_replace(
  100. array('{description}')
  101. , array($term_obj->description)
  102. , $format);
  103. }
  104. }
  105. $desc = trim($desc);
  106. if ($desc)
  107. $desc = $this->get_desc_paged($desc);
  108. $desc = trim($desc);
  109. $desc = su_esc_attr($desc);
  110. return $desc;
  111. }
  112. function get_desc_paged($desc) {
  113. global $wp_query, $numpages;
  114. if (is_paged() || get_query_var('page')) {
  115. if (is_paged()) {
  116. $num = absint(get_query_var('paged'));
  117. $max = absint($wp_query->max_num_pages);
  118. } else {
  119. $num = absint(get_query_var('page'));
  120. if (is_singular()) {
  121. $post = $wp_query->get_queried_object();
  122. $max = count(explode('<!--nextpage-->', $post->post_content));
  123. } else
  124. $max = '';
  125. }
  126. return str_replace(
  127. array('{meta_description}', '{num}', '{max}'),
  128. array( $desc, $num, $max ),
  129. $this->get_setting('description_paged'));
  130. } else
  131. return $desc;
  132. }
  133. function postmeta_fields($fields, $screen) {
  134. $id = '_su_description';
  135. $value = su_esc_attr($this->get_postmeta('description'));
  136. $fields['serp'][20]['description'] =
  137. "<div class='form-group su textarea'>\n<label class='col-sm-4 col-md-4 control-label' for='$id'>".__('Meta Description:', 'seo-ultimate')."</label>\n<div class='col-sm-4 col-md-4'>"
  138. . "<textarea name='$id' id='$id' class='form-control regular-text' cols='60' rows='3' tabindex='2'"
  139. . " onkeyup=\"javascript:document.getElementById('su_meta_description_charcount').innerHTML = document.getElementById('_su_description').value.length\">$value</textarea>"
  140. . "</div>\n<div class='col-sm-4 col-md-4 help-text'>".sprintf(__('You&#8217;ve entered %s characters. Most search engines use up to 140.', 'seo-ultimate'), "<strong id='su_meta_description_charcount'>".strlen($value)."</strong>")
  141. . "</div>\n</div>\n";
  142. return $fields;
  143. }
  144. function postmeta_help($help) {
  145. $help[] = __('<strong>Meta Description</strong> &mdash; The value of the meta description tag. The description will often appear underneath the title in search engine results. Writing an accurate, attention-grabbing description for every post is important to ensuring a good search results clickthrough rate.', 'seo-ultimate');
  146. return $help;
  147. }
  148. function add_help_tabs($screen) {
  149. $screen->add_help_tab(array(
  150. 'id' => 'su-meta-descriptions-overview'
  151. , 'title' => __('Overview', 'seo-ultimate')
  152. , 'content' => __("
  153. <ul>
  154. <li><strong>What it does:</strong> Meta Descriptions Editor lets you customize the text that you want to appear under your webpages&#8217; titles in search results.</li>
  155. <li><strong>Why it helps:</strong> Getting ranked isn&#8217;t enough; once you're ranked, you need visitors to click on your site in the results. That&#8217;s where meta descriptions can help. When you provide text that makes searchers want to visit your site, you can increase your SERP clickthrough rate and thus increase search traffic.</li>
  156. <li><strong>How to use it:</strong> Enter meta descriptions for your homepage, posts, pages, etc. as desired, and then click Save Changes. You can also customize the meta data of an individual post or page by using the textboxes that Meta Editor adds to the post/page editors.</li>
  157. </ul>
  158. ", 'seo-ultimate')));
  159. $screen->add_help_tab(array(
  160. 'id' => 'su-meta-descriptions-blog-homepage'
  161. , 'title' => __('Blog Homepage Tab', 'seo-ultimate')
  162. , 'content' => __("
  163. <p>Here&#8217;s information on the various settings:</p>
  164. <ul>
  165. <li><strong>Blog Homepage Meta Description</strong> &mdash; When your blog homepage appears in search results, it&#8217;ll have a title and a description. When you type a description into this box, the Meta Editor will add code to your blog homepage (the <code>&lt;meta name=&quot;description&quot; /&gt;</code> tag) that asks search engines to use what you&#8217;ve entered as the homepage&#8217;s search results description.</li>
  166. <li><strong>Use this blog&#8217;s tagline as the default homepage description.</strong> &mdash; If this box is checked and if the Blog Homepage Meta Description field is empty, Meta Editor will use your blog&#8217;s tagline as the meta description. You can edit the blog&#8217;s tagline under <a href='options-general.php'>Settings &rArr; General</a>.</li>
  167. </ul>
  168. ", 'seo-ultimate')));
  169. $screen->add_help_tab(array(
  170. 'id' => 'su-meta-descriptions-faq'
  171. , 'title' => __('FAQ', 'seo-ultimate')
  172. , 'content' => __("
  173. <ul>
  174. <li><strong>How do I edit the meta description of my homepage?</strong><br />If you have configured your <a href='options-reading.php'>Settings &rArr; Reading</a> section to use a &#8220;front page&#8221; and/or a &#8220;posts page,&#8221; just edit those pages&#8217;s meta descriptions on the &#8220;Pages&#8221; tab. Otherwise, just use the Blog Homepage field.</li>
  175. </ul>
  176. ", 'seo-ultimate')));
  177. $screen->add_help_tab(array(
  178. 'id' => 'su-meta-descriptions-troubleshooting'
  179. , 'title' => __('Troubleshooting', 'seo-ultimate')
  180. , 'content' => __("
  181. <ul>
  182. <li>
  183. <p><strong>What do I do if my site has multiple meta tags?</strong><br />First, try removing your theme&#8217;s built-in meta tags if it has them. Go to <a href='theme-editor.php' target='_blank'>Appearance &rArr; Editor</a> and edit <code>header.php</code>. Delete or comment-out any <code>&lt;meta&gt;</code> tags.</p>
  184. <p>If the problem persists, try disabling other SEO plugins that may be generating meta tags.</p>
  185. <p>Troubleshooting tip: Go to <a href='options-general.php?page=seo-ultimate'>Settings &rArr; SEO Ultimate</a> and enable the &#8220;Identify the plugin&#8217;s HTML code insertions with HTML comment tags&#8221; option. This will mark SEO Ultimate&#8217;s meta tags with comments, allowing you to see which meta tags are generated by SEO Ultimate and which aren&#8217;t.</p>
  186. </li>
  187. </ul>
  188. ", 'seo-ultimate')));
  189. }
  190. }
  191. }
  192. ?>