array( 'name' => esc_html__( 'Posts Ajax', 'mega' ), 'description' => esc_html__( 'Recent, popular & random posts block', 'mega' )), 'universal' => true, 'true-wrapper' => false, 'class_base' => 'ys-timeline ys-timeline-posts' ); add_action( 'wp_ajax_nopriv_timeline_ajax', array( $this, 'ajaxRequest' )); add_action( 'wp_ajax_timeline_ajax', array( $this, 'ajaxRequest' )); parent::__construct( $id, $args ); } public function hookOnce() { //if ( !mega_is_mobile() ) add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' )); } public function enqueue() { wp_enqueue_script( 'mega.jquery.posts.timeline', MEGA_DIR_URI . '/assets/js/mega.jquery.posts.timeline.js', array( 'jquery' ), false, true ); } public function settings( $form ) { $form->add_control( 'Mega_Control_Select', array( 'label' => esc_html__( 'Order By', 'mega' ), 'value' => 'date', 'name' => 'order', 'choices' => array( 'date' => esc_html__( 'Date', 'mega' ), 'rand' => esc_html__( 'Random', 'mega' ), 'comment_count' => esc_html__( 'Comment Count', 'mega' ) ) )); $form->add_control( 'Mega_Control_Number', array( 'label' => esc_html__( 'Number of Results', 'mega' ), 'value' => 8, 'name' => 'count' )); $form->add_control( 'Mega_Control_Select', array( 'label' => esc_html__( 'Select a Category', 'mega' ), 'name' => 'cat', 'use_list' => 1, 'list' => 'mega_cat_list' )); $form->add_control( 'Mega_Control_onOff', array( 'label' => esc_html__( 'Image', 'mega' ), 'value' => 1, 'name' => 'image' )); $form->add_sub_control( 'Mega_Control_Number', array( 'control' => 'image', 'control_value' => 'true', 'label' => esc_html__( 'Image Width (%)', 'mega' ), 'value' => 100, 'name' => 'width' )); $form->add_sub_control( 'Mega_Control_Number', array( 'control' => 'image', 'control_value' => 'true', 'label' => esc_html__( 'Image Max Height (px)', 'mega' ), 'value' => 200, 'name' => 'max_height' )); $form->add_sub_control( 'Mega_Control_onOff', array( 'control' => 'image', 'control_value' => 'true', 'label' => esc_html__( 'Pull images from post meta using prefixed meta key: Theme-Name_thumb', 'mega' ), 'value' => false, 'name' => 'image_source_meta' )); $form->add_sub_control( 'Mega_Control_Checklist', array( 'control' => 'image', 'control_value' => 'true', 'label' => esc_html__( 'Image Sources', 'mega' ), 'value' => array( 'featured' => 1, 'first' => 1, 'ph' => 1 ), 'name' => 'sources', 'choices' => array( 'featured' => esc_html__( 'Featured', 'mega' ), 'first' => esc_html__( 'First', 'mega' ), 'ph' => esc_html__( 'Placeholder', 'mega' ) ) )); $form->add_control( 'Mega_Control_Text', array( 'label' => esc_html__( 'Layout', 'mega' ), 'desc' => wp_kses( __( 'Use [title], [excerpt] and [meta] snippets to format the layout order.

Title - [title]
Excerpt - [excerpt]
Meta - [meta]', 'mega' ), array( 'br' => true, 'strong' => true )), 'value' => '[title][excerpt]', 'name' => 'layout' )); $form->add_control( 'Mega_Control_Number', array( 'label' => esc_html__( 'Excerpt length', 'mega' ), 'value' => 40, 'name' => 'length' )); $form->add_control( 'Mega_Control_Text', array( 'label' => esc_html__( 'Meta', 'mega' ), 'desc' => wp_kses( __( 'Use [date], [comments] and [author] snippets to format the meta order.

Date - [date]
Comments - [comments]
Author - [author]', 'mega' ), array( 'br' => true, 'strong' => true )), 'value' => '[comments][date]', 'name' => 'meta' )); $form->add_control( 'Mega_Control_Text', array( 'label' => esc_html__( 'Date Format', 'mega' ), 'value' => esc_html__( 'F jS, Y', 'mega' ), 'name' => 'date' )); } public function block() { extract( $this->mega['settings'] ); $page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; $query = new WP_Query( array( 'post_type' => 'post', 'orderby' => $order, //'ignore_sticky_posts' => 1, 'posts_per_page' => mega_is_mobile() && $count > 3 ? 3 : $count, 'cat' => $cat, 'paged' => $page )); echo '
'; echo ''; echo ''; echo '
'; echo '' . esc_html__( 'Load more', 'mega' ) . ''; wp_reset_postdata(); } public function post() { extract( $this->mega['settings'] ); echo '
  • '; echo ''; if ( $image ) { mega_thumb( array( 'width' => $width, 'height' => $height, 'sources' => $sources )); } echo '
    '; echo mega_parse_message( array( 'title' => sprintf( '%3$s', esc_url( get_permalink() ), esc_attr( sprintf( esc_html__( 'Permalink to %s', 'mega' ), get_the_title() )), esc_html( substr( get_the_title(), 0, 50 )) ), 'excerpt' => '
    ' . esc_html( substr( get_the_excerpt(), 0, $length )) . '
    ', 'meta' => mega_parse_message( array( 'date' => $this->getMeta( 'date', $this->mega ), 'comments' => $this->getMeta( 'comments', $this->mega ), 'author' => $this->getMeta( 'author', $this->mega ) ), '
    ' . $meta . '
    ' ) ), $layout ); echo '
    '; echo '
  • '; } public function ajaxRequest() { check_ajax_referer( 'security', 'nounce' ); $post = wp_unslash( $_POST ); $this->parseWidgetSettings( $post['instance'] ); extract( $this->mega['settings'] ); $args = array( 'post_type' => 'post', 'orderby' => $order, 'posts_per_page' => mega_is_mobile() && $count > 3 ? 3 : $count, 'cat' => $cat, 'paged' => $post['page'] ); $query = new WP_Query( $args ); while ( $query->have_posts() ) : $query->the_post(); $this->post(); endwhile; wp_reset_postdata(); exit; } public function getMeta( $item, $instance ) { return mega_html( esc_html( $this->getWrap( $item, $instance )), '', '', false ); } public function getWrap( $item, $instance ) { extract( $instance['settings'] ); switch( $item ) { case 'date' : return get_the_date( $date ); case 'comments' : return !post_password_required() ? sprintf( esc_html__( '%s Comments', 'mega' ), get_comments_number() ) : ''; case 'author' : return sprintf( esc_html__( 'By %s', 'mega' ), get_the_author() ); } } }