public function debug_log($message, $title = null)
    {
        if (WP_DEBUG_LOG !== true) {
            return false;
        }
        $prefix = '[pinim] ';
        if ($title) {
            $prefix .= $title . ': ';
        }
        if (is_array($message) || is_object($message)) {
            error_log($prefix . print_r($message, true));
        } else {
            error_log($prefix . $message);
        }
    }
}
/**
 * The main function responsible for returning the one Instance
 * to functions everywhere.
 *
 * Use this function like you would a global variable, except without needing
 * to declare the global.
 *
 */
function pinim()
{
    return PinIm::instance();
}
if (is_admin()) {
    pinim();
}
 /** ************************************************************************
  * REQUIRED! This is where you prepare your data for display. This method will
  * usually be used to query the database, sort and filter the data, and generally
  * get it ready to be displayed. At a minimum, we should set $this->items and
  * $this->set_pagination_args(), although the following properties and methods
  * are frequently interacted with here...
  * 
  * @global WPDB $wpdb
  * @uses $this->_column_headers
  * @uses $this->items
  * @uses $this->get_columns()
  * @uses $this->get_sortable_columns()
  * @uses $this->get_pagenum()
  * @uses $this->set_pagination_args()
  **************************************************************************/
 function prepare_items()
 {
     global $wpdb;
     //This is used only if making any database queries
     /**
      * First, lets decide how many records per page to show
      */
     $per_page = pinim()->get_options('pins_per_page');
     /**
      * REQUIRED. Now we need to define our column headers. This includes a complete
      * array of columns to be displayed (slugs & titles), a list of columns
      * to keep hidden, and a list of columns that are sortable. Each of these
      * can be defined in another method (as we've done here) before being
      * used to build the value for our _column_headers property.
      */
     $columns = $this->get_columns();
     $hidden = array();
     $sortable = $this->get_sortable_columns();
     /**
      * REQUIRED. Finally, we build an array to be used by the class for column 
      * headers. The $this->_column_headers property takes an array which contains
      * 3 other arrays. One for all columns, one for hidden columns, and one
      * for sortable columns.
      */
     $this->_column_headers = array($columns, $hidden, $sortable);
     /**
      * Optional. You can handle your bulk actions however you see fit. In this
      * case, we'll handle them within our package just to keep things clean.
      */
     $this->process_bulk_action();
     /**
      * Instead of querying a database, we're going to fetch the example data
      * property we created for use in this plugin. This makes this example 
      * package slightly different than one you might build on your own. In 
      * this example, we'll be using array manipulation to sort and paginate 
      * our data. In a real-world implementation, you will probably want to 
      * use sort and pagination data to build a custom query instead, as you'll
      * be able to use your precisely-queried data immediately.
      */
     $data = $this->input_data;
     /**
      * This checks for sorting input and sorts the data in our array accordingly.
      * 
      * In a real-world situation involving a database, you would probably want 
      * to handle sorting by passing the 'orderby' and 'order' values directly 
      * to a custom query. The returned data will be pre-sorted, and this array
      * sorting technique would be unnecessary.
      */
     function usort_reorder($a, $b)
     {
         $orderby_default = 'date';
         $order_default = 'desc';
         $orderby = !empty($_REQUEST['orderby']) ? $_REQUEST['orderby'] : $orderby_default;
         //If no sort, default to date
         $order = !empty($_REQUEST['order']) ? $_REQUEST['order'] : $order_default;
         //If no order, default to desc
         switch ($orderby) {
             case 'title':
                 $title_a = $a->get_datas('title') ? $a->get_datas('title') : $a->pin_id;
                 $title_b = $b->get_datas('title') ? $b->get_datas('title') : $b->pin_id;
                 $result = strcmp($title_a, $title_b);
                 break;
             case 'date':
                 $result = strcmp($a->get_datas('created_at'), $b->get_datas('created_at'));
                 break;
             case 'updated':
                 $post_a = $a->get_post();
                 $post_b = $b->get_post();
                 $timestamp_a = get_post_modified_time('U', false, $post_a);
                 $timestamp_b = get_post_modified_time('U', false, $post_b);
                 $result = strcmp($timestamp_a, $timestamp_b);
                 break;
         }
         return $order === 'asc' ? $result : -$result;
         //Send final sort direction to usort
     }
     usort($data, 'usort_reorder');
     /***********************************************************************
      * ---------------------------------------------------------------------
      * vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
      * 
      * In a real-world situation, this is where you would place your query.
      *
      * For information on making queries in WordPress, see this Codex entry:
      * http://codex.wordpress.org/Class_Reference/wpdb
      * 
      * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      * ---------------------------------------------------------------------
      **********************************************************************/
     /**
      * REQUIRED for pagination. Let's figure out what page the user is currently 
      * looking at. We'll need this later, so you should always include it in 
      * your own package classes.
      */
     $current_page = $this->get_pagenum();
     /**
      * REQUIRED for pagination. Let's check how many items are in our data array. 
      * In real-world use, this would be the total number of items in your database, 
      * without filtering. We'll need this later, so you should always include it 
      * in your own package classes.
      */
     $total_items = count($data);
     /**
      * The WP_List_Table class does not handle pagination for us, so we need
      * to ensure that the data is trimmed to only the current page. We can use
      * array_slice() to 
      */
     $data = array_slice($data, ($current_page - 1) * $per_page, $per_page);
     /**
      * REQUIRED. Now we can add our *sorted* data to the items property, where 
      * it can be used by the rest of the class.
      */
     $this->items = $data;
     /**
      * REQUIRED. We also have to register our pagination options & calculations.
      */
     $this->set_pagination_args(array('total_items' => $total_items, 'per_page' => $per_page, 'total_pages' => ceil($total_items / $per_page)));
 }
function pinim_get_root_category_id()
{
    if (!($category_id = pinim()->get_options('category_root_id'))) {
        if ($root_term = pinim_get_term_id(pinim()->root_term_name, 'category')) {
            return $root_term['term_id'];
        }
    }
    return false;
}
    function importer_page()
    {
        // Set class property
        ?>
        <div class="wrap">
            <h2><?php 
        _e('Pinterest Importer', 'pinim');
        ?>
</h2>  
            <?php 
        $pins_count = count($this->existing_pin_ids);
        if ($pins_count > 1) {
            $rate_link_wp = 'https://wordpress.org/support/view/plugin-reviews/pinterest-importer?rate#postform';
            $rate_link = '<a href="' . $rate_link_wp . '" target="_blank" href=""><i class="fa fa-star"></i> ' . __('Reviewing it', 'pinim') . '</a>';
            $donate_link = '<a href="' . pinim()->donation_url . '" target="_blank" href=""><i class="fa fa-usd"></i> ' . __('make a donation', 'pinim') . '</a>';
            ?>
                    <p class="description" id="header-links">
                        <?php 
            printf(__('<i class="fa fa-pinterest-p"></i>roudly already imported %1$s pins !  Happy with it ? %2$s and %3$s would help a lot!', 'pinim'), '<strong>' . $pins_count . '</strong>', $rate_link, $donate_link);
            ?>
                    </p>
                    <?php 
        }
        ?>
            
            <?php 
        settings_errors('pinim');
        ?>
            
            <h2 class="nav-tab-wrapper">
                <?php 
        $this->importer_page_tabs($this->current_step);
        ?>
            </h2>
                    <?php 
        switch ($this->current_step) {
            case 2:
                //fetch pins
                ?>
                            <form id="pinim-form" method="post" action="">
                                <?php 
                $this->table_pins->views();
                $this->table_pins->display();
                ?>
                            </form>
                            <?php 
                break;
            case 1:
                //'boards-settings'
                ?>
                            <form id="pinim-form" method="post" action="">
                                <?php 
                $this->table_board->views();
                $this->table_board->display();
                ?>
                            </form>
                            <?php 
                break;
            default:
                //login
                ?>
                            <form id="pinim-form" method="post" action="options.php">
                                <?php 
                // This prints out all hidden setting fields
                settings_fields('pinim');
                ?>
                                <input type="hidden" name="step" value="<?php 
                echo $this->current_step;
                ?>
" />
                                <?php 
                do_settings_sections('pinim-user-auth');
                submit_button();
                ?>
                            </form>
                            <?php 
                break;
        }
        ?>
            
        </div>
        <?php 
    }