Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URL injecting with Ajax #6

Open
Si-HaMaDa opened this issue Mar 7, 2018 · 4 comments
Open

URL injecting with Ajax #6

Si-HaMaDa opened this issue Mar 7, 2018 · 4 comments

Comments

@Si-HaMaDa
Copy link

when the site is in a subdirectory the URL is not working probably in the index but works fine in archive page cause we preset the URL with "data-archive" and passing it...
for example is the site URL is
http://localhost/wordpress
in the homepage when scrolling to first div of "page-limit" the URL becomes
http://localhost/
or when load more becomes
http://localhost/page/2
So here's my solution I hope it's good
in index
`

			<div class="container text-center container-load-previous">
				<a class="btn-sunset-load sunset-load-more" data-prev="1" data-homepage="<?php echo sunset_grab_current_uri(); ?>" data-page="<?php echo sunset_check_paged(1); ?>" data-url="<?php echo admin_url('admin-ajax.php'); ?>">
					<span class="sunset-icon sunset-loading"></span>
					<span class="text">Load Previous</span>
				</a>
			</div><!-- .container -->
			
		<?php endif; ?>
		
		<div class="container sunset-posts-container">
			
			<?php 
				
				if( have_posts() ):
					
					echo '<div class="page-limit" data-page="' . sunset_grab_current_uri() . '">';
					
					while( have_posts() ): the_post();
						
						/*
						$class = 'reveal';
						set_query_var( 'post-class', $class );
						*/
						get_template_part( 'template-parts/content', get_post_format() );
					
					endwhile;
					
					echo '</div>';
					
				endif;
            
			?>
			
		</div><!-- .container -->
		
		<div class="container text-center">
			<a class="btn-sunset-load sunset-load-more" data-homepage="<?php echo sunset_grab_current_uri(); ?>" data-page="<?php echo sunset_check_paged(1); ?>" data-url="<?php echo admin_url('admin-ajax.php'); ?>">
				<span class="sunset-icon sunset-loading"></span>
				<span class="text">Load More</span>
			</a>
		</div><!-- .container -->`

in sunset.js
`$(document).on('click','.sunset-load-more:not(.loading)', function(){

	var that = $(this);
	var page = $(this).data('page');
	var newPage = page+1;
	var ajaxurl = that.data('url');
	var prev = that.data('prev');
	var archive = that.data('archive');
	var homepage = that.data('homepage');
	
	if( typeof prev === 'undefined' ){
		prev = 0;
	}
	
	if( typeof archive === 'undefined' ){
		archive = homepage;
	}
	
	that.addClass('loading').find('.text').slideUp(320);
	that.find('.sunset-icon').addClass('spin');
	
	$.ajax({
		
		url : ajaxurl,
		type : 'post',
		data : {
			
			page : page,
			prev : prev,
			archive: archive,
			action: 'sunset_load_more'
			
		},
		error : function( response ){
			console.log(response);
		},
		success : function( response ){
			
			if( response == 0 ){
				
				$('.sunset-posts-container').append( '<div class="text-center"><h3>You reached the end of the line!</h3><p>No more posts to load.</p></div>' );
				that.slideUp(320);
				
			} else {
				
				setTimeout(function(){
			
					if( prev == 1 ){
						$('.sunset-posts-container').prepend( response );
						newPage = page-1;
					} else {
						$('.sunset-posts-container').append( response );
					}
					
					if( newPage == 1 ){
						
						that.slideUp(320);
						
					} else {
						
						that.data('page', newPage);
					
						that.removeClass('loading').find('.text').slideDown(320);
						that.find('.sunset-icon').removeClass('spin');
						
					}
					
					revealPosts();
					
				}, 1000);
				
			}
			
			
		}
		
	});
	
});`

in ajax.php
`function sunset_load_more()
{
$paged = $_POST['page'] + 1;
$prev = $_POST['prev'];
$archive = $_POST['archive'];

if ($prev == 1 && $_POST['page'] != 1) {
    $paged = $_POST['page'] - 1;
}

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'paged' => $paged,
);

if ($archive != '0') {
    $archVal = explode('/', $archive);
    $flipped = array_flip($archVal);

    switch (isset($flipped)) {

        case $flipped['category']:
            $type = 'category_name';
            $key = 'category';
            break;

        case $flipped['tag']:
            $type = 'tag';
            $key = $type;
            break;

        case $flipped['author']:
            $type = 'author';
            $key = $type;
            break;

    }

    $currKey = array_keys($archVal, $key);
    $nextKey = $currKey[0] + 1;
    $value = $archVal[ $nextKey ];

    $args[ $type ] = $value;

    //check page trail and remove "page" value
    if (isset($flipped['page'])) {
        $pageVal = explode('page', $archive);
        $page_trail = $pageVal[0];
    } else {
        $page_trail = $archive;
    }
} else {
    $page_trail = '/';
}

$query = new WP_Query($args);

if ($query->have_posts()):

    echo '<div class="page-limit" data-page="'.$page_trail.'page/'.$paged.'/">';

while ($query->have_posts()): $query->the_post();

get_template_part('template-parts/content', get_post_format());

endwhile;

echo '</div>'; else:

    echo 0;

endif;

wp_reset_postdata();

die();

}and as improvment in archive.php

			<div class="container text-center container-load-previous">
				<a class="btn-sunset-load sunset-load-more" data-prev="1" data-archive="<?php echo sunset_grab_current_uri(); ?>" data-page="<?php echo sunset_check_paged(1); ?>" data-url="<?php echo admin_url('admin-ajax.php'); ?>">
					<span class="sunset-icon sunset-loading"></span>
					<span class="text">Load Previous</span>
				</a>
			</div><!-- .container -->

		<?php endif; ?>

		<div class="container sunset-posts-container">

			<?php

				if( have_posts() ):

					echo '<div class="page-limit" data-page="' . sunset_grab_current_uri() . '">';

					while( have_posts() ): the_post();

						get_template_part( 'template-parts/content', get_post_format() );

					endwhile;

					echo '</div>';

				endif;

			?>

		</div><!-- .container -->

		<div class="container text-center">
			<a class="btn-sunset-load sunset-load-more" data-page="<?php echo sunset_check_paged(1); ?>" data-archive="<?php echo sunset_grab_current_uri(); ?>" data-url="<?php echo admin_url('admin-ajax.php'); ?>">
				<span class="sunset-icon sunset-loading"></span>
				<span class="text">Load More</span>
			</a>
		</div><!-- .container -->`

what I did is passing the full URL like you did in archive.php
I passed the full URL even in index...
I hope you see it and tell if there's something wrong in my code

@iswaq
Copy link

iswaq commented Mar 7, 2019

I have the same issue, is there any solution?

@Si-HaMaDa
Copy link
Author

I have the same issue, is there any solution?

I've fixed it... copy and paste the code in your files and try
also you can read to understand what I did

@iswaq
Copy link

iswaq commented Mar 7, 2019

I just uploaded my theme on the hosting server, it is working fine there. It means, it is creating problem with localhost.

@Si-HaMaDa
Copy link
Author

I just uploaded my theme on the hosting server, it is working fine there. It means, it is creating problem with localhost.

the problem isn't with local host or server
the problem will appear with sub-directory even on you server
try to make a wordpress installation on sub-directory on your server and see

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants