0

I am trying to show the array that enumerates the data in my database, with WP_List_Table, but I am experiencing this error:

Call to undefined method SP_Plugin::get_instance() 

My code:

<?php 
if(class_exists('Installations')){
    $inst_installation = new Installations();
}

if(isset($inst_installation)){
     register_activation_hook(__FILE__, array($inst_installation, 'installations_install'));
}   

class Installations {
    function installations_install(){
        global $wpdb;
        $table_site = $wpdb->prefix . "installations";

        if($wpdb->get_var("SHOW TABLES LIKE '$table_site'") != $table_site) {
            $sql = "CREATE TABLE `$table_site`(
                `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
                `nom` VARCHAR(255) NOT NULL,
                `title` VARCHAR(255) NOT NULL,
                `subtitle` VARCHAR(255) NOT NULL,
                `image_file` VARCHAR(255) NOT NULL,
                `desccription_title` VARCHAR(255) NOT NULL,
                `description_id` VARCHAR(255) NOT NULL,
                `video_url` VARCHAR(255) NULL,
                `video_image` VARCHAR(255)  NULL,
                `galerie_id` BIGINT UNSIGNED NULL
            )";
            require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
            dbDelta($sql);
        }
    }
}   

if ( !class_exists( 'WP_List_Table' ) ) {
    require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );   
}

class Installations_List extends WP_List_Table {
    public function __construct() {
        parent::__construct( [
            'singular' => __( 'Installation', 'sp' ),   //singular name of the listed records
            'plural'   => __( 'Installations', 'sp' ),  //plural name of the listed records
            'ajax'     => false                         //should this table support ajax?
        ] );
    }

    public function no_items(){                        //aucune installations dans la DB
        _e("Aucunes installations enregistrées");
    }
}

function get_installations( $per_page = 5, $page_number = 1 ) {  //classe enfant (test DB)
    global $wpdb;
    $sql = "SELECT * FROM {$wpdb->prefix}installations";

    if ( !empty( $_REQUEST['orderby'] ) ) {
        $sql .= ' ORDER BY ' . esc_sql( $_REQUEST['orderby'] );
        $sql .= ! empty( $_REQUEST['order'] ) ? ' ' . esc_sql( $_REQUEST['order'] ) : ' ASC';
    }

    $sql .= " LIMIT $per_page";
    $sql .= ' OFFSET ' . ( $page_number - 1 ) * $per_page;
    $result = $wpdb->get_results( $sql, 'ARRAY_A' );
    return $result;
}  

function delete_installation($id){ //supprimer une installation de la DB
    global $wpdb;
    $wpdb->delete(
        "{$wpdb->prefix}installations",
        ['ID' => $id],
        ['%d']
    );
}

function record_count(){            // nombre d'installations dans la DB
    global $wpdb;
    $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}installations";
    return $wpdb->get_var($sql);
}

function column_name($item){
    $delete_nonce = wp_create_nonce('sp_delete_customer');
    $title = '<strong>' . $item['nom'] . '</strong>';
    $action = [
        'delete' => sprintf('<a href="?page=%s&action=%s&installation=%s&_wpnonce=%s">Supprimer</a>')
    ];

    return $title . $this->row_actions($actions);
}

function column_default($item, $column_name){
}

function column_cb($item){
    return sprintf(
        '<input type="checkbox" name="bulk-delete[]" value="%s"/>', $item['Id']
    );
}

function get_columns(){      // Elememnts à afficher
    $columns = [
        'cb' => '<input type="checkbox"/>',
        'nom' => array('Nom', sp)
    ];
    return $columns;
}

function get_sortable_columns(){
    $sortable_colums = array(
        'nom' => array('nom', true)
    );
    return $sortable_colums;
}

function get_bulk_actions(){
    $actions = [
        'bulk-delete' => 'Delete'
    ];
    return $actions;
}

function prepare_items(){
    $this->_column_headers = $this->get_column_info();
    $this->process_bulk_action();

    $per_page     = $this->get_items_per_page( 'installations_per_page', 5 );
    $current_page = $this->get_pagenum();
    $total_items  = self::record_count();

    $this->set_pagination_args( [
        'total_items' => $total_items, //WE have to calculate the total number of items
        'per_page'    => $per_page //WE have to determine how many items to show on a page
    ] );

    $this->items = self::get_installations( $per_page, $current_page );
}

function process_bulk_action(){
    //Detect when a bulk action is being triggered...
    if ( 'delete' === $this->current_action() ) {
        $nonce = esc_attr( $_REQUEST['_wpnonce'] );

        if ( ! wp_verify_nonce( $nonce, 'sp_delete_installation' ) ) {
            die( 'Go get a life script kiddies' );
        }
        else {
            self::delete_installations( absint( $_GET['installation'] ) );

            wp_redirect( esc_url( add_query_arg() ) );
            exit;
        }
    }

    // If the delete bulk action is triggered
    if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' ) || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-delete' )) {
        $delete_ids = esc_sql( $_POST['bulk-delete'] );

        // loop over the array of record IDs and delete them
        foreach ( $delete_ids as $id ) {
            self::delete_installation( $id );
        }
        wp_redirect( esc_url( add_query_arg() ) );
        exit;
    }
}

class SP_Plugin {
    // class instance
    static $instance;

    // customer WP_List_Table object
    public $installations_obj;

    // class constructor
    public function __construct() {
        add_filter( 'set-screen-option', [ __CLASS__, 'set_screen' ], 10, 3 );
        add_action( 'admin_menu', [ $this, 'plugin_menu' ] );
    }    
}

function set_screen( $status, $option, $value ) {
    return $value;
}

function plugin_menu() {
    $hook = add_menu_page(
        'Sitepoint WP_List_Table Example',
        'SP WP_List_Table',
        'manage_options',
        'wp_list_table_class',
        [ $this, 'plugin_settings_page' ]
    );

    add_action( "load-$hook", [ $this, 'screen_option' ] );
}

function screen_option() {
    $option = 'per_page';
    $args   = [
        'label'   => 'Installations',
        'default' => 5,
        'option'  => 'installations_per_page'
    ];

    add_screen_option( $option, $args );
    $this->installations_obj = new Installationss_List();
}

function plugin_settings_page() { ?>

    <div class="wrap">
        <h2>Toutes les installations</h2>

        <div id="poststuff">
            <div id="post-body" class="metabox-holder columns-2">
                <div id="post-body-content">
                    <div class="meta-box-sortables ui-sortable">
                        <form method="post">
                            <?php
                            $this->installations_obj->prepare_items();
                            $this->installations_obj->display(); ?>
                        </form>
                    </div>
                </div>
            </div>
            <br class="clear">
        </div>
    </div> <?php
}

function get_instance() {
    if ( ! isset( self::$instance ) ) {
        self::$instance = new self();
    }
    return self::$instance;
}
add_action( 'plugins_loaded', function () {
    echo SP_Plugin::get_instance(); exit;
});
?>
2

0

Browse other questions tagged or ask your own question.