Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.12 KB | None | 0 0
  1. <?php /* Mystique/digitalnature */
  2.  
  3.  
  4. // //based on Austin Matzko's code from wp-hackers email list -- not used anymore
  5. // function mystique_filter_where($where = '') {
  6. //  //posts in the last 30 days
  7. //  $where .= " AND post_date > '".date('Y-m-d', strtotime('-'.get_mystique_option('featured_timeframe').' days'))."'";
  8. //  return $where;
  9. // }
  10. //  add_filter('posts_where', 'mystique_filter_where');
  11.  
  12. // check if the user wants featured posts enabled on whatever page we're on
  13. function mystique_featured_post_target_page(){
  14.  $enable = (is_page_template('page-featured.php')) || (get_mystique_option('featured_on_home') && is_home()) || (get_mystique_option('featured_on_single') && is_singular()) || (get_mystique_option('featured_on_archives') && is_archive()) || (get_mystique_option('featured_on_404') && is_404()) || (get_mystique_option('featured_on_search') && is_search()) || (get_mystique_option('featured_on_archives') && is_archive()) || (get_mystique_option('featured_on_pages') && is_page());
  15.  return ($enable ? true : false);
  16.  
  17. }
  18.  
  19. function mystique_featured_post() {
  20.   if(mystique_featured_post_target_page()):
  21.    //global $valid_featured_posts;
  22.  
  23.  
  24.   global $wp_query;
  25.   $featured_posts = get_mystique_option('featured_posts');
  26.  
  27.   $found_posts = array();
  28.   $featured_posts = explode(',', $featured_posts);
  29.   shuffle($featured_posts); // randomize
  30.  
  31.   foreach($featured_posts as $featured_post):
  32.     $current_post = get_post($featured_post);
  33.     $current_post = (array)$current_post;
  34.  
  35.     if($current_post)       // take it in consideration only if is a recent post
  36.      if(strtotime($current_post['post_date_gmt']) > strtotime('-'.get_mystique_option('featured_timeframe').' days')) $found_posts[] = $current_post;
  37.   endforeach;
  38.  
  39.   // remove posts from the main loop (on homepage)?
  40.   // $wp_query->query_vars['post__not_in'] = $found_posts;
  41.  
  42.    $count = (int)get_mystique_option('featured_count');
  43.   ?>
  44.  
  45. <div id="featured-content"<?php if($count > 1 && count($found_posts) > 1): ?> class="withSlider"<?php endif; ?>>
  46.  <?php if($found_posts): // only show if the global variable is set in the function above ?>
  47.  <!-- block container -->
  48.  <div class="slide-container">
  49.   <ul class="slides">
  50.        <?php
  51.         $number = 1;
  52.         foreach($found_posts as $featured_post): ?>
  53.          <!-- slide (100%) -->
  54.          <li class="slide slide-<?php echo $number; ?> featured-content">
  55.           <div class="slide-content clearfix">
  56.             <div class="details clearfix">
  57.              <?php
  58.               mystique_post_thumb('featured-thumbnail', $featured_post['ID']);
  59.               echo '<h2><a href="'.get_permalink($featured_post['ID']).'">'.mystique_strip_string(70, strip_tags($featured_post['post_title'])).'</a></h2>';
  60.               echo '<div class="summary">'.mystique_strip_string(420, strip_tags(strip_shortcodes($featured_post['post_content']))).'</div>';
  61.              ?>
  62.             </div>
  63.           <a class="readmore" href="<?php echo get_permalink($featured_post['ID']); ?>"><?php _e("More","mystique"); ?></a>
  64.           </div>
  65.          </li>
  66.          <!-- /slide -->
  67.          <?php
  68.           if($count == $number) break;
  69.           $number++;
  70.         endforeach;
  71.        ?>
  72.   </ul>
  73.  </div>
  74.  <!-- /block container -->
  75.  <?php
  76.   else:
  77.    if (current_user_can('switch_themes')) echo '<h4>'.sprintf(__("No featured posts found older than %s days.", "mystique"), get_mystique_option('featured_timeframe')).'</h4>';
  78.   endif; ?>
  79. </div>
  80.  
  81.    <?php
  82.   endif;
  83. }
  84.  
  85. function mystique_featured_post_class($class){
  86.   global $post;
  87.   $featured_posts = explode(',', get_mystique_option('featured_posts'));
  88.  
  89.   // featured post?
  90.   if (in_array($post->ID, $featured_posts)) $class .= ' featured';
  91.   return $class;
  92. }
  93.  
  94.  
  95. function mystique_add_featured_posts_column($defaults) {
  96.   $defaults['featured'] = __('Featured','mystique');
  97.   return $defaults;
  98. }
  99.  
  100. // build the post listing cta for each row
  101. function mystique_featured_posts_column($column_name, $id) {
  102.   if ($column_name == 'featured'):
  103.     $featured_posts = get_mystique_option('featured_posts');
  104.     $featured_arr = explode(',', $featured_posts);
  105.     echo '<div class="featured_post"><a id="featured-'.$id.'"  class="'.(in_array($id, $featured_arr) ? "on" : "off").'"></a></div>';
  106.   endif;
  107. }
  108.  
  109. function mystique_featured_process_js() {
  110.   wp_print_scripts(array('jquery'));
  111.   ?>
  112.   <script type="text/javascript">
  113.   //<![CDATA[
  114.  
  115.   jQuery(document).ready(function ($) {
  116.    jQuery(".featured_post a").click(function () {
  117.     $link = jQuery(this);
  118.     var pos = $link.attr('id').lastIndexOf('-');
  119.     var targetID = $link.attr('id').substr(++pos);
  120.  
  121.     jQuery.ajax({
  122.         url: '<?php bloginfo( 'wpurl' ); ?>/wp-admin/admin-ajax.php',
  123.         type: "GET",
  124.         data: ({action: 'featured_process', id: targetID, isOn: ($link.hasClass('on') ? 1 : 0)  }),
  125.         beforeSend: function() {
  126.           $link.attr("class","loading");
  127.         },
  128.  
  129.         error: function(request){
  130.            alert('<?php echo wp_specialchars(__("Error while featuring posts","mystique"), 'single'); // thanks Frasten :) ?>');
  131.           $link.attr('class',"error");
  132.         },
  133.  
  134.         success: function(data) {
  135.           $link.attr('class',data);
  136.         }
  137.  
  138.     });
  139.    });
  140.   });
  141.  
  142.   //]]>
  143.   </script>
  144. <?php
  145. }
  146.  
  147.  
  148. // the ajax
  149. function mystique_featured_process() {
  150.  
  151.   $mystique_settings = get_option('mystique');
  152.  
  153.   // read submitted information
  154.   $id = $_GET['id'];
  155.   $is_on = $_GET['isOn'];
  156.  
  157.   $featured_arr = $mystique_settings['featured_posts'] ? explode(',', $mystique_settings['featured_posts']) : array();
  158.  
  159.   // add to array if not on and not currently in the array
  160.   if (!$is_on && ! in_array($id, $featured_arr)) array_push($featured_arr, $id);
  161.   rsort($featured_arr);
  162.  
  163.   $featured_str = '';
  164.  
  165.   // if not the same as selected, add to the featured str
  166.   foreach ($featured_arr as $post_id) if (!($is_on && $post_id == $id)) $featured_str .= $post_id.',';
  167.   if ($featured_str) $featured_str = substr($featured_str, 0, -1);
  168.  
  169.   $mystique_settings['featured_posts'] = $featured_str;
  170.   update_option('mystique', $mystique_settings);
  171.  
  172.   // reverse classes
  173.   die($is_on ? 'off' : 'on');
  174. }
  175.  
  176.  
  177. function mystique_featured_default_settings($defaults){
  178. //  $d = array();
  179. //  if(!get_mystique_option('featured_posts')): // query only if there are no f.p. set
  180. //    $a_few_random_posts = get_posts('numberposts=3&orderby=rand');
  181. //    $a_few_random_posts = mystique_objectToArray($a_few_random_posts);
  182. //    foreach($a_few_random_posts as $random_post) $d[] = $random_post['ID'];
  183. //  endif;
  184. //  $defaults['featured_posts'] = implode(",", $d);
  185.  
  186.   $defaults['featured_posts'] = '';
  187.   $defaults['featured_timeframe'] = 30; // days
  188.   $defaults['featured_on_home'] = 0;
  189.   $defaults['featured_on_single'] = 0;
  190.   $defaults['featured_on_archives'] = 0;
  191.   $defaults['featured_on_404'] = 0;
  192.   $defaults['featured_on_search'] = 0;
  193.   $defaults['featured_on_pages'] = 0;
  194.   $defaults['featured_count'] = 5;
  195.   $defaults['featured_timeout'] = 10;
  196.   return $defaults;
  197. }
  198.  
  199. function mystique_featured_admin(){ ?>
  200.  <tr>
  201.   <th scope="row"><p><?php _e("Featured posts","mystique"); ?><span><?php printf(__("You can mark posts as featured on the %s page", "mystique"), '<a href="'.admin_url('edit.php').'">Posts</a>'); ?></span></p></th>
  202.   <td>
  203.  
  204.     <table>
  205.  
  206.      <tr>
  207.       <th scope="row"><p><?php _e("Show on","mystique"); ?></p></th>
  208.       <td>
  209.  
  210.        <label for="opt_featured_on_template">
  211.          <input disabled="disabled" checked="checked" name="featured_on_template" id="opt_featured_on_template" type="checkbox" class="checkbox" value="1" ?> <?php _e("Pages that use the Featured posts template", "mystique"); ?>
  212.        </label>
  213.        <br />
  214.  
  215.        <label for="opt_featured_on_home">
  216.          <input name="featured_on_home" id="opt_featured_on_home" type="checkbox" class="checkbox" value="1" <?php checked('1', get_mystique_option('featured_on_home')) ?> /> <?php _e("Home page", "mystique"); ?>
  217.        </label>
  218.        <br />
  219.  
  220.        <label for="opt_featured_on_single">
  221.          <input name="featured_on_single" id="opt_featured_on_single" type="checkbox" class="checkbox" value="1" <?php checked('1', get_mystique_option('featured_on_single')) ?> /> <?php _e("Single post pages", "mystique"); ?>
  222.        </label>
  223.        <br />
  224.  
  225.        <label for="opt_featured_on_archives">
  226.          <input name="featured_on_archives" id="opt_featured_on_archives" type="checkbox" class="checkbox" value="1" <?php checked('1', get_mystique_option('featured_on_archives')) ?> /> <?php _e("Archive pages (Category, Tags etc)", "mystique"); ?>
  227.        </label>
  228.        <br />
  229.  
  230.        <label for="opt_featured_on_404">
  231.          <input name="featured_on_404" id="opt_featured_on_404" type="checkbox" class="checkbox" value="1" <?php checked('1', get_mystique_option('featured_on_404')) ?> /> <?php _e("404 pages", "mystique"); ?>
  232.        </label>
  233.        <br />
  234.  
  235.        <label for="opt_featured_on_search">
  236.          <input name="featured_on_search" id="opt_featured_on_search" type="checkbox" class="checkbox" value="1" <?php checked('1', get_mystique_option('featured_on_search')) ?> /> <?php _e("Search page", "mystique"); ?>
  237.        </label>
  238.        <br />
  239.  
  240.        <label for="opt_featured_on_pages">
  241.          <input name="featured_on_pages" id="opt_featured_on_pages" type="checkbox" class="checkbox" value="1" <?php checked('1', get_mystique_option('featured_on_pages')) ?> /> <?php _e("All other pages", "mystique"); ?>
  242.        </label>
  243.  
  244.       </td>
  245.      </tr>
  246.  
  247.      <tr>
  248.       <th scope="row"><p><?php _e("Maximum number of posts to display","mystique"); ?></p></th>
  249.       <td>
  250.        <input type="text" size="5" name="featured_count" value="<?php echo wp_specialchars(get_mystique_option('featured_count')); ?>" />
  251.       </td>
  252.      </tr>
  253.  
  254.      <tr>
  255.       <th scope="row"><p><?php _e("Slide delay","mystique"); ?></p></th>
  256.       <td>
  257.        <input type="text" size="5" name="featured_timeout" value="<?php echo wp_specialchars(get_mystique_option('featured_timeout')); ?>" />
  258.        <?php _e("seconds","mystique"); ?>
  259.       </td>
  260.      </tr>
  261.  
  262.      <tr>
  263.       <th scope="row"><p><?php _e("Don't show posts older than","mystique"); ?></p></th>
  264.       <td>
  265.        <input type="text" size="5" name="featured_timeframe" value="<?php echo wp_specialchars(get_mystique_option('featured_timeframe')); ?>" />
  266.        <?php _e("days","mystique"); ?>
  267.       </td>
  268.      </tr>
  269.  
  270.     </table>
  271.  
  272.     <input type="hidden" name="featured_posts" value="<?php echo get_mystique_option('featured_posts'); ?>" />
  273.  
  274.   </td>
  275.  </tr>
  276.  <?php
  277. }
  278.  
  279.  
  280. add_action('mystique_admin_content','mystique_featured_admin');
  281.  
  282. // add column to post listings
  283. add_filter('manage_posts_columns', 'mystique_add_featured_posts_column');
  284. add_filter('manage_posts_custom_column', 'mystique_featured_posts_column', 10, 2);
  285.  
  286. add_filter('mystique_post_class', 'mystique_featured_post_class');
  287. add_filter('mystique_default_settings','mystique_featured_default_settings');
  288.  
  289. // add ajax processing stuff
  290. add_action('wp_ajax_featured_process', 'mystique_featured_process');
  291. add_action('admin_print_scripts', 'mystique_featured_process_js' );
  292.  
  293. add_action('mystique_before_main', 'mystique_featured_post');
  294.  
  295. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement