Advertisement
Artem78

Glued Posts

Mar 25th, 2015
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.19 KB | None | 0 0
  1. <?php
  2.  
  3. register_activation_hook(__FILE__, 'create_glued_tables');
  4. if (is_admin()) {
  5.     add_action('load-post.php', 'pti_glued_posts');
  6.     add_action('load-post-new.php', 'pti_glued_posts');
  7. }
  8.  
  9. add_filter('posts_results', 'pti_insert_glued_post');
  10. add_action('pre_get_posts', 'pti_exclude_glued_post');
  11.  
  12. function pti_exclude_glued_post($query) {
  13.     if (!is_admin() && $query->is_main_query()) {
  14.         global $wpdb;
  15.         $rows = $wpdb->get_results('SELECT `ID` FROM `'.$wpdb->prefix.'postGlued`');
  16.         if (count($rows)) {
  17.             $found_glued = array();
  18.             foreach ($rows as $row) $found_glued[] = $row->ID;
  19.             $query->set('post__not_in', $found_glued);
  20.         }
  21.     }
  22. }
  23.  
  24. function pti_insert_glued_post($posts) {
  25.     global $wpdb, $wp_query;
  26. //print_r($posts);
  27.     if (!is_admin() && is_main_query() && !is_single() && !defined('_RUN_ONCE')) {
  28.         define('_RUN_ONCE', TRUE);
  29.         $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
  30.         $rows = $wpdb->get_results('SELECT * FROM `'.$wpdb->prefix.'postGlued` WHERE `page` = '.$page);
  31.         $found_glued = array();
  32.  
  33.         if (count($rows)) {
  34.             foreach ($rows as $row) $found_glued[] = $row->ID;
  35.             $p2insert = new WP_Query(array('post__in' => $found_glued, 'suppress_filters' => true));
  36.  
  37.             if ($p2insert->have_posts()) {
  38.                 while ($p2insert->have_posts()) {
  39.                     $p2insert->next_post();
  40.                     foreach ($rows as $row) {
  41.                         if ($p2insert->post->ID == $row->ID) {
  42.                             array_splice($posts, $row->position - 1, 0, array($p2insert->post));
  43. //print_r($p2insert->post);
  44.                         }
  45.                     }
  46.                 }
  47.             }
  48.  
  49. /*
  50.             $insert_at = 3;
  51.             if (!empty($p2insert->posts)) {
  52.                 array_splice($posts,$insert_at,0,$p2insert->posts);
  53.             }
  54. */
  55.         }
  56.     }
  57. //print_r($posts);
  58.     return $posts;
  59. }
  60.  
  61. function create_glued_tables() {
  62.     global $wpdb;
  63.     $query = 'CREATE TABLE IF NOT EXISTS `'.$wpdb->prefix.'postGlued` (`ID` BIGINT(20) NOT NULL, `page` INT NOT NULL, `position` INT NOT NULL, primary key(`ID`), key(`page`))';
  64.     require_once(ABSPATH.'wp-admin/includes/upgrade.php');
  65.     dbDelta($query);
  66. }
  67.  
  68. function get_glued_data($post_id) {
  69.     global $wpdb;
  70.     if ($post = $wpdb->get_row('SELECT * FROM `'.$wpdb->prefix.'postGlued` WHERE `ID` = '.$post_id)) {
  71.         return $post;
  72.     } else {
  73.         return FALSE;
  74.     }
  75. }
  76.  
  77. function set_glued_data($post_id, $data) {
  78.     global $wpdb;
  79.     if (!$data->page) {
  80.         if ($wpdb->query('DELETE FROM `'.$wpdb->prefix.'postGlued` WHERE `ID` = '.$post_id)) {
  81.             return TRUE;
  82.         } else {
  83.             return FALSE;
  84.         }
  85.     } else {
  86.         if ($wpdb->query('REPLACE INTO `'.$wpdb->prefix.'postGlued` (`ID`, `page`, `position`) VALUES ('.$post_id.', '.$data->page.', '.$data->position.')')) {
  87.             return TRUE;
  88.         } else {
  89.             return FALSE;
  90.         }
  91.     }
  92. }
  93.  
  94. function pti_glued_posts() {
  95.     new pti_glued_posts();
  96. }
  97. class pti_glued_posts {
  98.     function __construct() {
  99.         add_action('add_meta_boxes', array($this, 'add_meta_box'));
  100.         add_action('save_post', array($this, 'save'));
  101.     }
  102.     function add_meta_box($post_type) {
  103.         $post_types = array('post');     //limit meta box to certain post types
  104.         if (in_array($post_type, $post_types)) {
  105.             add_meta_box(
  106.                 'pti_glued_posts'
  107.                 ,'Glued Post Options'
  108.                 ,array($this, 'render_meta_box_content')
  109.                 ,$post_type
  110.                 ,'advanced'
  111.                 ,'high'
  112.             );
  113.         }
  114.     }
  115.     function render_meta_box_content($post) {
  116.         // Add an nonce field so we can check for it later.
  117.         wp_nonce_field('pti_glued_inner_custom_box', 'pti_glued_inner_custom_box_nonce');
  118.  
  119.         // Use get_post_meta to retrieve an existing value from the database.
  120.         $post_data = get_glued_data($post->ID);
  121.  
  122.         // Display the form, using the current value.
  123. ?>
  124.     <label for="pti_glued_page_field">Page:</label>
  125.     <select name="pti_glued_page_field" id="pti_glued_page_field">
  126. <?php
  127.     $count_pages = ceil(wp_count_posts()->publish / get_option('posts_per_page'));
  128.     for ($i = 0; $i <= $count_pages; $i++) {
  129. ?>
  130.         <option <?php if ($post_data) selected($i, $post_data->page); ?> value="<?=$i?>"><?=$i == 0 ? 'Unset' : ($i == 1 ? 'Homepage' : $i)?></option>
  131. <?php
  132.     }
  133. ?>
  134.     </select>
  135.     <label for="pti_glued_position_field">Position:</label>
  136.     <select name="pti_glued_position_field" id="pti_glued_position_field">
  137. <?php
  138.     $count_posts = get_option('posts_per_page');
  139.     for ($i = 1; $i <= $count_posts; $i++) {
  140. ?>
  141.         <option <?php if ($post_data) selected($i, $post_data->position); ?> value="<?=$i?>"><?=$i == 1 ? 'First' : ($i == $count_posts ? 'Last' : ($i == round($count_posts / 2) ? 'Middle' : $i))?></option>
  142. <?php
  143.     }
  144. ?>
  145.     </select>
  146. <?php
  147.     }
  148.     function save($post_id) {
  149.         // Check if our nonce is set.
  150.         if (!isset($_POST['pti_glued_inner_custom_box_nonce'])) return $post_id;
  151.  
  152.         $nonce = $_POST['pti_glued_inner_custom_box_nonce'];
  153.  
  154.         // Verify that the nonce is valid.
  155.         if (!wp_verify_nonce($nonce, 'pti_glued_inner_custom_box')) return $post_id;
  156.  
  157.         if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
  158.  
  159.         // Check the user's permissions.
  160.         if ($_POST['post_type'] == 'post') {
  161.             if (!current_user_can('edit_post', $post_id)) return $post_id;
  162.         }
  163.  
  164.         /* OK, its safe for us to save the data now. */
  165.  
  166.         $data = new STDClass();
  167.         $data->page = $_POST['pti_glued_page_field'];
  168.         $data->position = $_POST['pti_glued_position_field'];
  169.  
  170.         set_glued_data($post_id, $data);
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement