Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

jsarnowski / jsarnowski/jet-elements   php

Repository URL to install this package:

/ class-jet-elements-post-tools.php

<?php
/**
 * Cherry addons tools class
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

if ( ! class_exists( 'Jet_Elementst_Post_Tools' ) ) {

	/**
	 * Define Jet_Elements_Post_Tools class
	 */
	class Jet_Elements_Post_Tools {

		/**
		 * A reference to an instance of this class.
		 *
		 * @since 1.0.0
		 * @var   object
		 */
		private static $instance = null;

		/**
		 * Get post title.
		 *
		 * @since  1.0.0
		 * @param array  $args array of arguments.
		 * @param [type] $type - post, term.
		 * @param int    $id ID of post.
		 * @return string
		 */
		public function get_post_title( $args = array(), $type = 'post', $id = 0 ) {
			$object = call_user_func( array( $this, 'get_' . $type . '_object' ), $id );

			if ( 'post' === $type && empty( $object->ID ) || 'term' === $type && empty( $object->term_id ) ) {
				return '';
			}

			$default_args = array(
				'visible'      => true,
				'length'       => -1,
				'trimmed_type' => 'word',
				'ending'       => '&hellip;',
				'html'         => '<h3 %1$s><a href="%2$s" %3$s rel="bookmark">%4$s</a></h3>',
				'class'        => '',
				'title'        => '',
				'echo'         => false,
			);
			$args = wp_parse_args( $args, $default_args );
			$html = '' ;

			if ( filter_var( $args['visible'], FILTER_VALIDATE_BOOLEAN ) && 0 !== $args['length'] ) {
				$title     = ( 'post' === $type ) ? $object->post_title : $object->name;
				$title_cut = $title;

				$title     = ( $args['title'] ) ? 'title="' . $args['title'] . '"' : 'title="' . $title . '"';
				$title_cut = $this->cut_text( $title_cut, $args['length'], $args['trimmed_type'], $args['ending'] );

				$link       = ( 'post' === $type ) ? $this->get_post_permalink() : $this->get_term_permalink( $object->term_id );
				$html_class = ( $args['class'] ) ? 'class="' . $args['class'] . '"' : '' ;

				$html = sprintf( $args['html'], $html_class, $link, $title, $title_cut );
			}

			return $this->output_method( $html, $args['echo'] );
		}

		/**
		 * Get post excerpt
		 *
		 * @since  1.0.0
		 * @param array  $args array of arguments.
		 * @param [type] $type - post, term.
		 * @param int    $id ID of post.
		 * @return string
		 */
		public function get_post_content( $args = array(), $type = 'post', $id = 0 ) {
			$object = call_user_func( array( $this, 'get_' . $type . '_object' ), $id );

			if ( 'post' === $type && empty( $object->ID ) || 'term' === $type && empty( $object->term_id ) ) {
				return '';
			}

			$default_args = array(
				'visible'      => true,
				'content_type' => 'post_content',
				'length'       => -1,
				'trimmed_type' => 'word',
				'ending'       => '&hellip;',
				'html'         => '<p %1$s>%2$s</p>',
				'class'        => '',
				'echo'         => false,
			);
			$args = wp_parse_args( $args, $default_args );
			$html = '' ;
			$content_type = $args['content_type'];

			if ( filter_var( $args['visible'], FILTER_VALIDATE_BOOLEAN ) ) {
				$is_content = false;

				if ( 'term' === $type ) {
					$text = $object->description;
				} elseif ( 'post_content' === $content_type || 'post_excerpt' === $content_type && empty( $object->$content_type ) ) {
					$is_content = true;
					$text = get_the_content();
				} else {
					$text = get_the_excerpt();
				}

				$text = $this->cut_text( $text, $args['length'], $args['trimmed_type'], $args['ending'], $is_content );

				if ( $text ) {
					$html_class = ( $args['class'] ) ? 'class="' . $args['class'] . '"' : '' ;

					$html = sprintf( $args['html'], $html_class, $text );
				}
			}

			if ( 'post_content' === $content_type && -1 === $args['length'] ) {
				$html = apply_filters( 'the_content', $html );
			}

			return $this->output_method( $html, $args['echo'] );
		}

		/**
		 * Get post author
		 *
		 * @since  1.0.0
		 * @return string
		 */
		public function get_post_author( $args = array(), $id = 0 ) {
			$object = $this->get_post_object( $id );

			if ( empty( $object->ID ) ) {
				return false;
			}

			$default_args = array(
				'visible'	=> 'true',
				'icon'		=> '',
				'prefix'	=> '',
				'html'		=> '%1$s<a href="%2$s" %3$s %4$s rel="author">%5$s%6$s</a>',
				'title'		=> '',
				'class'		=> 'post-author',
				'echo'		=> false,
			);
			$args = wp_parse_args( $args, $default_args );
			$html = '' ;

			if ( filter_var( $args['visible'], FILTER_VALIDATE_BOOLEAN ) ) {
				$html_class = ( $args['class'] ) ? 'class="' . $args['class'] . '"' : '';
				$title      = ( $args['title'] ) ? 'title="' . $args['title'] . '"' : '';
				$author     = get_the_author();
				$link       = get_author_posts_url( $object->post_author );

				$html = sprintf( $args['html'], $args['prefix'], $link, $title, $html_class, $args['icon'], $author );
			}

			return $this->output_method( $html, $args['echo'] );
		}

		/**
		 * Get post date.
		 *
		 * @since  1.0.0
		 * @return string
		 */
		public function get_post_date( $args = array(), $id = 0 ) {
			$object = $this->get_post_object( $id );

			if ( empty( $object->ID ) ) {
				return false;
			}

			$default_args = array(
				'visible'		=> true,
				'icon'			=> '',
				'prefix'		=> '',
				'html'			=> '%1$s<a href="%2$s" %3$s %4$s ><time datetime="%5$s" title="%5$s">%6$s%7$s</time></a>',
				'title'			=> '',
				'class'			=> 'post-date',
				'date_format'	=> '',
				'human_time'	=> false,
				'echo'			=> false,
			);
			$args = wp_parse_args( $args, $default_args );
			$html = '' ;

			if ( filter_var( $args['visible'], FILTER_VALIDATE_BOOLEAN ) ) {
				$html_class			= ( $args['class'] ) ? 'class="' . esc_attr( $args['class'] ) . '"' : '' ;
				$title				= ( $args['title'] ) ? 'title="' . esc_attr( $args['title'] ) . '"' : '' ;
				$date_post_format	= ( $args['date_format'] ) ? esc_attr( $args['date_format'] ) : get_option( 'date_format' );
				$date				= ( $args['human_time'] ) ? human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) : get_the_date( $date_post_format );
				$time				= get_the_time( 'Y-m-d\TH:i:sP' );

				preg_match_all( '/(\d+)/mi', $time, $date_array );
				$link = get_day_link( (int) $date_array[0][0], (int) $date_array[0][1], (int) $date_array[0][2] );

				$html = sprintf( $args['html'], $args['prefix'], $link, $title, $html_class, $time, $args['icon'], $date );
			}

			return $this->output_method( $html, $args['echo'] );
		}

		/**
		 * Get comment count
		 *
		 * @since  1.0.0
		 * @return string
		 */
		public function get_post_comment_count( $args = array(), $id = 0 ) {
			$object = $this->get_post_object( $id );

			if ( empty( $object->ID ) ) {
				return false;
			}

			$default_args = array(
				'visible'		=> true,
				'icon'			=> '',
				'prefix'		=> '',
				'suffix'		=> '%s',
				'html'			=> '%1$s<a href="%2$s" %3$s %4$s>%5$s%6$s</a>',
				'title'			=> '',
				'class'			=> 'post-comments-count',
				'echo'			=> false,
			);

			$args = wp_parse_args( $args, $default_args );

			$args['suffix'] = ( isset( $args['sufix'] ) ) ? $args['sufix'] : $args['suffix'];

			$html  = '';
			$count = '';

			if ( filter_var( $args['visible'], FILTER_VALIDATE_BOOLEAN ) ) {
				$post_type = get_post_type( $object->ID );
				if ( post_type_supports( $post_type, 'comments' ) ) {
					$suffix = is_string( $args['suffix'] ) ? $args['suffix'] : translate_nooped_plural( $args['suffix'], $object->comment_count, $args['suffix']['domain'] );
					$count = sprintf( $suffix, $object->comment_count );
				}

				$html_class = ( $args['class'] ) ? 'class="' . $args['class'] . '"' : '';
				$title = ( $args['title'] ) ? 'title="' . $args['title'] . '"' : '';
				$link = get_comments_link();

				$html = sprintf( $args['html'], $args['prefix'], $link, $title, $html_class, $args['icon'], $count );
			}

			return $this->output_method( $html, $args['echo'] );
		}

		/**
		 * Get post more button
		 *
		 * @since  1.0.0
		 * @param array  $args array of arguments.
		 * @param [type] $type - post, term.
		 * @param int    $id ID of post.
		 * @return string
		 */
		public function get_post_button( $args = array(), $type = 'post', $id = 0 ) {
			$object = call_user_func( array( $this, 'get_' . $type . '_object' ), $id );

			if ( 'post' === $type && empty( $object->ID ) || 'term' === $type && empty( $object->term_id ) ) {
				return false;
			}

			$default_args = array(
				'visible' => true,
				'text'    => '',
				'icon'    => '',
				'html'    => '<a href="%1$s" %2$s %3$s><span class="btn__text">%4$s</span>%5$s</a>',
				'class'   => 'btn',
				'title'   => '',
				'echo'    => false,
			);
			$args = wp_parse_args( $args, $default_args );
			$html = '' ;

			if ( filter_var( $args['visible'], FILTER_VALIDATE_BOOLEAN ) ) {

				if ( $args['text'] || $args['icon'] ) {

					$html_class = ( $args['class'] ) ? 'class="' . $args['class'] . '"' : '' ;
					$text = esc_html( $args['text'] );

					if ( 'term' === $type ) {

						$title = $object->name;
						$link = esc_url( get_category_link( $object->term_id ) );
					} else {
						$title = $object->post_title;
						$link = esc_url( get_the_permalink() );
					}

					$title = ( $args['title'] ) ? 'title="' . $args['title'] . '"' : 'title="' . $title . '"' ;
					$html = sprintf( $args['html'], $link, $title, $html_class, wp_kses( $text, wp_kses_allowed_html( 'post' ) ), $args['icon'] );
				}
			}

			return $this->output_method( $html, $args['echo'] );
		}

		/**
		 * [get_image description]
		 * @param  array   $args [description]
		 * @param  string  $type [description]
		 * @param  integer $id   [description]
		 * @return [type]        [description]
		 */
		public function get_post_image( $args = array(), $type = 'post', $id = 0 ) {

			if ( is_callable( array( $this, 'get_' . $type . '_object' ) ) ) {
				$object = call_user_func( array( $this, 'get_' . $type . '_object' ), $id );

				if ( 'post' === $type && empty( $object->ID ) || 'term' === $type && empty( $object->term_id ) ) {
					return '';
				}
			}

			$default_args = array(
				'visible'                => true,
				'size'                   => apply_filters( 'cherry_normal_image_size', 'post-thumbnail' ),
				'mobile_size'            => apply_filters( 'cherry_mobile_image_size', 'post-thumbnail' ),
				'html'                   => '<a href="%1$s" %2$s ><img src="%3$s" alt="%4$s" loading="lazy" %5$s ></a>',
				'class'                  => 'wp-image',
				'placeholder'            => true,
				'placeholder_background' => '000',
				'placeholder_foreground' => 'fff',
				'placeholder_title'      => '',
				'html_tag_suze'          => true,
				'echo'                   => false,
			);
			$args = wp_parse_args( $args, $default_args );
			$html = '';

			if ( filter_var( $args['visible'], FILTER_VALIDATE_BOOLEAN ) ) {

				$intermediate_image_sizes   = get_intermediate_image_sizes();
				$intermediate_image_sizes[] = 'full';
Loading ...