Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. $args= array(
  2. 'meta_key' => 'c3m_shown_on',
  3. 'meta_value'=> 'home' );
  4. $box_query = new WP_Query($args); ?>
  5. <ul id="sortable">
  6. <?php
  7. while ($box_query->have_posts()) : $box_query->the_post(); global $post; global $prefix;
  8. $box_size = c3m_get_field($prefix.'box_size', FALSE);
  9. $box_image = c3m_get_field($prefix.'post_box_image', FALSE);
  10. $overlay_class = c3m_get_field($prefix.'overlay_class', FALSE);
  11.  
  12. if ( c3m_get_field($prefix.'external_link', FALSE) ) {
  13. $post_link = c3m_get_field($prefix.'external_link', FALSE);
  14. } else
  15. { $post_link = post_permalink();
  16. } ?>
  17. <li class="<?php echo $box_size;?> ui-state-default">
  18. <article <?php post_class() ?> id="post-<?php the_ID(); ?>">
  19. <?php echo '<a href="'.$post_link.'" ><img src="'.esc_url($box_image).'" alt="Image via xxxxx.com" /></a>'; ?>
  20. <div class="post-box <?php echo $overlay_class;?>">
  21. <?php if ( c3m_get_field( $prefix.'text_display', FALSE) ) { ?>
  22. <h2><a href="<?php echo $post_link?>"><?php the_title();?></a></h2>
  23. <p><?php echo substr($post->post_excerpt, 0, 90) . '...'; ?></p>
  24. <?php } ?>
  25. </div>
  26. </article>
  27. </li>
  28. <?php endwhile; ?>
  29. </ul>
  30. </section>
  31.  
  32. jQuery(document).ready(function() {
  33. jQuery("#sortable").sortable();
  34. });
  35.  
  36. function c3m_load_scripts() {
  37. if ( current_user_can( 'edit_posts' ) ) {
  38. wp_enqueue_script( 'jquery-ui' );
  39. wp_enqueue_script( 'functions', get_bloginfo( 'stylesheet_directory' ) . '/_/js/functions.js', array( 'jquery', 'jquery-ui' ), false);
  40. wp_localize_script( 'functions', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
  41. }
  42. }
  43.  
  44. jQuery(document).ready(function($) {
  45. var itemList = $('#sortable');
  46.  
  47. itemList.sortable({
  48. update: function(event, ui) {
  49. $('#loading-animation').show(); // Show the animate loading gif while waiting
  50.  
  51. opts = {
  52. url: ajaxurl, // ajaxurl is defined by WordPress and points to /wp-admin/admin-ajax.php
  53. type: 'POST',
  54. async: true,
  55. cache: false,
  56. dataType: 'json',
  57. data:{
  58. action: 'item_sort', // Tell WordPress how to handle this ajax request
  59. order: itemList.sortable('toArray').toString() // Passes ID's of list items in 1,3,2 format
  60. },
  61. success: function(response) {
  62. $('#loading-animation').hide(); // Hide the loading animation
  63. return;
  64. },
  65. error: function(xhr,textStatus,e) { // This can be expanded to provide more information
  66. alert(e);
  67. // alert('There was an error saving the updates');
  68. $('#loading-animation').hide(); // Hide the loading animation
  69. return;
  70. }
  71. };
  72. $.ajax(opts);
  73. }
  74. });
  75. });
  76.  
  77. function my_save_item_order() {
  78. global $wpdb;
  79.  
  80. $order = explode(',', $_POST['order']);
  81. $counter = 0;
  82. foreach ($order as $item_id) {
  83. $wpdb->update($wpdb->posts, array( 'menu_order' => $counter ), array( 'ID' => $item_id) );
  84. $counter++;
  85. }
  86. die(1);
  87. }
  88. add_action('wp_ajax_item_sort', 'my_save_item_order');
  89. add_action('wp_ajax_nopriv_item_sort', 'my_save_item_order');
  90.  
  91. $args= array(
  92. 'meta_key' => 'c3m_shown_on',
  93. 'meta_value'=> 'home'
  94. 'orderby' => 'menu_order',
  95. 'order' => 'ASC'
  96. );
  97.  
  98. $box_query = new WP_Query($args);
  99.  
  100. <img src="<?php bloginfo('url'); ?>/wp-admin/images/loading.gif" id="loading-animation" />
  101. <ul id="sortable">
  102. <li id="{echo post ID here}">{echo title or other name here}</li>
  103. </ul>
  104.  
  105. /**
  106. * Enqueue javascript and css files
  107. */
  108. function uc_enqueue_my_assets() {
  109. wp_enqueue_script( 'jquery-ui-sortable');
  110. wp_register_script( 'order', plugins_url( '/js/order.js', __FILE__ ), array( 'jquery' ) );
  111. wp_enqueue_script( 'order' );
  112. }
  113.  
  114. function uc_is_user_logged_in()
  115. {
  116. if ( is_user_logged_in()) {
  117. add_action( 'wp_enqueue_scripts', 'uc_enqueue_my_assets' );
  118. add_action( 'admin_enqueue_scripts', 'uc_enqueue_my_assets' );
  119. }
  120. }
  121. add_action('init', 'uc_is_user_logged_in');
  122.  
  123.  
  124. /**
  125. * Update order of posts by ajax on trigger of drag and drop event
  126. */
  127. function uc_sort_post_items() {
  128.  
  129. $order = wp_parse_id_list(explode(',', $_POST['order']));
  130. write_log($order);
  131.  
  132. global $wpdb;
  133. $list = join(', ', $order);
  134. $wpdb->query('SELECT @i:=0');
  135. $wpdb->query(
  136. "UPDATE wp_posts SET menu_order = ( @i:= @i+1 )
  137. WHERE ID IN ( $list ) ORDER BY FIELD( ID, $list );"
  138. );
  139.  
  140. wp_die();
  141. }
  142. add_action('wp_ajax_uc_sort_post_items', 'uc_sort_post_items');
  143. add_action('wp_ajax_nopriv_uc_sort_post_items', 'uc_sort_post_items');
  144.  
  145.  
  146.  
  147. /**
  148. * Display sorted posts
  149. */
  150. function uc_pre_get_posts( $wp_query ) {
  151. write_log(basename($_SERVER['PHP_SELF']));
  152. $wp_query->set('orderby', 'menu_order');
  153. $wp_query->set('order', 'ASC');
  154. }
  155. add_action( 'pre_get_posts', 'uc_pre_get_posts', 1 );
  156.  
  157. $('#the-list').sortable({
  158. update: function(event, ui) {
  159.  
  160. $.ajax({
  161.  
  162. url: '/wp-admin/admin-ajax.php',
  163. type: 'post',
  164. dataType: 'json',
  165. data:{
  166. action: 'uc_sort_post_items', // Tell WordPress how to handle this ajax request
  167. order: '4567,4569,4565 ' // Passes ID's of list items in 1,3,2 format
  168. },
  169. success: function(data, response) {
  170. console.log(response);
  171. },
  172. error: function(xhr,textStatus,e) {
  173. alert(e);
  174. }
  175.  
  176. });
  177.  
  178. }
  179. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement