Guest User

Untitled

a guest
Mar 26th, 2022
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. if( !function_exists('salient_wpbakery_general_sitemap_images') ) {
  2. function salient_wpbakery_general_sitemap_images( $images, $id ) {
  3.  
  4. $post = get_post( $id );
  5.  
  6. if ( $post && strpos( $post->post_content, '[vc_row' ) !== false ) {
  7. preg_match_all( '/(?:image_url|image_1_url|image_2_url)\=\"([^\"]+)\"/', $post->post_content, $matches );
  8. foreach ( $matches[1] as $m ) {
  9. $ids = explode( ',', $m );
  10. foreach ( $ids as $id ) {
  11. if ( (int) $id ) {
  12. $images[] = array(
  13. 'src' => wp_get_attachment_url( $id ),
  14. 'title' => get_the_title( $id ),
  15. );
  16. }
  17. }
  18. }
  19. }
  20.  
  21.  
  22. return $images;
  23. }
  24. }
  25.  
  26. if( !function_exists('salient_wpbakery_query_sitemap_images') ) {
  27. function salient_wpbakery_query_sitemap_images( $images, $id ) {
  28.  
  29. $post = get_post( $id );
  30.  
  31. if( !$post ) {
  32. return $images;
  33. }
  34.  
  35. $elements_arr = array('nectar_blog','recent_posts','nectar_post_grid','nectar_portfolio');
  36.  
  37. foreach( $elements_arr as $element ) {
  38.  
  39. if ( preg_match_all( '/\['.$element.'(\s.*?)?\]/s', $post->post_content, $matches, PREG_SET_ORDER ) ) {
  40.  
  41. if (!empty($matches)) {
  42.  
  43. foreach ($matches as $shortcode) {
  44.  
  45. if( $shortcode && isset($shortcode[1]) ) {
  46.  
  47. $atts = shortcode_parse_atts($shortcode[1]);
  48.  
  49. if( !empty($atts) ) {
  50.  
  51. $posts_per_page = isset($atts['posts_per_page']) ? $atts['posts_per_page'] : '-1';
  52. $post_offset = isset($atts['offset']) ? $atts['offset'] : 0;
  53. $category = isset($atts['category']) ? $atts['category'] : null;
  54. $post_type = 'post';
  55.  
  56. if( $element === 'nectar_post_grid' ) {
  57. $post_offset = isset($atts['post_offset']) ? $atts['post_offset'] : 0;
  58. }
  59.  
  60. $nectar_blog_arr = array(
  61. 'post_type' => $post_type,
  62. 'posts_per_page' => $posts_per_page,
  63. 'offset' => $post_offset
  64. );
  65.  
  66.  
  67. if( $element === 'nectar_post_grid' ) {
  68.  
  69. $post_grid_post_type = isset($atts['post_type'] ) ? $atts['post_type'] : 'post';
  70.  
  71. if( $post_grid_post_type === 'post' ) {
  72. $category = (isset($atts['blog_category']) && 'all' !== $atts['blog_category']) ? $atts['blog_category'] : null;
  73. $nectar_blog_arr['category_name'] = $category;
  74. }
  75. else if( $post_grid_post_type === 'portfolio' ) {
  76. $category = (isset($atts['portfolio_category']) && 'all' !== $atts['portfolio_category']) ? $atts['portfolio_category'] : null;
  77. $nectar_blog_arr['project-type'] = $category;
  78. $nectar_blog_arr['post_type'] = 'portfolio';
  79. }
  80. else {
  81. continue;
  82. }
  83. }
  84.  
  85. else if( $element === 'nectar_portfolio' ) {
  86. $project_offset = isset($atts['project_offset']) ? $atts['project_offset'] : 0;
  87. $projects_per_page = isset($atts['projects_per_page']) ? $atts['projects_per_page'] : -1;
  88.  
  89. $nectar_blog_arr['post_type'] = 'portfolio';
  90. $nectar_blog_arr['project-type'] = $category;
  91. $nectar_blog_arr['offset'] = $project_offset;
  92. $nectar_blog_arr['posts_per_page'] = $projects_per_page;
  93. }
  94.  
  95. else {
  96. if( 'all' === $category ) {
  97. $category = null;
  98. }
  99. $nectar_blog_arr['category_name'] = $category;
  100. }
  101.  
  102. $nectar_blog_el_query = new WP_Query( $nectar_blog_arr );
  103.  
  104. if( $nectar_blog_el_query->have_posts() ) : while( $nectar_blog_el_query->have_posts() ) : $nectar_blog_el_query->the_post();
  105.  
  106. // Gather alt and image.
  107. $alt_text = get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true );
  108. if( !$alt_text ) {
  109. $alt_text = get_the_title();
  110. }
  111. $featured_image = get_the_post_thumbnail_url();
  112.  
  113. // Items to filter out.
  114. $allowed_image = true;
  115.  
  116. //// Rwcent Posts.
  117. if( false !== strpos($shortcode[0],'[recent_posts ') ) {
  118. $style = isset($atts['style']) ? $atts['style'] : 'default';
  119.  
  120. $blacklisted_styles = array(
  121. 'minimal',
  122. 'title_only',
  123. 'slider',
  124. 'slider_multiple_visible'
  125. );
  126. if( in_array($style, $blacklisted_styles) ) {
  127. $allowed_image = false;
  128. }
  129. }
  130.  
  131. //// Portfolio.
  132. if( false !== strpos($shortcode[0],'[nectar_post_grid ') ||
  133. false !== strpos($shortcode[0],'[nectar_portfolio ')) {
  134. $custom_thumbnail = get_post_meta(get_the_ID(), '_nectar_portfolio_custom_thumbnail', true);
  135.  
  136. if( !empty($custom_thumbnail) ) {
  137. $featured_image = $custom_thumbnail;
  138. }
  139. }
  140.  
  141. // Add image to array.
  142. if( $featured_image && $allowed_image ) {
  143. $images[] = array(
  144. 'src' => $featured_image,
  145. 'title' => $alt_text
  146. );
  147. }
  148.  
  149. endwhile; endif;
  150. wp_reset_postdata();
  151.  
  152. } // end found $atts
  153. } // end $shortcode
  154. } // end foreach $matches
  155. } // end found $matches
  156. } // end preg_match_all
  157. } // end foreach $elements_arr
  158.  
  159. return $images;
  160. }
  161. }
  162.  
  163.  
  164. add_filter( 'wpseo_sitemap_urlimages', 'salient_wpbakery_general_sitemap_images', 10, 2 );
  165. add_filter( 'wpseo_sitemap_urlimages', 'salient_wpbakery_query_sitemap_images', 10, 2 );
Advertisement
Add Comment
Please, Sign In to add comment