/wp-content/plugins/all-in-one-seo-pack/app/Common/ImportExport/YoastSeo/Helpers.php

https://gitlab.com/ebrjose/comcebu · PHP · 143 lines · 91 code · 16 blank · 36 comment · 11 complexity · a32e598e723cd823f5c464f62b65ad8a MD5 · raw file

  1. <?php
  2. namespace AIOSEO\Plugin\Common\ImportExport\YoastSeo;
  3. // Exit if accessed directly.
  4. if ( ! defined( 'ABSPATH' ) ) {
  5. exit;
  6. }
  7. use AIOSEO\Plugin\Common\ImportExport;
  8. // phpcs:disable WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
  9. /**
  10. * Contains helper methods for the import from Rank Math.
  11. *
  12. * @since 4.0.0
  13. */
  14. class Helpers extends ImportExport\Helpers {
  15. /**
  16. * Converts the macros from Yoast SEO to our own smart tags.
  17. *
  18. * @since 4.0.0
  19. *
  20. * @param string $string The string with macros.
  21. * @param string $postType The post type.
  22. * @param string $pageType The page type.
  23. * @return string $string The string with smart tags.
  24. */
  25. public function macrosToSmartTags( $string, $postType = null, $pageType = null ) {
  26. $macros = $this->getMacros( $postType, $pageType );
  27. if ( preg_match( '#%%BLOGDESCLINK%%#', $string ) ) {
  28. $blogDescriptionLink = '<a href="' .
  29. aioseo()->helpers->decodeHtmlEntities( get_bloginfo( 'url' ) ) . '">' .
  30. aioseo()->helpers->decodeHtmlEntities( get_bloginfo( 'name' ) ) . ' - ' .
  31. aioseo()->helpers->decodeHtmlEntities( get_bloginfo( 'description' ) ) . '</a>';
  32. $string = str_replace( '%%BLOGDESCLINK%%', $blogDescriptionLink, $string );
  33. }
  34. if ( preg_match_all( '#%%cf_([^%]*)%%#', $string, $matches ) && ! empty( $matches[1] ) ) {
  35. foreach ( $matches[1] as $name ) {
  36. if ( ! preg_match( '#\s#', $name ) ) {
  37. $string = aioseo()->helpers->pregReplace( "#%%cf_$name%%#", "#custom_field-$name", $string );
  38. }
  39. }
  40. }
  41. if ( preg_match_all( '#%%tax_([^%]*)%%#', $string, $matches ) && ! empty( $matches[1] ) ) {
  42. foreach ( $matches[1] as $name ) {
  43. if ( ! preg_match( '#\s#', $name ) ) {
  44. $string = aioseo()->helpers->pregReplace( "#%%tax_$name%%#", "#tax_name-$name", $string );
  45. }
  46. }
  47. }
  48. foreach ( $macros as $macro => $tag ) {
  49. $string = aioseo()->helpers->pregReplace( "#$macro(?![a-zA-Z0-9_])#im", $tag, $string );
  50. }
  51. // Strip out all remaining tags.
  52. $string = aioseo()->helpers->pregReplace( '/%[^\%\s]*\([^\%]*\)%/i', '', aioseo()->helpers->pregReplace( '/%[^\%\s]*%/i', '', $string ) );
  53. return trim( $string );
  54. }
  55. /**
  56. * Returns the macro mappings.
  57. *
  58. * @since 4.1.1
  59. *
  60. * @param string $postType The post type.
  61. * @param string $pageType The page type.
  62. * @return array $macros The macros.
  63. */
  64. protected function getMacros( $postType = null, $pageType = null ) {
  65. $macros = [
  66. '%%sitename%%' => '#site_title',
  67. '%%sitedesc%%' => '#tagline',
  68. '%%sep%%' => '#separator_sa',
  69. '%%term_title%%' => '#taxonomy_title',
  70. '%%term_description%%' => '#taxonomy_description',
  71. '%%category_description%%' => '#taxonomy_description',
  72. '%%tag_description%%' => '#taxonomy_description',
  73. '%%primary_category%%' => '#taxonomy_title',
  74. '%%archive_title%%' => '#archive_title',
  75. '%%pagenumber%%' => '#page_number',
  76. '%%caption%%' => '#attachment_caption',
  77. '%%name%%' => '#author_first_name #author_last_name',
  78. '%%user_description%%' => '#author_bio',
  79. '%%date%%' => '#archive_date',
  80. '%%currentday%%' => '#current_day',
  81. '%%currentmonth%%' => '#current_month',
  82. '%%currentyear%%' => '#current_year',
  83. '%%searchphrase%%' => '#search_term',
  84. '%%AUTHORLINK%%' => '#author_link',
  85. '%%POSTLINK%%' => '#post_link',
  86. '%%BLOGLINK%%' => '#site_link',
  87. '%%category%%' => '#categories',
  88. '%%parent_title%%' => '#parent_title',
  89. '%%wc_sku%%' => '#woocommerce_sku',
  90. '%%wc_price%%' => '#woocommerce_price',
  91. '%%wc_brand%%' => '#woocommerce_brand',
  92. '%%excerpt%%' => '#post_excerpt',
  93. '%%excerpt_only%%' => '#post_excerpt_only'
  94. /* '%%tag%%' => '',
  95. '%%id%%' => '',
  96. '%%page%%' => '',
  97. '%%modified%%' => '',
  98. '%%pagetotal%%' => '',
  99. '%%focuskw%%' => '',
  100. '%%term404%%' => '',
  101. '%%ct_desc_[^%]*%%' => '' */
  102. ];
  103. if ( $postType ) {
  104. $postType = get_post_type_object( $postType );
  105. if ( ! empty( $postType ) ) {
  106. $macros += [
  107. '%%pt_single%%' => $postType->labels->singular_name,
  108. '%%pt_plural%%' => $postType->labels->name,
  109. ];
  110. }
  111. }
  112. switch ( $pageType ) {
  113. case 'archive':
  114. $macros['%%title%%'] = '#archive_title';
  115. break;
  116. case 'term':
  117. $macros['%%title%%'] = '#taxonomy_title';
  118. break;
  119. default:
  120. $macros['%%title%%'] = '#post_title';
  121. break;
  122. }
  123. // Strip all other tags.
  124. $macros['%%[^%]*%%'] = '';
  125. return $macros;
  126. }
  127. }