web-dev-qa-db-fra.com

Colonne personnalisée pour changer le statut du message via ajax

J'essaie actuellement d'impliquer une nouvelle colonne personnalisée dans l'écran de gestion des pages qui me permettra de modifier le statut (publié/en attente) des différentes pages via un simple lien à bascule.

D'après ce que j'ai recueilli, je dois utiliser $ post-> post_status, ' status ' d'une manière ou d'une autre, probablement avec jQuery ou quelque chose du genre. Mais je n'arrive pas à comprendre comment je l'associe à l'ajax. Je pensais pouvoir obtenir de l'aide pour examiner le modificateur de statut de publication par défaut dans l'écran de modification de publication, mais je me suis rendu compte que ce n'est pas basé sur l'ajax.

Voici mon essai moche d'essayer jusqu'ici:

function wp_change_page_status_link() {
    $status = get_post_status($ID);
    if($status == 'publish'){
        $status = '<span>On</span>';
        //$post->post_status, 'pending'
    }
    elseif($status == 'pending'){
        $status = '<span>Off</span>';
    }
    return $status;
}

Je serais vraiment reconnaissant si quelqu'un pouvait me diriger dans la bonne direction.

5
INT

voici:

<?php
/*
Plugin Name: ajaxed-status
Plugin URI: http://en.bainternet.info
Description: answer to : Custom column for changing post status via ajax
http://wordpress.stackexchange.com/questions/33442/custom-column-for-changing-post-status-via-ajax
Version: 1.0
Author: Bainternet
Author URI: http://en.bainternet.info
*/



if ( !class_exists('ajaxed_status')){
    class ajaxed_status {

        //constarctor
        public function __construct() {
            global $pagenow,$typenow; //&& $typenow =='page'
            if (is_admin()  && $pagenow=='edit.php'){
                add_filter('admin_footer',array($this,'insert_ajax_status_script'));
            }

            add_filter( 'manage_edit-post_columns', array($this,'add_new_columns'));
            add_action( 'manage_post_posts_custom_column', array($this, 'manage_columns'), 10, 2);

            //manage columns
            add_filter('manage_pages_columns', array($this,'add_new_columns'));
            add_action('manage_pages_custom_column', array($this, 'manage_columns'), 10, 2);

            //ajax function
            add_action('wp_ajax_change_status', array($this,'ajax_change_status'));
        }

        /*
        * the function that will actually change the post status
        $post_id - The ID of the post you'd like to change.
        $status -  The post status publish|pending|draft|private|static|object|attachment|inherit|future|trash.
        */
        public function change_post_status($post_id,$status){
            $current_post = get_post( $post_id, 'ARRAY_A' );
            $current_post['post_status'] = $status;
            wp_update_post($current_post);
        }


        /* 
         ****************************
         * manage columns functions *
         ****************************
         */

        //add new columns function 
        public function add_new_columns($columns){
            $columns['status']= __('Status');
            return $columns;
        }

        //rander columns function 
        public function manage_columns($column_name, $id) {
            global $wpdb,$post;
            if ("status" == $column_name){
                echo '<div id="psatus">';
                switch ($post->post_status) {
                    case 'publish':
                        echo '<a href="#" class="pb" change_to="pending" pid="'.$id.'">Published</a>';
                        break;
                    case 'draft':
                        echo '<a href="#" class="pb" change_to="publish" pid="'.$id.'">Draft</a>';
                        break;
                    case 'pending':
                        echo '<a href="#" class="pb" change_to="publish" pid="'.$id.'">Pending</a>';
                        break;
                    default:
                        echo 'unknown';
                        break;
                } // end switch
                echo '</div>';
            }
        }


        //js/jquery code to call ajax
        public function insert_ajax_status_script(){
            ?>
            <div id="status_update_working" style="background-color: green; color: #fff; font-wieght: bolder;   font-size: 22px;   height: 33px;   left: 40%;   padding: 35px;   position: fixed;   top: 100px;   width: 350px; display:none !important; ">Changing status...</div>
            <script type="text/javascript">

            function ajax_change_status(p){
                jQuery("#status_update_working").show('fast');
                jQuery.getJSON(ajaxurl,
                    {   post_id: p.attr("pid"),
                        action: "change_status",
                        change_to: p.attr("change_to")
                    },
                    function(data) {
                        if (data.error){
                            alert(data.error);                      
                        }else{
                            p.text(data.text);
                            p.attr("change_to",data.change_to);
                        }
                    }
                );
                jQuery("#status_update_working").hide('9500');
            }
            jQuery(document).ready(function(){
                jQuery(".pb").click(function(){
                    ajax_change_status(jQuery(this));
                });
            });
            </script>
            <?php
        }

        //ajax callback function
        public function ajax_change_status(){
            if (!isset($_GET['post_id'])){
                $re['data'] = 'something went wrong ...';
                echo json_encode($re);
                die();
            }
            if (isset($_GET['change_to'])){
                $this->change_post_status($_GET['post_id'],$_GET['change_to']);
                if ($_GET['change_to'] == "pending"){
                    $re['text'] = "Pending";
                    $re['change_to'] = "publish";
                }else{
                    $re['text'] = "Published";
                    $re['change_to'] = "pending";
                }
            }else{
                $re['data'] = 'something went wrong ...';
            }
            echo json_encode($re);
            die();
        }
    }
}

new ajaxed_status();
13
Bainternet