Skip to content

Instantly share code, notes, and snippets.

@vineettalwar
Last active March 12, 2019 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vineettalwar/bf737f43fc3db1d00cc96e3de00032d2 to your computer and use it in GitHub Desktop.
Save vineettalwar/bf737f43fc3db1d00cc96e3de00032d2 to your computer and use it in GitHub Desktop.
Ajax back and forth
<?php
class FF_Track {
public function __construct {
add_action( 'wp_ajax_nopriv_ajax_music', array($this, 'ajax_music') );
add_action( 'wp_ajax_ajax_music', array($this, 'ajax_music') );
add_action( 'wp_ajax_nopriv_ajax_next_music', array($this, 'ajax_next_music') );
add_action( 'wp_ajax_ajax_next_music', array($this, 'ajax_next_music') );
}
public function ajax_music(){
$id = $_POST['id'];
$type = $_POST['type'];
$post_type = get_post_type($id);
// Update post views
if( 'music' == $post_type || 'product' == $post_type ){
do_action( 'ff_save_views', $id);
}
if( FF()->validate_nonce() ){
if ($type == 'user') {
$tracks_get = $this->get_user_music_list($id);
} elseif ($type == 'fm_genre') {
$genre_array= wp_get_object_terms( $id, 'track_genre');
if ( ! empty( $genre_array ) ) {
if ( ! is_wp_error( $genre_array ) ) {
$genre = $genre_array[0];
print_r($genre);
$genre_id = $genre->term_id;
$posts_array = get_posts(
array(
'posts_per_page' => -1,
'post_type' => 'track',
'tax_query' => array(
array(
'taxonomy' => 'track_genre',
'field' => 'term_id',
'terms' => $genre_id,
)
)
)
);
foreach($posts_array as $post ){
$id = $post->ID ;
}
}
}
$tracks_get = $this->get_music_list($id);
} else {
$tracks_get = $this->get_music_list($id);
}
FF()->response(array(
'status' => 'success',
'tracks' => $tracks_get,
));
}
}
public function ajax_next_music($id) {
$type = 'fm_genre';
$post_type = get_post_type($id);
// Update post views
if ('music' == $post_type || 'product' == $post_type) {
do_action('ff_save_views', $id);
}
if (FF()->validate_nonce()) {
if ($type == 'fm_genre') {
$genre_array = wp_get_object_terms($id, 'track_genre');
if (!empty($genre_array)) {
if (!is_wp_error($genre_array)) {
$genre = $genre_array[0];
$genre_id = $genre->term_id;
$posts_array = get_posts(
array(
'posts_per_page' => -1,
'post_type' => 'track',
'tax_query' => array(
array(
'taxonomy' => 'track_genre',
'field' => 'term_id',
'terms' => $genre_id,
)
)
)
);
foreach ($posts_array as $post) {
if ($post->ID != $id) {
$tid = $post->ID;
print_r($tid);
}
}
}
}
}
FF()->response(array(
'status' => 'success',
'id' => $tid,
));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment