'widget_text', 'description' => __('Add your books here', 'author-showcase')); parent::__construct('Author_Showcase_Widget', __('Author Showcase', 'author-showcase'), $widget_ops); } public function widget( $args, $instance ) { global $wpdb; extract( $args ); $books = array(); if(isset($instance['book_ids'])) { $books = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}buybooks WHERE id IN (".esc_sql($instance['book_ids']).")", ARRAY_A); } if ($instance['other_fields'] != 'null' && $instance['other_fields'] != NULL) { $instance['other_fields'] = json_decode($instance['other_fields'], true); } else { $instance['other_fields'] = array(); } $bookHTML = ""; if(!empty($books) && $instance['randomize'] != 1) { $bookHTML = btbe_generateWidgetDisplay($books, $instance['other_fields'], $instance['book_ids'], $instance['icons_visible']); } if(!empty($books) && $instance['randomize'] == 1) { $randombookid = array_rand($books); $bookHTML = btbe_generateRandomWidgetDisplay($books[$randombookid], $instance['other_fields'], $instance['icons_visible']); } $header = apply_filters( 'widget_header', $instance['header'] ); $before_widget = '
'; $after_widget = '
'; $before_header = '

'; $after_header = '

'; echo $before_widget; if ( ! empty( $header ) ) { echo $before_header . esc_attr($header) . $after_header; } echo $bookHTML; echo $after_widget; } public function update( $new_instance, $old_instance ) { $instance = array(); $instance['header'] = esc_html(strip_tags( $new_instance['header'] )); $instance['book_ids'] = esc_html(strip_tags( $new_instance['book_ids'] )); $instance['other_fields'] = json_encode( $new_instance['other_fields'] ); $instance['randomize'] = esc_html( $new_instance['randomize'] ); $instance['icons_visible'] = esc_html( $new_instance['icons_visible'] ); return $instance; } public function form( $instance ) { global $wpdb; if ( isset( $instance[ 'header' ] ) ) { $header = $instance[ 'header' ]; } else { $header = __( 'Author Showcase', 'author-showcase' ); } $columns = array(); foreach ( $wpdb->get_col( "DESC " . $wpdb->prefix."buybooks", 0 ) as $column_name ) { if(in_array($column_name, array('id', 'services', 'cover', 'created', 'updated', 'blurb', 'book_page'))) { continue; } $columns[] = $column_name; } if ($instance['other_fields'] != 'null' && $instance['other_fields'] != NULL) { $instance['other_fields'] = json_decode($instance['other_fields'], true); } else { $instance['other_fields'] = array(); } ?>

checked/>

checked/>

checked/>

'bb_book_item', 'plural' => 'bb_book_items', 'ajax' => false ) ); } function get_columns() { return $columns= array( 'id' => __('Book ID', 'author-showcase'), 'title'=>__('Title', 'author-showcase'), 'subtitle'=>__('Subtitle', 'author-showcase'), 'author'=>__('Author', 'author-showcase'), 'series'=>__('Series', 'author-showcase'), 'cover'=>__('Cover', 'author-showcase'), 'updated' =>__('Last Update', 'author-showcase'), 'links'=>__('Sales Links', 'author-showcase'), 'book_link'=> __('Book Page', 'author-showcase') ); } public function get_sortable_columns() { return $sortable = array( 'title'=>'title', ); } function prepare_items() { global $wpdb, $_wp_column_headers; $screen = get_current_screen(); $query = "SELECT * FROM {$wpdb->prefix}buybooks"; $totalitems = $wpdb->query($query); $perpage = 10; $paged = !empty($_GET["paged"]) ? (int)$_GET["paged"] : ''; if(empty($paged) || !is_numeric($paged) || $paged<=0 ){ $paged=1; } $totalpages = ceil($totalitems/$perpage); if(!empty($paged) && !empty($perpage)){ $offset=($paged-1)*$perpage; $query.=' LIMIT '.(int)$offset.','.(int)$perpage; } $this->set_pagination_args( array( "total_items" => $totalitems, "total_pages" => $totalpages, "per_page" => $perpage, ) ); $columns = $this->get_columns(); $_wp_column_headers[$screen->id]=$columns; $this->items = $wpdb->get_results($query); } function display_rows() { $records = $this->items; $columns = $this->get_columns(); echo ''; foreach ($columns as $column_display_name) { echo ''.$column_display_name.''; } echo ''; //Loop for each record if(!empty($records)) { foreach($records as $rec) { //Open the line echo ''; foreach ( $columns as $column_name => $column_display_name ) { //Style attributes for each col $class = "class='$column_name column-$column_name'"; $style = ""; //edit link $editlink = 'admin.php?page=btbe_add&btbe_id='.(int)$rec->id; $services = json_decode($rec->services, true); $service_string = ""; if(!empty($services)) { foreach ($services as $s) { $service_string .= ''; } } if($rec->book_page != '') { $booklink = ''.__('View Book Page', 'author-showcase').''; } else { $booklink = ''; } //Display the cell switch ( $column_name ) { case "id": echo ''.stripslashes($rec->id).''; break; case "title": echo ''.stripslashes($rec->title).''; break; case "subtitle": echo ''.stripslashes($rec->subtitle).''; break; case "author": echo ''.stripslashes($rec->author).''; break; case "series": echo ''.stripslashes($rec->series).' '.stripslashes($rec->series_num).''; break; case "cover": echo ''; break; case "updated": echo ''.date("D, M jS Y H:ia", strtotime($rec->updated)).'
'.__('Edit this book', 'author-showcase').''; break; case "links": echo ''.$service_string.''; break; case "book_link": echo ''.$booklink.''; break; } } //Close the line echo''; } } } } // class Author_Showcase_Widget function register_author_showcase() { register_widget('Author_Showcase_Widget'); } function btbe_enqueuestylesandjs(){ wp_enqueue_script( 'jquery' ); wp_enqueue_style('bookstyle', plugins_url('style.css', __FILE__)); wp_enqueue_script('bookscript', plugins_url('buybook.js', __FILE__)); } function btbe_install() { global $wpdb; $table_name = $wpdb->prefix."buybooks"; $bb_params = $wpdb->prefix."bb_apis"; if($wpdb->get_var("SHOW TABLES LIKE '$table_name'" ) != $table_name){ $sql= "CREATE TABLE $table_name ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, created DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL, updated DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL, title VARCHAR(300) DEFAULT '' NOT NULL, subtitle VARCHAR(300) DEFAULT '' NOT NULL, series VARCHAR(300) DEFAULT '' NOT NULL, author VARCHAR(300) DEFAULT '' NOT NULL, cover VARCHAR(300) DEFAULT '' NOT NULL, blurb TEXT DEFAULT '' NOT NULL, asin VARCHAR(20) DEFAULT '' NOT NULL, isbn VARCHAR(20) DEFAULT '' NOT NULL, services TEXT DEFAULT '' NULL, book_page VARCHAR(20) DEFAULT '' NULL, short_blurb VARCHAR(400) DEFAULT '' NULL, series_num VARCHAR(20) DEFAULT '' NULL );"; require_once (ABSPATH. 'wp-admin/includes/upgrade.php' ); dbDelta($sql); } if($wpdb->get_var("SHOW TABLES LIKE '$bb_params'" ) != $bb_params) { $sql= "CREATE TABLE $bb_params ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, api_name VARCHAR(100) DEFAULT '' NOT NULL, assoc VARCHAR(300) DEFAULT '' NOT NULL, access_key VARCHAR(300) DEFAULT '' NOT NULL, secret VARCHAR(300) DEFAULT '' NOT NULL );"; require_once (ABSPATH. 'wp-admin/includes/upgrade.php' ); dbDelta($sql); } add_option( 'buybook_db_version', '1.4.1' ); } function btbe_admin() { add_menu_page( __('Author Showcase Book List', 'author-showcase'), __('Author Showcase Book List', 'author-showcase'), 'manage_options', 'btbe_admin', 'btbe_admin_options' ); add_submenu_page( 'btbe_admin', __('Add Book', 'author-showcase'), __('Add Book', 'author-showcase'), 'manage_options', 'btbe_add', 'btbe_add_options'); add_submenu_page( 'btbe_admin', __('Add API Keys', 'author-showcase'), __('Add API Keys', 'author-showcase'), 'manage_options', 'btbe_apis', 'btbe_apis_options'); } function btbe_admin_options() { if ( !current_user_can( 'manage_options' ) ) { wp_die( __( 'You do not have sufficient permissions to access this page.', 'author-showcase' ) ); } include(dirname(__FILE__) . "/btbe_admin.php"); } function btbe_add_options() { if ( !current_user_can( 'manage_options' ) ) { wp_die( __( 'You do not have sufficient permissions to access this page.' , 'author-showcase') ); } include(dirname(__FILE__) . "/btbe_add.php"); } function btbe_apis_options() { if ( !current_user_can( 'manage_options' ) ) { wp_die( __( 'You do not have sufficient permissions to access this page.', 'author-showcase' ) ); } include(dirname(__FILE__) . "/btbe_apis.php"); } function btbe_page_display($atts) { global $wpdb; $books = array(); if(isset($atts['books'])) { $books = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}buybooks WHERE id IN (".esc_sql($atts['books']).")", ARRAY_A); } if(isset($atts['series'])) { $series = $wpdb->get_results("SELECT DISTINCT series FROM {$wpdb->prefix}buybooks WHERE id IN (".esc_sql($atts['series']).")", ARRAY_A); $books = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}buybooks WHERE series LIKE '%".esc_sql($series['series'])."%' ORDER BY created", ARRAY_A); } if(!empty($books)) { return btbe_generatePageDisplay($books, $atts); } } function btbe_enqueuemedia($page) { wp_enqueue_media(); } function btbe_load_textdomain() { load_plugin_textdomain( 'author-showcase', false, basename( dirname( __FILE__ ) ) . '/lang/' ); } add_action('widgets_init', 'register_author_showcase'); add_action('wp_enqueue_scripts', 'btbe_enqueuestylesandjs'); add_filter('admin_enqueue_scripts', 'btbe_enqueuemedia' ); add_action( 'admin_menu', 'btbe_admin' ); add_shortcode( 'btbe_display', 'btbe_page_display' ); add_action('plugins_loaded', 'btbe_load_textdomain'); register_activation_hook( __FILE__, 'btbe_install' );