Guest User

Untitled

a guest
Feb 26th, 2024
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.62 KB | None | 0 0
  1. <?php
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit;
  5. } // Exit if accessed directly
  6.  
  7.  
  8. /*
  9. * Adjustments for the Yoast WordPress SEO Plugin
  10. */
  11.  
  12. if ( ! defined( 'WPSEO_VERSION' ) && ! class_exists( 'wpSEO', false ) ) {
  13. return;
  14. }
  15.  
  16. if ( ! function_exists( 'avia_wpseo_register_assets' ) ) {
  17. function avia_wpseo_register_assets()
  18. {
  19. $screen = get_current_screen();
  20. $vn = avia_get_theme_version();
  21. $min_js = avia_minify_extension( 'js' );
  22.  
  23. if ( is_null( $screen ) || $screen->post_type == '' ) {
  24. return;
  25. }
  26.  
  27. wp_enqueue_script(
  28. 'avia_analytics_js',
  29. AVIA_BASE_URL . "config-templatebuilder/avia-template-builder/assets/js/avia-analytics{$min_js}.js",
  30. [ 'avia_builder_js' ],
  31. $vn,
  32. true
  33. );
  34.  
  35. wp_enqueue_script(
  36. 'avia_yoast_seo_js',
  37. AVIA_BASE_URL . "config-wordpress-seo/wpseo-mod{$min_js}.js",
  38. [ 'avia_analytics_js' ],
  39. $vn,
  40. true
  41. );
  42. }
  43.  
  44. if ( is_admin() ) {
  45. add_action( 'admin_enqueue_scripts', 'avia_wpseo_register_assets' );
  46. }
  47. }
  48.  
  49.  
  50. if ( ! function_exists( 'avia_wpseo_deactivate_avia_set_follow' ) ) {
  51. /**
  52. * There's no need for the default set follow function. Yoast SEO takes care of it
  53. * and user can set custom robot meta values for each post/page.
  54. *
  55. * @param string $meta
  56. * @return string
  57. */
  58. function avia_wpseo_deactivate_avia_set_follow( $meta )
  59. {
  60. return '';
  61. }
  62.  
  63. add_filter( 'avf_set_follow', 'avia_wpseo_deactivate_avia_set_follow', 10, 1 );
  64. }
  65.  
  66. if ( ! function_exists( 'avia_wpseo_change_title_adjustment' ) ) {
  67. /**
  68. * Yoast SEO takes care of the title. It uses the wp_title() hook and the output data is stored in $wptitle.
  69. * So just return $wptitle and leave everything else to Yoast.
  70. *
  71. * This filter has been deprecated with WP 4.1 - function _wp_render_title_tag() is used instead
  72. *
  73. * @param type $title
  74. * @param type $wptitle
  75. * @return type
  76. */
  77. function avia_wpseo_change_title_adjustment( $title, $wptitle )
  78. {
  79. return $wptitle;
  80. }
  81.  
  82. add_filter( 'avf_title_tag', 'avia_wpseo_change_title_adjustment', 10, 2 );
  83. }
  84.  
  85. if ( ! function_exists( 'avia_wpseo_pre_get_document_title_before' ) ) {
  86. /**
  87. * Checks, if we are on an ALB shop page
  88. *
  89. * @since 4.5.5
  90. * @return boolean
  91. */
  92. function avia_wpseo_alb_shop_page()
  93. {
  94. global $post;
  95.  
  96. if ( ! $post instanceof WP_Post || ! class_exists( 'WooCommerce', false ) ) {
  97. return false;
  98. }
  99.  
  100. $shop_page = wc_get_page_id( 'shop' );
  101.  
  102. if ( $post->ID != $shop_page ) {
  103. return false;
  104. }
  105.  
  106. if ( 'active' != Avia_Builder()->get_alb_builder_status( $shop_page ) ) {
  107. return false;
  108. }
  109.  
  110. return true;
  111. }
  112.  
  113. /**
  114. * YOAST takes care of title in normal situations.
  115. * Only when WC is active and we have a ALB shop page the title is not recognised correctly (because this is no archive page)
  116. * In that case we simulate this.
  117. *
  118. * @since 4.5.5
  119. * @param string $title
  120. * @return string
  121. */
  122. function avia_wpseo_pre_get_document_title_before( $title )
  123. {
  124. global $wp_query, $avia_wp_query_archive_state;
  125.  
  126. if ( avia_wpseo_alb_shop_page() ) {
  127. $avia_wp_query_archive_state = $wp_query->is_archive;
  128. $wp_query->is_archive = true;
  129. }
  130.  
  131. return $title;
  132. }
  133.  
  134. /**
  135. * Reset is_archive state
  136. *
  137. * @since 4.5.5
  138. * @param string $title
  139. * @return string
  140. */
  141. function avia_wpseo_pre_get_document_title_after( $title )
  142. {
  143. global $wp_query, $avia_wp_query_archive_state;
  144.  
  145. if ( avia_wpseo_alb_shop_page() ) {
  146. $wp_query->is_archive = $avia_wp_query_archive_state;
  147. }
  148.  
  149. return $title;
  150. }
  151.  
  152. add_filter( 'pre_get_document_title', 'avia_wpseo_pre_get_document_title_before', 1, 1 );
  153. add_filter( 'pre_get_document_title', 'avia_wpseo_pre_get_document_title_after', 99999, 1 );
  154. }
  155.  
  156.  
  157. if ( ! function_exists( 'avia_filter_wpseo_xml_sitemap_transient_caching' ) ) {
  158. /**
  159. * Disable cache in WP_DEBUG mode
  160. *
  161. * @since 5.4
  162. * @param boolean $cache
  163. * @return boolean
  164. */
  165. function avia_filter_wpseo_xml_sitemap_transient_caching( $cache )
  166. {
  167. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
  168. $cache = false;
  169. }
  170.  
  171. return $cache;
  172. }
  173.  
  174. add_filter( 'wpseo_enable_xml_sitemap_transient_caching', 'avia_filter_wpseo_xml_sitemap_transient_caching', 10, 1 );
  175. }
  176.  
  177.  
  178. if ( ! function_exists( 'avia_extract_shortcodes_attachment_ids' ) ) {
  179. /**
  180. * Enable Yoast SEO to index ALB elements that contains images.
  181. * https://github.com/KriesiMedia/wp-themes/issues/1361
  182. *
  183. * @since 5.4 modified
  184. * @param array $elements
  185. * @param string $content
  186. * @return array
  187. */
  188. function avia_extract_shortcodes_attachment_ids( $elements, $content )
  189. {
  190. $container = array();
  191.  
  192. if ( ! empty( $elements ) ) {
  193. foreach ( $elements as $key => $element ) {
  194. $shortcodes = array();
  195. preg_match_all( $element['pattern'], $content, $shortcodes );
  196.  
  197. foreach ( $shortcodes[0] as $shortcode ) {
  198. switch( $element['source'] ) {
  199. case 'ids':
  200. $src = '/ids=\\\'(\d+(,\d+)*)\\\'/';
  201. break;
  202. case 'attachment':
  203. $src = '/attachment=\\\'(\d+)\\\'/';
  204. break;
  205. case 'sid':
  206. $src = '/id=\\\'(\d+)\\\'/sim';
  207. break;
  208. default:
  209. continue 2;
  210. }
  211.  
  212. $id = array();
  213. preg_match_all( $src, $shortcode, $id );
  214.  
  215. foreach ( $id[1] as $key => $value ) {
  216. if ( empty( $value ) ) {
  217. continue;
  218. }
  219.  
  220. $img_ids = explode( ',', $value );
  221.  
  222. $container = array_merge( $container, $img_ids );
  223. }
  224. }
  225. }
  226. }
  227.  
  228. return array_unique( $container, SORT_NUMERIC );
  229. }
  230. }
  231.  
  232.  
  233. if ( ! function_exists( 'avia_filter_wpseo_sitemap_urlimages' ) ) {
  234. /**
  235. *
  236. * @param array $images
  237. * @param int $post_id
  238. * @return array
  239. */
  240. function avia_filter_wpseo_sitemap_urlimages( $images, $post_id )
  241. {
  242. $post = get_post( $post_id );
  243.  
  244. if ( $post instanceof WP_Post ) {
  245. $content = $post->post_content;
  246.  
  247. $shortcodes = array(
  248. 'av_masonry_entries',
  249. 'av_blog',
  250. 'av_postslider',
  251. 'av_magazine',
  252. 'av_portfolio'
  253. );
  254.  
  255. $posts_images = array();
  256.  
  257. foreach ( $shortcodes as $shortcode_name ) {
  258. $shortcode_contents = avf_get_shortcodes_by_name( $shortcode_name, $content );
  259.  
  260. foreach ($shortcode_contents as $shortcode_content) {
  261. if ( $shortcode_content ) {
  262. $thumbnails = avf_process_shortcode_for_thumbnails( $shortcode_name, $shortcode_content );
  263.  
  264. if ( is_array( $thumbnails ) && ! empty( $thumbnails ) ) {
  265. $posts_images = array_merge($posts_images, $thumbnails);
  266. }
  267. }
  268. }
  269. }
  270.  
  271. $elements = apply_filters(
  272. 'avf_add_elements_wpseo_sitemap',
  273. array(
  274. 'image_src' => array( 'pattern' => '/\[av_image [^]]*]/', 'source' => 'src' ),
  275. 'image_attachment' => array( 'pattern' => '/\[av_image [^]]*]/', 'source' => 'attachment' ),
  276. 'masonry' => array( 'pattern' => '/\[av_masonry_gallery [^]]*]/', 'source' => 'ids' ),
  277. 'gallery' => array( 'pattern' => '/\[av_gallery [^]]*]/', 'source' => 'ids' ),
  278. 'horizontal' => array( 'pattern' => '/\[av_horizontal_gallery [^]]*]/', 'source' => 'ids' ),
  279. 'accordion' => array( 'pattern' => '/\[av_slideshow_accordion(.+?)?\](?:(.+?)?\[\/av_slideshow_accordion\])?/sim', 'source' => 'sid' ),
  280. 'slideshow' => array( 'pattern' => '/\[av_slideshow(.+?)?\](?:(.+?)?\[\/av_slideshow\])?/sim', 'source' => 'sid' ),
  281. 'slideshow_full' => array( 'pattern' => '/\[av_slideshow_full(.+?)?\](?:(.+?)?\[\/av_slideshow_full\])?/sim', 'source' => 'sid' ),
  282. 'slideshow_fullscreen' => array( 'pattern' => '/\[av_fullscreen(.+?)?\](?:(.+?)?\[\/av_fullscreen\])?/sim', 'source' => 'sid' ),
  283. 'color_section' => array( 'pattern' => '/\[av_section(.+?)?\](?:(.+?)?\[\/av_section\])?/sim', 'source' => 'attachment' )
  284. ),
  285. $post_id
  286. );
  287.  
  288. $ids = avia_extract_shortcodes_attachment_ids( $elements, $content );
  289.  
  290. foreach ( $ids as $id ) {
  291. $title = get_the_title( $id );
  292. $alt = get_post_meta( $id, '_wp_attachment_image_alt', true );
  293. $src = wp_get_attachment_url( $id );
  294.  
  295. $images[] = array(
  296. 'src' => $src,
  297. 'title' => $title,
  298. 'alt' => $alt
  299. );
  300. }
  301. }
  302.  
  303. return array_merge($images, $posts_images);
  304. }
  305.  
  306. add_action( 'wpseo_sitemap_urlimages', 'avia_filter_wpseo_sitemap_urlimages', 10, 2);
  307. }
  308.  
  309.  
  310. function avf_get_shortcodes_by_name( $shortcode_name, $content )
  311. {
  312. preg_match_all( "/\[$shortcode_name(.*?)\]/", $content, $matches );
  313.  
  314. if ( isset( $matches[0] ) ) {
  315. return $matches[0];
  316. }
  317.  
  318. return false;
  319. }
  320.  
  321. function avf_process_shortcode_for_thumbnails( $shortcode_name, $shortcode_content )
  322. {
  323. $thumbnails = array();
  324. $blog_type = '';
  325. $shortcode_content = stripslashes($shortcode_content);
  326. $term_ids = '';
  327. $count = 10;
  328.  
  329. preg_match( '/items=["\'](\d+)["\']/', $shortcode_content, $blog_count_matches );
  330. preg_match( '/blog_type=["\']([^"\']+)["\']/', $shortcode_content, $blog_type_matches );
  331.  
  332. if ( isset( $blog_count_matches[1] ) ) {
  333. $count = $blog_count_matches[1];
  334. }
  335.  
  336. if ( isset( $blog_type_matches[1] ) ) {
  337. $blog_type = $blog_type_matches[1];
  338. }
  339.  
  340. if ( $blog_type === 'posts' || $shortcode_name == 'av_portfolio' ) {
  341. preg_match( '/categories=\'([\d,]+)\'/', $shortcode_content, $term_matches );
  342. $taxonomy = $shortcode_name == 'av_portfolio' ? 'portfolio_entries' : 'category';
  343. }
  344.  
  345. if ( $blog_type === 'taxonomy' || ( empty($blog_type) && $shortcode_name == 'av_magazine' ) ) {
  346. preg_match( '/link=[\'"]([^\'"]+)[\'"]/', $shortcode_content, $taxonomy_matches );
  347. $taxonomy_parts = explode( ',', $taxonomy_matches[1] );
  348. $taxonomy = isset( $taxonomy_parts[0] ) ? $taxonomy_parts[0] : '';
  349. preg_match( '/(\d+(,\d+)*)/', $taxonomy_matches[1], $term_matches );
  350. }
  351.  
  352. $query_args = array(
  353. 'post_type' => array('portfolio', 'post', 'product'),
  354. 'posts_per_page' => $count
  355. );
  356.  
  357. if ( isset( $term_matches[1] ) && $taxonomy ) {
  358. $term_ids = explode( ',', $term_matches[1] );
  359. }
  360.  
  361. $tax_query = array(
  362. 'taxonomy' => $taxonomy,
  363. 'field' => 'term_id'
  364. );
  365.  
  366. if ( ! empty( $term_ids ) ) {
  367. $tax_query['terms'] = $term_ids;
  368. } else {
  369. $terms = get_terms( array(
  370. 'taxonomy' => $taxonomy,
  371. 'fields' => 'ids', // Retrieve only term IDs
  372. ) );
  373.  
  374. if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
  375. $tax_query['terms'] = $terms;
  376. }
  377. }
  378.  
  379. $query_args['tax_query'] = array( $tax_query );
  380.  
  381. $query = new WP_Query( $query_args );
  382.  
  383. if ( $query->have_posts() ) {
  384. while ( $query->have_posts() ) {
  385. $query->the_post();
  386. $url = get_the_post_thumbnail_url( get_the_ID() );
  387. $title = get_the_title( get_the_ID());
  388. $alt = get_post_meta( get_the_ID(), '_wp_attachment_image_alt', true );
  389.  
  390. $thumbnail = array(
  391. 'src' => $url,
  392. 'title' => $title,
  393. 'alt' => $alt
  394. );
  395.  
  396. if ( $thumbnail ) {
  397. $thumbnails[] = $thumbnail;
  398. }
  399. }
  400. wp_reset_postdata();
  401. }
  402.  
  403. return $thumbnails;
  404. }
  405.  
  406. if ( ! function_exists( 'avia_wpseo_sitemap_exclude_pages' ) ) {
  407. add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', 'avia_wpseo_sitemap_exclude_pages', 10, 1 );
  408.  
  409. /**
  410. * Callback to theme to get a list of all pages that should be excluded from sitemap
  411. *
  412. * @link https://kriesi.at/support/topic/yoast-seo-fatal-error-on-post-sitemap-xml-or-page-sitemap-xml/
  413. * Filter was called with null -> removed type hint array for parameter $post_ids
  414. *
  415. * @since 4.5.1
  416. * @since 5.6.7 removed type hint array for parameter $post_ids
  417. * @param array|null $post_ids
  418. * @return array
  419. */
  420. function avia_wpseo_sitemap_exclude_pages( $post_ids = array() )
  421. {
  422. if ( ! is_array( $post_ids ) ) {
  423. if ( is_numeric( $post_ids ) ) {
  424. $post_ids = array( $post_ids );
  425. } else {
  426. $post_ids = array();
  427. }
  428. }
  429.  
  430. /**
  431. *
  432. * @used_by Avia_Custom_Pages 10
  433. * @used_by enfold\config-wpml\config.php 20
  434. * @since 4.5.1
  435. * @param array $post_ids
  436. * @param string $context
  437. * @return array
  438. */
  439. $post_ids = apply_filters( 'avf_get_special_pages_ids', $post_ids, 'sitemap' );
  440.  
  441. $post_ids = array_unique( $post_ids, SORT_NUMERIC );
  442.  
  443. return $post_ids;
  444. }
  445. }
  446.  
  447. if ( ! function_exists( 'avia_wpseo_process_shortcode_in_backend' ) ) {
  448. /**
  449. * This is a beta trial only.
  450. * Process shortcode in backend if not called with ajax
  451. * Ajax call wpseo_filter_shortcodes has only opening tags of shortcodes. Processing makes no sense.
  452. *
  453. * @since 4.5.7.1
  454. * @param string $process
  455. * @param aviaShortcodeTemplate $class
  456. * @param array $atts
  457. * @param string $content
  458. * @param string $shortcodename
  459. * @param boolean $fake
  460. * @return string '' | 'process_shortcode_in_backend'
  461. */
  462. function avia_wpseo_process_shortcode_in_backend( $process, $class, $atts, $content, $shortcodename, $fake )
  463. {
  464. if ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_REQUEST['action'] ) && ( 'wpseo_filter_shortcodes' == $_REQUEST['action'] ) ) {
  465. // return ''; // as a try we evaluate shortcodes even if we have no content
  466. return 'process_shortcode_in_backend';
  467. }
  468.  
  469. /**
  470. * Currently we do not alter this
  471. */
  472. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
  473. return $process;
  474. }
  475.  
  476. return 'process_shortcode_in_backend';
  477. }
  478.  
  479. add_filter( 'avf_process_shortcode_in_backend', 'avia_wpseo_process_shortcode_in_backend', 20, 6 );
  480. }
  481.  
Advertisement
Add Comment
Please, Sign In to add comment