-
-
Save DavidPeralvarez/455ce01cd89444926b8d468d43ac46a4 to your computer and use it in GitHub Desktop.
Plugin de Ideas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$( "#ideas" ).on( "click", ".vote-btn", function(e) { | |
e.preventDefault(); | |
var voteButton = $(this), | |
postId = voteButton.parent().data('id'), | |
votes = voteButton.parent().find('.votes').text(), | |
operation = ( voteButton.hasClass('vote-plus') ) ? "plus" : "minus"; | |
$.ajax({ | |
type: 'post', | |
dataType: 'html', | |
url: a2_parameters.ajax_url, | |
data: { | |
action: 'a2_update_votes', | |
_ajax_nonce: a2_parameters.nonce, | |
votes: votes, | |
postId: postId, | |
operation: operation | |
}, | |
beforeSend: function(){ | |
console.log( 'Cargando...' ); | |
}, | |
success: function( response ){ | |
$( "#ideas" ).html( response ); | |
} | |
}); | |
}); | |
}) ( jQuery ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Ajax 2 | |
Description: Ideas | |
*/ | |
// Register Custom Post Type | |
add_action( 'init', 'a2_ideas_post_type' ); | |
function a2_ideas_post_type() { | |
$labels = [ | |
'name' => 'Ideas', | |
'singular_name' => 'Idea', | |
]; | |
$args = array( | |
'label' => 'Idea', | |
'labels' => $labels, | |
'supports' => array( 'title' ), | |
'hierarchical' => false, | |
'public' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'menu_position' => 5, | |
'menu_icon' => 'dashicons-lightbulb', | |
'show_in_admin_bar' => true, | |
'show_in_nav_menus' => true, | |
'can_export' => false, | |
'has_archive' => true, | |
'exclude_from_search' => true, | |
'publicly_queryable' => true, | |
'capability_type' => 'page', | |
'show_in_rest' => true, | |
); | |
register_post_type( 'a2_idea', $args ); | |
} | |
// Initialize votes | |
add_action( 'save_post', 'a2_initialize_votes', 10, 3 ); | |
function a2_initialize_votes( $post_id, $post, $update ) { | |
if ( $update ) return; // return if we're updating | |
if ( 'a2_idea' == $post->post_type ): | |
update_post_meta( $post_id, 'a2_votes', 0 ); | |
endif; | |
} | |
// Create shortcode to list ideas | |
add_shortcode( 'ideas', 'a2_ideas_func' ); | |
function a2_ideas_func(){ | |
$result .= '<ul id="ideas">'; | |
$result .= a2_query_ideas(); | |
$result .= '</ul>'; | |
return $result; | |
} | |
// Helper function to query ideas | |
function a2_query_ideas(){ | |
$args = [ | |
'post_type' => 'a2_idea', | |
'post_status' => 'publish', | |
'posts_per_page' => -1, | |
'orderby' => 'meta_value_num', | |
'meta_key' => 'a2_votes', | |
'order' => 'DESC' | |
]; | |
$ideas_query = new WP_Query( $args ); | |
$result = ''; | |
if( $ideas_query->have_posts() ): | |
while( $ideas_query->have_posts() ): | |
$ideas_query->the_post(); | |
$post_id = get_the_ID(); | |
$current_votes = get_post_meta( $post_id, 'a2_votes', true ); | |
$result .= '<li data-id="' . $post_id . '">' . get_the_title() . ' | <span class="votes">' . $current_votes . '</span> | <a href="#" class="vote-btn vote-plus">Más</a> | <a href="#" class="vote-btn vote-minus">Menos</a></li>'; | |
endwhile; | |
endif; | |
wp_reset_postdata(); | |
return $result; | |
} | |
// Enqueue scripts | |
add_action( 'wp_enqueue_scripts', 'a2_enqueue_scripts' ); | |
function a2_enqueue_scripts(){ | |
wp_enqueue_script( | |
'ajax-2', | |
plugins_url( '/ajax-2.js', __FILE__ ), | |
['jquery'], | |
'1.0', | |
true | |
); | |
wp_localize_script( | |
'ajax-2', | |
'a2_parameters', | |
[ | |
'ajax_url' => admin_url( 'admin-ajax.php' ), | |
'nonce' => wp_create_nonce( 'a2_nonce' ) | |
] | |
); | |
} | |
// Like a post | |
add_action( 'wp_ajax_a2_update_votes', 'a2_update_votes' ); | |
add_action( 'wp_ajax_nopriv_a2_update_votes', 'a2_update_votes' ); | |
function a2_update_votes(){ | |
check_ajax_referer( 'a2_nonce' ); | |
$votes = intval( $_POST['votes'] ); | |
$post_id = intval( $_POST['postId'] ); | |
$operation = $_POST['operation']; | |
if( $operation == 'plus' ): | |
$votes++; | |
elseif( ( $operation == 'minus' ) && ( $votes > 0 ) ) : | |
$votes--; | |
endif; | |
update_post_meta( $post_id, 'a2_votes', $votes ); | |
echo a2_query_ideas(); | |
wp_die(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment