Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- register_activation_hook(__FILE__, 'create_glued_tables');
- if (is_admin()) {
- add_action('load-post.php', 'pti_glued_posts');
- add_action('load-post-new.php', 'pti_glued_posts');
- }
- add_filter('posts_results', 'pti_insert_glued_post');
- add_action('pre_get_posts', 'pti_exclude_glued_post');
- function pti_exclude_glued_post($query) {
- if (!is_admin() && $query->is_main_query()) {
- global $wpdb;
- $rows = $wpdb->get_results('SELECT `ID` FROM `'.$wpdb->prefix.'postGlued`');
- if (count($rows)) {
- $found_glued = array();
- foreach ($rows as $row) $found_glued[] = $row->ID;
- $query->set('post__not_in', $found_glued);
- }
- }
- }
- function pti_insert_glued_post($posts) {
- global $wpdb, $wp_query;
- //print_r($posts);
- if (!is_admin() && is_main_query() && !is_single() && !defined('_RUN_ONCE')) {
- define('_RUN_ONCE', TRUE);
- $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
- $rows = $wpdb->get_results('SELECT * FROM `'.$wpdb->prefix.'postGlued` WHERE `page` = '.$page);
- $found_glued = array();
- if (count($rows)) {
- foreach ($rows as $row) $found_glued[] = $row->ID;
- $p2insert = new WP_Query(array('post__in' => $found_glued, 'suppress_filters' => true));
- if ($p2insert->have_posts()) {
- while ($p2insert->have_posts()) {
- $p2insert->next_post();
- foreach ($rows as $row) {
- if ($p2insert->post->ID == $row->ID) {
- array_splice($posts, $row->position - 1, 0, array($p2insert->post));
- //print_r($p2insert->post);
- }
- }
- }
- }
- /*
- $insert_at = 3;
- if (!empty($p2insert->posts)) {
- array_splice($posts,$insert_at,0,$p2insert->posts);
- }
- */
- }
- }
- //print_r($posts);
- return $posts;
- }
- function create_glued_tables() {
- global $wpdb;
- $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`))';
- require_once(ABSPATH.'wp-admin/includes/upgrade.php');
- dbDelta($query);
- }
- function get_glued_data($post_id) {
- global $wpdb;
- if ($post = $wpdb->get_row('SELECT * FROM `'.$wpdb->prefix.'postGlued` WHERE `ID` = '.$post_id)) {
- return $post;
- } else {
- return FALSE;
- }
- }
- function set_glued_data($post_id, $data) {
- global $wpdb;
- if (!$data->page) {
- if ($wpdb->query('DELETE FROM `'.$wpdb->prefix.'postGlued` WHERE `ID` = '.$post_id)) {
- return TRUE;
- } else {
- return FALSE;
- }
- } else {
- if ($wpdb->query('REPLACE INTO `'.$wpdb->prefix.'postGlued` (`ID`, `page`, `position`) VALUES ('.$post_id.', '.$data->page.', '.$data->position.')')) {
- return TRUE;
- } else {
- return FALSE;
- }
- }
- }
- function pti_glued_posts() {
- new pti_glued_posts();
- }
- class pti_glued_posts {
- function __construct() {
- add_action('add_meta_boxes', array($this, 'add_meta_box'));
- add_action('save_post', array($this, 'save'));
- }
- function add_meta_box($post_type) {
- $post_types = array('post'); //limit meta box to certain post types
- if (in_array($post_type, $post_types)) {
- add_meta_box(
- 'pti_glued_posts'
- ,'Glued Post Options'
- ,array($this, 'render_meta_box_content')
- ,$post_type
- ,'advanced'
- ,'high'
- );
- }
- }
- function render_meta_box_content($post) {
- // Add an nonce field so we can check for it later.
- wp_nonce_field('pti_glued_inner_custom_box', 'pti_glued_inner_custom_box_nonce');
- // Use get_post_meta to retrieve an existing value from the database.
- $post_data = get_glued_data($post->ID);
- // Display the form, using the current value.
- ?>
- <label for="pti_glued_page_field">Page:</label>
- <select name="pti_glued_page_field" id="pti_glued_page_field">
- <?php
- $count_pages = ceil(wp_count_posts()->publish / get_option('posts_per_page'));
- for ($i = 0; $i <= $count_pages; $i++) {
- ?>
- <option <?php if ($post_data) selected($i, $post_data->page); ?> value="<?=$i?>"><?=$i == 0 ? 'Unset' : ($i == 1 ? 'Homepage' : $i)?></option>
- <?php
- }
- ?>
- </select>
- <label for="pti_glued_position_field">Position:</label>
- <select name="pti_glued_position_field" id="pti_glued_position_field">
- <?php
- $count_posts = get_option('posts_per_page');
- for ($i = 1; $i <= $count_posts; $i++) {
- ?>
- <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>
- <?php
- }
- ?>
- </select>
- <?php
- }
- function save($post_id) {
- // Check if our nonce is set.
- if (!isset($_POST['pti_glued_inner_custom_box_nonce'])) return $post_id;
- $nonce = $_POST['pti_glued_inner_custom_box_nonce'];
- // Verify that the nonce is valid.
- if (!wp_verify_nonce($nonce, 'pti_glued_inner_custom_box')) return $post_id;
- if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
- // Check the user's permissions.
- if ($_POST['post_type'] == 'post') {
- if (!current_user_can('edit_post', $post_id)) return $post_id;
- }
- /* OK, its safe for us to save the data now. */
- $data = new STDClass();
- $data->page = $_POST['pti_glued_page_field'];
- $data->position = $_POST['pti_glued_position_field'];
- set_glued_data($post_id, $data);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement