Untitled

mail@pastecode.io avatar
unknown
php
3 years ago
7.7 kB
7
Indexable
Never
function acf_links_content($content){

    $max_related      = 6; //Quantidade de posts relacionados
    $aCadaXparagrafos = 10; //Intervalo de parágrafos (a cada X)
    $aCadaXH          = 10; //Intervalo de H2 ou H3 (a cada X)

    if(has_tag('norelated')){
      return $content;
    }else{

      global $post;

      $id = $post->ID;

        # Get ACF array
      $posts = get_field('relacionadas');

      if($posts == ""){

        if(has_tag()){

          $tags             = get_the_tags();
          $num_tags         = count($tags);
          $tag              = $tags[0]->slug;
          $the_query        = new WP_Query(
            array(
              'tag'          => $tag,
              'post__not_in' => array($id),
            )
          );
          $posts            = $the_query->posts;
          $limit            = count($posts);
          $marcaParagrafo   = "</p>";
          $content_orig     = $content;
          $content          = explode($marcaParagrafo, $content);

          if(count($content) < ($limit * $aCadaXparagrafos)){
            $marcaParagrafo = "</h2>";
            $content = explode($marcaParagrafo, $content_orig);
            $aCadaXparagrafosOrig = $aCadaXparagrafos;
            $aCadaXparagrafos = $aCadaXH;

            if(count($content) < $max_related){

              $marcaParagrafo = "</h3>";
              $content = explode($marcaParagrafo, $content_orig);
              $numTags = count($content);

              if($numTags < 2 || $numTags < $max_related){
                $marcaParagrafo = "</p>";
                $content = explode($marcaParagrafo, $content_orig);
                $aCadaXparagrafos = floor(($aCadaXparagrafosOrig-1)/2);
              }
            }
          }

          $new_content = '';
          $numParagrafos = count($content);
          $acf_i = 0;
          $c = 0;

          for ($i = 0; $i < $numParagrafos; $i++) {

            if ($i % $aCadaXparagrafos == 0 && $i != 0) {

              $c++;
              ob_start();
              if($c < $limit or $c == $limit) {
                if($id == $posts[$acf_i]->ID) { } else {
                  setup_postdata($posts[$acf_i]);
                  $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($posts[$acf_i]->ID), '218thumb' );
                  $url = $thumb['0']; ?>

                  <?php if(is_single()) { ?>
                    <div class="relacionado">
                      <a href="<?php the_permalink($posts[$acf_i]->ID); ?><?php if (is_amp_endpoint()) { echo "amp/"; } ?>" class="related-middle-content"><img src="<?php echo $url; ?>" class="img-responsive<?php if (is_amp_endpoint()) { echo " relacionado-amp"; } ?>"></a>
                      <?php if (is_amp_endpoint()) { } else { ?><span>Veja também</span><?php } ?>
                      <span><a href="<?php the_permalink($posts[$acf_i]->ID); ?><?php if (is_amp_endpoint()) { echo "amp/"; } ?>" class="related-middle-content">
                        <?php $title = get_the_title($posts[$acf_i]->ID); echo $title; ?></a></span>
                      </div>
                    <?php } else { ?>
                      <p><span class="leia">Leia também:</span><a href="<?php the_permalink($posts[$acf_i]->ID); ?><?php if (is_amp_endpoint()) { echo "amp/"; } ?>">
                        <?php $title = get_the_title($posts[$acf_i]->ID); echo $title; ?> <?php echo $limite; ?></a> <?php echo $id; ?></p>
                      <?php }
                    }
                    $acf_i++;
                  }
                  $new_content.= ob_get_contents();
                  ob_end_clean();
                }
                $new_content .= $content[$i].$marcaParagrafo;
              }
              wp_reset_postdata();
              return $new_content;
            }else{
              return $content;
            }
          }else{

            # Validating array to avoid warnings (Luiz)
            if (is_array($posts)){
              $limit = count($posts);
            }else{
              $limit = 0;
            }

            # $limit = count($posts);
            $marcaParagrafo = "</p>";
            #$numParagrafos = count($content);
            $numParagrafos = is_array($content) ? count($content) : 1;
            $new_content = '';
            $acf_i = 0;
            $c = 0;

            $content_orig     = $content;
            $content          = explode($marcaParagrafo, $content);

            if(count($content) < ($limit * $aCadaXparagrafos)){
              $marcaParagrafo = "</h2>";
              $content = explode($marcaParagrafo, $content_orig);
              $aCadaXparagrafosOrig = $aCadaXparagrafos;
              $aCadaXparagrafos = $aCadaXH;

              if(count($content) < $max_related){

                $marcaParagrafo = "</h3>";
                $content = explode($marcaParagrafo, $content_orig);
                $numTags = count($content);

                if($numTags < 2 || $numTags < $max_related){
                  $marcaParagrafo = "</p>";
                  $content = explode($marcaParagrafo, $content_orig);
                  $aCadaXparagrafos = floor(($aCadaXparagrafosOrig-1)/2);
                }
              }
            }

            $new_content = '';
            $numParagrafos = count($content);

            for ($i = 0; $i < $numParagrafos; $i++) {
              if ($i % $aCadaXparagrafos == 0 && $i != 0) {
                $c++;
                ob_start();
                if($c < $limit or $c == $limit) {
                  if($id == $posts[$acf_i]->ID) { } else {
                    setup_postdata($posts[$acf_i]);
                    $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($posts[$acf_i]->ID), '218thumb' );
                    $url = $thumb['0']; ?>
                    <?php if(is_single()) { ?>
                      <div class="relacionado">
                        <a href="<?php the_permalink($posts[$acf_i]->ID); ?><?php if (is_amp_endpoint()) { echo "amp/"; } ?>" class="related-middle-content"><img src="<?php echo $url; ?>" class="img-responsive<?php if (is_amp_endpoint()) { echo " relacionado-amp"; } ?>"></a>
                        <?php if (is_amp_endpoint()) { } else { ?><span>Veja também</span><?php } ?>
                        <span><a href="<?php the_permalink($posts[$acf_i]->ID); ?><?php if (is_amp_endpoint()) { echo "amp/"; } ?>" class="related-middle-content">
                          <?php $title = get_the_title($posts[$acf_i]->ID); echo $title; ?></a></span>
                        </div>
                      <?php } else { ?>
                        <p><span class="leia">Leia também:</span><a href="<?php the_permalink($posts[$acf_i]->ID); ?><?php if (is_amp_endpoint()) { echo "amp/"; } ?>">
                          <?php $title = get_the_title($posts[$acf_i]->ID); echo $title; ?> <?php echo $limite; ?></a> <?php echo $id; ?></p>
                        <?php } ?>
                        <?php
                      }
                      $acf_i++;
                    }
                    $new_content.= ob_get_contents();
                    ob_end_clean();
                  }
                  $new_content .= $content[$i].$marcaParagrafo;
                }
                return $new_content;
              }
            }
          }
          add_filter('the_content', 'acf_links_content', 10);

          function acf_filter_blockquote($content){

            $re    = '/(<blockquote\s*.*?>.*?)(<div class="[^"]*?relacionado[^"]*?">.*?<\/div>)(.*?<\/blockquote>)/ims';
            $subst = '\\1\\3\\2';

            return preg_replace($re, $subst, $content);
          }
          //add_filter('the_content', 'acf_filter_blockquote', 20);