is_single = true == $this->is_single() ? $this->is_single() : $this->is_single;
$this->is_loop = true == $this->is_loop() ? $this->is_loop() : $this->is_loop;
$this->paged = $this->paged();
if ( $this->is_single ){
// amp-next-page experiment meta tag
add_action('amp_experiment_meta', array( $this, 'amp_experiment_meta') );
// amp-next-page script
add_filter('amp_post_template_data', array( $this , 'amp_infinite_scroll_script') );
// amp-next-page tag
if ( 4 != ampforwp_get_setting('amp-design-selector') )
add_action('ampforwp_above_related_post', array( $this , 'amp_next_page') );
else
add_action('ampforwp_single_design_type_handle', array( $this , 'amp_next_page') );
}
if ( $this->is_loop ) {
// amp-next-page experiment meta tag
add_action('amp_experiment_meta', array( $this, 'amp_experiment_meta') );
// amp-next-page script
add_filter('amp_post_template_data', array( $this , 'amp_infinite_scroll_script') );
// amp-next-page tag
add_action('ampforwp_loop_before_pagination', array( $this , 'amp_next_page') );
// Next Posts Link
add_filter('ampforwp_next_posts_link', array( $this , 'next_posts_link') , 10 , 2 );
}
}
public function is_single() {
if ( is_single() && true == ampforwp_get_setting('ampforwp-infinite-scroll-single') ) {
return true;
}
return false;
}
public function is_loop() {
if ( (ampforwp_is_home() || is_archive()) && true == ampforwp_get_setting('ampforwp-infinite-scroll-home') ) {
return true;
}
return false;
}
public function paged() {
if ( get_query_var( 'paged' ) ) {
return get_query_var('paged');
} elseif ( get_query_var( 'page' ) ) {
return get_query_var('page');
} else {
return 1;
}
}
public function amp_experiment_meta() {
echo '';
}
public function amp_infinite_scroll_script( $data ) {
if ( empty( $data['amp_component_scripts']['amp-next-page'] ) ) {
$data['amp_component_scripts']['amp-next-page'] = 'https://cdn.ampproject.org/v0/amp-next-page-0.1.js';
}
return $data;
}
public function amp_next_page() {
$loop_link = $first_url = $first_title = $first_image = $second_url = $second_image = $second_title ='';
$single_links = $single_titles = $single_images = $classes = $pages = array();
if ( $this->is_loop ) {
$loop_link = $this->loop_link();
$loop_link1 = $loop_link2 = '';
$loop_link1 = $loop_link.($this->paged+1);
$loop_link2 = $loop_link.($this->paged+2);
if ( true == ampforwp_get_setting('amp-core-end-point') ) {
$loop_link1 = ampforwp_url_controller($loop_link1);
$loop_link2 = ampforwp_url_controller($loop_link2);
}
$pages[] = array('title'=>'','image'=>'','ampUrl'=>$loop_link1);
$pages[] = array('title'=>'','image'=>'','ampUrl'=>$loop_link2);
}
if ( $this->is_single ) {
$pages = $this->single_post();
}
$classes = $this->hide();
?>
ID;
$query_args = array(
'post_type' => get_post_type(),
'orderby' => 'date',
'ignore_sticky_posts' => 1,
'paged' => esc_attr($this->paged),
'post__not_in' => $exclude_ids,
'has_password' => false ,
'post_status'=> 'publish',
'posts_per_page' => 2
);
$query = new WP_Query( $query_args );
while ($query->have_posts()) {
$query->the_post();
$pages[] = array('title'=>get_the_title(),'image'=>ampforwp_get_post_thumbnail('url', 'full'),'ampUrl'=>ampforwp_url_controller( get_permalink() ));
}
wp_reset_postdata();
return $pages;
}
public function loop_link() {
global $wp;
$amp_url = trailingslashit(home_url($wp->request));
if( $this->paged < 2 ) {
$amp_url = trailingslashit($amp_url.'page');
}
else
$amp_url = str_replace('/'.$this->paged, '', $amp_url);
return $amp_url;
}
public function hide() {
$classes = array();
$design = ampforwp_get_setting('amp-design-selector');
if ( 1 == $design ) {
$classes = array("#pagination",".related_posts", ".amp-wp-footer",".amp-wp-header",".f-w");
}
if ( 2 == $design ) {
$classes = array("#headerwrap","#pagination","#footer",".nav_container",".related_posts",".f-w");
}
if ( 3 == $design ) {
$classes = array("#headerwrap",".relatedpost",".footer_wrapper",".pagination-holder",".f-w");
}
if ( 4 == $design ) {
$classes = array(".p-m-fl",".loop-pagination",".footer",".r-pf",".srp ul",".srp h3","#pagination",".h_m_w", ".f-w");
}
return json_encode($classes);
}
public function next_posts_link( $next_link , $paged ) {
// Change the next link to paged+3
// reason: amp-next-page will show the results for 3 pages
$next_link = preg_replace('/'.($paged+1).'/', ($paged+3), $next_link);
return $next_link;
}
}
// Initiate the Class
new AMPforWP_Infinite_Scroll();
}