Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Quotes llama
- Plugin URI: http://wordpress.org/plugins/quotes-llama/
- Version: 0.5.1
- Description: Share the thoughts that mean the most... display your quotes in widget, page, template, gallery or post.
- Author: oooorgle
- Author URI: http://wordpress.org/plugins/quotes-llama/
- Text Domain: quotes-llama
- Domain Path: quotes-llama/lang
- License: Copyheart
- License URI: http://copyheart.org
- */
- // deny access to page except through WordPress
- defined( "ABSPATH" ) or die( "Cannot access pages directly." );
- // include our local WP_List_Table class
- // http://codex.wordpress.org/Class_Reference/WP_List_Table
- if( ! class_exists( "QuotesLlama_List_Table" ) ) {
- // http://php.net/manual/en/function.require-once.php
- require_once( "quotes-llama-list-table.php" );
- }
- class QuotesLlama {
- private $userlevel; // Permission level to administer plugin.
- private $plugin_version; // Plugin version.
- private $db_version; // Plugin database table version.
- private $table_name; // Plugin table name.
- private $active_tab; // Currently selected tab.
- private $plugin_options; // Array of plugin options.
- private $url; // The URL of the directory that contains the plugin.
- private $plugin; // The URL of the plugin.
- private $msg; // The msg for success/failure of actions.
- public function __construct() {
- global $wpdb;
- // Plugin version.
- $this->plugin_version = "0.5.1";
- // db version.
- $this->db_version = "1.0";
- // Full table name.
- $this->table_name = $wpdb->prefix . "quotes_llama";
- // Plugin optons array.
- // http://codex.wordpress.org/Function_Reference/get_option
- $this->plugin_options = get_option( "quotes-llama-settings" );
- // User level needed to manage plugin. Admin or Editor only.
- // http://codex.wordpress.org/Roles_and_Capabilities
- $this->userlevel = $this->plugin_options["permission_level"];
- $this->msg = "";
- // Process any $_POSTs.
- // http://codex.wordpress.org/Plugin_API/Action_Reference/init
- add_action( "init", array( $this, "plugin_posts" ) );
- // Set shortcode for starting plugin.
- add_action( "init", array( $this, "plugin_shortcodes" ) );
- // Non authenticated user privilege .ajax.
- // http://codex.wordpress.org/Function_Reference/add_action
- add_action( "wp_ajax_nopriv_widget_instance", array( $this, "widget_instance" ) );
- add_action( "wp_ajax_widget_instance", array( $this, "widget_instance" ) );
- add_action( "wp_ajax_nopriv_quotes_select", array( $this, "quotes_select" ) );
- add_action( "wp_ajax_quotes_select", array( $this, "quotes_select" ) );
- add_action( "wp_ajax_nopriv_template_page_author", array( $this, "template_page_author" ) );
- add_action( "wp_ajax_template_page_author", array( $this, "template_page_author" ) );
- // Plugin path.
- // http://codex.wordpress.org/Function_Reference/plugin_dir_url
- $this->url = plugin_dir_url( __FILE__ );
- // Plugin file.
- // http://codex.wordpress.org/Function_Reference/plugin_basename
- $this->plugin = plugin_basename( __FILE__ );
- // Define i18n language folder.
- add_action( "plugin_text_domain", array( $this, "plugin_text_domain" ) );
- // https://codex.wordpress.org/Function_Reference/is_admin
- if ( !is_admin() ) {
- // Page view - only load if viewing the front page.
- add_action( "wp_enqueue_scripts", array( $this, "plugin_scripts" ) );
- // init widget class
- add_action( "widgets_init", array( $this, "register_widgets" ) );
- } else {
- // Dashboard view - only load if viewing the Dashboard.
- add_action( "admin_enqueue_scripts", array( $this, "admin_scripts" ) );
- // Create plugin database table.
- register_activation_hook( __FILE__, array( $this, "plugin_db_setup" ) );
- // Create plugin options.
- // http://codex.wordpress.org/Function_Reference/register_activation_hook
- register_activation_hook( __FILE__, array( $this, "plugin_activation" ) );
- // Remove plugin options and settings when deactivating.
- // http://codex.wordpress.org/Function_Reference/register_deactivation_hook
- register_deactivation_hook( __FILE__, array( $this, "plugin_deactivation" ) );
- // Create admin manage links, css, and page fields.
- // http://codex.wordpress.org/Function_Reference/add_filter
- add_filter( "plugin_action_links_" . $this->plugin, array( $this, "plugin_manage_link" ) );
- add_filter( 'set-screen-option', array( $this, "quotes_llama_set_option" ), 10, 3 );
- add_action( "admin_menu", array( $this, "plugin_settings_link" ) );
- add_action( "admin_init", array( $this, "admin_page_fields" ) );
- add_action( "admin_head", array( $this, "plugin_css" ) );
- // init widget class
- add_action( "widgets_init", array( $this, "register_widgets" ) );
- }
- } // End __construct
- /*
- * Dashboard scripts, localizations and styles.
- */
- public function admin_scripts() {
- // http://codex.wordpress.org/Function_Reference/wp_enqueue_script
- wp_enqueue_script( "quotesllamaAjax", $this->url . "quotes-llama.js", array("jquery"), $this->plugin_version, true );
- // http://codex.wordpress.org/Function_Reference/wp_localize_script
- wp_localize_script( "quotesllamaAjax", "quotesllamaOption",
- array( "ajaxurl" => admin_url( "admin-ajax.php" )
- ));
- // Enqueues all scripts, styles, settings, and templates necessary to use all media JavaScript APIs.
- // http://codex.wordpress.org/Function_Reference/wp_enqueue_media
- wp_enqueue_media();
- }
- /*
- * Front-end scripts, localizations and styles.
- */
- public function plugin_scripts() {
- wp_enqueue_script( "quotesllamaAjax", $this->url . "quotes-llama.js", array("jquery"), $this->plugin_version, true );
- // Our php variables to pass to Javascript and the admin-ajax.php path for .ajax posts from javascript.
- // avoid boolean because of false causing indexing errors in debug.
- // http://codex.wordpress.org/Function_Reference/wp_localize_script
- wp_localize_script( "quotesllamaAjax", "quotesllamaOption",
- array( "ajaxurl" => admin_url( "admin-ajax.php" ),
- "BackgroundColor" => $this->plugin_options["background_color"],
- "ForegroundColor" => $this->plugin_options["foreground_color"],
- "GalleryInterval" => $this->plugin_options["gallery_timer_interval"],
- "GalleryMinimum" => $this->plugin_options["gallery_timer_minimum"],
- "Sidebarpos" => $this->plugin_options["sidebar"],
- "Limit" => $this->plugin_options["character_limit"],
- "Ellipses" => $this->plugin_options["ellipses_text"],
- "SourceNewLine" => $this->plugin_options["source_newline"],
- "MoreText" => $this->plugin_options["read_more_text"],
- "LessText" => $this->plugin_options["read_less_text"]
- )
- );
- // http://codex.wordpress.org/Function_Reference/wp_enqueue_style
- wp_enqueue_style( "quotesllamastyle", $this->url . "css/quotes-llama-style.css" );
- }
- /*
- * Process $_POST requests for
- * Single feeds, link submissions, and category pages.
- */
- public function plugin_posts() {
- // If we are searching the quotes table.
- // Check url for _wp_http_referer and remove it to avoid url too long error when performing multiple searches.
- if( isset( $_REQUEST["quotes_llama_s"] ) ){
- $current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
- // If it contains the referer field strip it and the nonce.
- if ( strpos( $current_url, "_wp_http_referer" ) !== false ) {
- $new_url = remove_query_arg( array( "_wp_http_referer", "_wpnonce" ), stripslashes( $current_url ));
- wp_redirect( $new_url );
- exit;
- }
- }
- // $_GET clicked tab or set initial tab.
- if ( isset( $_GET[ "tab" ] ) ) {
- $this->active_tab = isset( $_GET[ "tab" ] ) ? $_GET[ "tab" ] : "quotes";
- } else {
- $this->active_tab = "quotes";
- }
- // $_REQUEST to Export quotes to csv.
- if ( isset( $_REQUEST[ "exportcsv" ] ) ) {
- // http://codex.wordpress.org/Function_Reference/check_admin_referer
- if ( check_admin_referer( "export", "quotes-llama" ) ) {
- $export_csv = new QuotesLlamaBackup( $this->table_name, $this->plugin_options["export_delimiter"] );
- $export_csv->create_csv();
- } else {
- $this->msg = "<div class='error'><p>" . "Security Token Mismatch. Please reload the page and try again." . "</p></div>";
- }
- }
- // $_REQUEST to Export quotes to json.
- if ( isset( $_REQUEST[ "exportjson" ] ) ) {
- if ( check_admin_referer( "export", "quotes-llama" ) ) {
- $export_json = new QuotesLlamaBackup( $this->table_name, $this->plugin_options["export_delimiter"] );
- $export_json->create_json();
- } else {
- $this->msg = "<div class='error'><p>" . "Security Token Mismatch. Please reload the page and try again." . "</p></div>";
- }
- }
- // $_REQUEST to Import quotes from json.
- if ( isset( $_REQUEST[ "import" ] ) ) {
- if ( check_admin_referer( "import", "quotes-llama" ) ) {
- $import = new QuotesLlamaBackup( $this->table_name, $this->plugin_options["export_delimiter"] );
- $import->generate_import();
- } else {
- $this->msg = "<div class='error'><p>" . "Security Token Mismatch. Please reload the page and try again." . "</p></div>";
- }
- }
- // $_REQUEST to add quote or Save Quote.
- // http://php.net/manual/en/reserved.variables.request.php
- if ( isset( $_REQUEST["submit"] ) ) {
- // http://codex.wordpress.org/I18n_for_WordPress_Developers
- if ( $_REQUEST["submit"] == __( "Add Quote", "quotes-llama" ) ) {
- // http://php.net/manual/en/function.extract.php
- extract( $_REQUEST );
- // http://codex.wordpress.org/Function_Reference/check_admin_referer
- // http://codex.wordpress.org/Plugin_API/Action_Reference/admin_notices
- if ( check_admin_referer( "quotes_llama_form", "security" ) ) {
- $this->msg = $this->quotes_insert( $quote, $first_name, $last_name, $source, $img_url );
- } else {
- $this->msg = "<div class='error'><p>" . "Security Token Mismatch. Please reload the page and try again." . "</p></div>";
- }
- }
- if ( $_REQUEST["submit"] == __( "Save Quote", "quotes-llama" ) ) {
- extract( $_REQUEST );
- if ( check_admin_referer( "quotes_llama_form", "security" ) ) {
- $this->msg = $this->quotes_update( $quote_id, $quote, $first_name, $last_name, $source, $img_url );
- }
- }
- }
- // $_REQUEST to delete a single quote.
- if ( isset( $_REQUEST["action"] ) && $_REQUEST["action"] == "quotes_llama_delete_single" ) {
- // http://codex.wordpress.org/Function_Reference/wp_verify_nonce
- if ( wp_verify_nonce( $_REQUEST["_wpnonce"], "delete_edit" ) ) {
- // Run our query and set our return message.
- $this->msg = $this->quotes_delete( $_REQUEST["quote_id"] );
- // Redirect our url without the quote_id or request action.
- // http://codex.wordpress.org/Function_Reference/wp_redirect
- wp_redirect( get_bloginfo( "wpurl" ) . "/wp-admin/admin.php?page=quotes-llama" . $this->get_page_number() );
- exit;
- } else {
- $this->msg = "<div class='error'><p>" . "Security Token Mismatch. Please reload the page and try again." . "</p></div>";
- }
- }
- // $_REQUEST to delete bulk quotes. The upper select box is action.
- // The lower select box is action2. Action2 is defined in quotes-llama-list-table.php
- // where we also edited the display_tablenav( $which ) function to not give 'url too long' errors.
- if ( isset( $_REQUEST["action"], $_REQUEST["action2"] ) && ( $_REQUEST["action"] == "quotes-llama-bulk-delete" || $_REQUEST["action2"] == "quotes-llama-bulk-delete" ) ) {
- if ( check_admin_referer( "delete_bulk", "quotes-llama" ) ) {
- // Check if any boxes are marked.
- if ( isset( $_REQUEST["bulkcheck"] ) && count( $_REQUEST["bulkcheck"] ) ) {
- $this->quotes_delete_bulk( $_REQUEST["bulkcheck"] );
- // Redirect our url without the quotes to delete or the request action.
- wp_redirect( get_bloginfo( "wpurl" ) . "/wp-admin/admin.php?page=quotes-llama" . $this->get_page_number() );
- exit;
- } else {
- wp_redirect( get_bloginfo( "wpurl" ) . "/wp-admin/admin.php?page=quotes-llama" . $this->get_page_number() );
- exit;
- }
- } else {
- $this->msg = "<div class='error'><p>" . "Security Token Mismatch. Please reload the page and try again." . "</p></div>";
- }
- }
- }
- /*
- * Base shortcode.
- */
- public function plugin_shortcodes() {
- // http://codex.wordpress.org/Function_Reference/add_shortcode
- add_shortcode( "quotes-llama", array( $this, "plugin_start" ) );
- }
- /*
- * Start plugin via template or page shortcode.
- *
- */
- public function plugin_start( $atts ) {
- // http://codex.wordpress.org/Function_Reference/shortcode_atts
- $att_array = shortcode_atts( array( "mode" => "quote", "id" => 0 ), $atts );
- if ( $att_array["mode"] == "gallery" ) {
- return $this->template_gallery();
- }
- if ( $att_array["mode"] == "page" ) {
- return $this->template_page();
- }
- if ( $att_array["mode"] == "quote" && $att_array["id"] == 0 ) {
- return $this->template_post();
- }
- if ( $att_array["id"] > 0 ) {
- return $this->template_id( $atts["id"] );
- }
- }
- /*
- * Register options array when activating plugin.
- */
- public function plugin_activation() {
- $add_options = array(
- "show_page_author" => true,
- "show_page_source" => true,
- "show_page_image" => true,
- "show_gallery_author" => true,
- "show_gallery_source" => true,
- "show_gallery_image" => true,
- "gallery_timer_interval" => 12,
- "gallery_timer_minimum" => 10,
- "sidebar" => "left",
- "background_color" => "#444",
- "foreground_color" => "silver",
- "default_sort" => "quote_id",
- "default_order" => "dsc",
- "permission_level" => "create_users",
- "admin_reset" => true,
- "export_delimiter" => "|",
- "character_limit" => 0,
- "ellipses_text" => "...",
- "source_newline" => "comma",
- "read_more_text" => "»",
- "read_less_text" => "«"
- );
- // http://codex.wordpress.org/Function_Reference/add_option
- add_option( "quotes-llama-settings", $add_options );
- }
- /*
- * Remove api setting when deactivating plugin
- * and the options but, only if enabled in options.
- */
- public function plugin_deactivation() {
- if ( isset( $this->plugin_options["admin_reset"] ) ) {
- // http://codex.wordpress.org/Function_Reference/delete_option
- delete_option( "quotes-llama-settings" );
- }
- // http://codex.wordpress.org/Function_Reference/unregister_setting
- unregister_setting( "quotes-llama-settings", "quotes-llama-settings" );
- }
- /*
- * Define i18n language folder.
- */
- public function plugin_text_domain() {
- // http://codex.wordpress.org/Function_Reference/load_plugin_textdomain
- load_plugin_textdomain( "quotes-llama", false, $this->url . "lang" );
- }
- /*
- * Information about plugin from top of this file.
- *
- * @param string $s - Field name to get.
- *
- * returns string - Field text.
- */
- public function plugin_information( $i ) {
- // http://codex.wordpress.org/Function_Reference/get_plugin_data
- $data = get_plugin_data( __FILE__ );
- $info = $data[$i];
- return $info;
- }
- /*
- * Apply admin page css.
- */
- public function plugin_css() {
- $url = plugin_dir_url( __FILE__ ) . "css/quotes-llama-admin.css";
- echo "<link rel='stylesheet' type='text/css' href='$url'>";
- }
- /*
- * Setup the database table.
- */
- public function plugin_db_setup() {
- global $wpdb;
- if ( ! defined( "DB_CHARSET" ) || !( $charset = DB_CHARSET ) ) {
- $charset = "utf8";
- }
- $charset = "CHARACTER SET ".$charset;
- if ( defined( "DB_COLLATE" ) && $collate = DB_COLLATE ) {
- $collate = "COLLATE ".$collate;
- }
- // If plugin table does not exist, install it.
- if ( $wpdb->get_var( "SHOW TABLES LIKE '$this->table_name'" ) != $this->table_name ) {
- $sql = "CREATE TABLE " . $this->table_name . " (
- quote_id mediumint(9) NOT NULL AUTO_INCREMENT,
- quote TEXT NOT NULL,
- first_name VARCHAR(255),
- last_name VARCHAR(255),
- source VARCHAR(255),
- img_url VARCHAR(255),
- UNIQUE KEY quote_id (quote_id)
- ) {$charset} {$collate};";
- $results = $wpdb->query( $sql );
- }
- }
- /*
- * Admin manage plugin link, admin panel -> plugins.
- * Prepend before | deactivate | edit
- *
- * @param array $links - Array of existing panel links.
- *
- * returns array with new link added.
- */
- public function plugin_manage_link( $links ) {
- $plugin_manage_link = "<a href='options-general.php?page=quotes-llama'>Manage</a>";
- // http://php.net/manual/en/function.array-unshift.php
- array_unshift( $links, $plugin_manage_link );
- return $links;
- }
- /*
- * Admin settings link, admin panel -> settings.
- * This is where permission to manage the plugin is set as well.
- */
- public function plugin_settings_link() {
- // http://codex.wordpress.org/Roles_and_Capabilities
- // http://codex.wordpress.org/Function_Reference/add_options_page
- // http://codex.wordpress.org/Function_Reference/add_menu_page
- $hook = add_menu_page( "Quotes llama", "Quotes", $this->plugin_options["permission_level"], "quotes-llama", array( $this, "admin_page" ), 'dashicons-editor-quote', 81 );
- add_action( "load-$hook", array( $this, "quotes_llama_add_option" ) );
- }
- /*
- * Setup admin page settings, sections, and fields.
- */
- public function admin_page_fields() {
- // http://codex.wordpress.org/Function_Reference/register_setting
- register_setting( "quotes-llama-settings", "quotes-llama-settings", array( $this, "admin_sanitize" ) );
- if ( $this->active_tab == "options" ) {
- // Settings sections defined here.
- // Section post.
- // http://codex.wordpress.org/Function_Reference/add_settings_section
- add_settings_section(
- "page",
- esc_html__( "Random/Static Quotes", "quotes-llama" ),
- array( $this, "admin_section_page_callback" ),
- "quotes-llama"
- );
- // Section gallery.
- add_settings_section(
- "gallery",
- esc_html__( "Gallery Quotes", "quotes-llama" ),
- array( $this, "admin_section_gallery_callback" ),
- "quotes-llama"
- );
- // Page options.
- add_settings_section(
- "authors",
- esc_html__( "Authors Page", "quotes-llama" ),
- array( $this, "admin_section_authors_callback" ),
- "quotes-llama"
- );
- // Quote character limit options.
- add_settings_section(
- "limit",
- esc_html__( "Limit Quote Display", "quotes-llama" ),
- array( $this, "admin_section_limit_callback" ),
- "quotes-llama"
- );
- // Quotes list.
- add_settings_section(
- "quotes_tab",
- esc_html__( "Quotes List Tab", "quotes-llama" ),
- array( $this, "admin_section_quotes_tab_callback" ),
- "quotes-llama"
- );
- // Other options.
- add_settings_section(
- "other",
- esc_html__( "Other Options", "quotes-llama" ),
- array( $this, "admin_section_other_callback" ),
- "quotes-llama"
- );
- // Settings fields defined here.
- // Show random author.
- // http://codex.wordpress.org/Function_Reference/add_settings_field
- add_settings_field(
- "show_page_author",
- esc_html__( "Author", "quotes-llama" ),
- array( $this, "admin_page_author_callback" ),
- "quotes-llama",
- "page"
- );
- // Show random source.
- add_settings_field(
- "show_page_source",
- esc_html__( "Source", "quotes-llama" ),
- array( $this, "admin_page_source_callback" ),
- "quotes-llama",
- "page"
- );
- // Show random image.
- add_settings_field(
- "show_page_image",
- esc_html__( "Image", "quotes-llama" ),
- array( $this, "admin_page_image_callback" ),
- "quotes-llama",
- "page"
- );
- // Show gallery author.
- add_settings_field(
- "show_gallery_author",
- esc_html__( "Author", "quotes-llama" ),
- array( $this, "admin_gallery_author_callback" ),
- "quotes-llama",
- "gallery"
- );
- // Show gallery source.
- add_settings_field(
- "show_gallery_source",
- esc_html__( "Source", "quotes-llama" ),
- array( $this, "admin_gallery_source_callback" ),
- "quotes-llama",
- "gallery"
- );
- // Show gallery image.
- add_settings_field(
- "show_gallery_image",
- esc_html__( "Image", "quotes-llama" ),
- array( $this, "admin_gallery_image_callback" ),
- "quotes-llama",
- "gallery"
- );
- // Gallery set timer interval.
- add_settings_field(
- "gallery_timer_interval",
- esc_html__( "Timer", "quotes-llama" ),
- array( $this, "admin_gallery_timer_interval_callback" ),
- "quotes-llama",
- "gallery"
- );
- // Gallery timer minimum.
- add_settings_field(
- "gallery_timer_minimum",
- esc_html__( "Timer Minimum", "quotes-llama" ),
- array( $this, "admin_gallery_timer_minimum_callback" ),
- "quotes-llama",
- "gallery"
- );
- // Sidebar position.
- add_settings_field(
- "sidebar",
- esc_html__( "Sidebar Position", "quotes-llama" ),
- array( $this, "admin_sidebar_position_callback" ),
- "quotes-llama",
- "authors"
- );
- // Background color.
- add_settings_field(
- "background_color",
- esc_html__( "Background Color", "quotes-llama" ),
- array( $this, "admin_background_color_callback" ),
- "quotes-llama",
- "authors"
- );
- // Foreground color.
- add_settings_field(
- "foreground_color",
- esc_html__( "Foreground Color", "quotes-llama" ),
- array( $this, "admin_foreground_color_callback" ),
- "quotes-llama",
- "authors"
- );
- // Quote character limit.
- add_settings_field(
- "character_limit",
- esc_html__( "Character Limit", "quotes-llama" ),
- array( $this, "admin_character_limit_callback" ),
- "quotes-llama",
- "limit"
- );
- // Ellipses text.
- add_settings_field(
- "ellipses_text",
- esc_html__( "Ellipses Text", "quotes-llama" ),
- array( $this, "admin_ellipses_text_callback" ),
- "quotes-llama",
- "limit"
- );
- // Read more text.
- add_settings_field(
- "read_more_text",
- esc_html__( "Read More Text", "quotes-llama" ),
- array( $this, "admin_read_more_text_callback" ),
- "quotes-llama",
- "limit"
- );
- // Read less text.
- add_settings_field(
- "read_less_text",
- esc_html__( "Read Less Text", "quotes-llama" ),
- array( $this, "admin_read_less_text_callback" ),
- "quotes-llama",
- "limit"
- );
- // Default sort column.
- add_settings_field(
- "default_sort",
- esc_html__( "Default Sort Column", "quotes-llama" ),
- array( $this, "admin_orderby_callback" ),
- "quotes-llama",
- "quotes_tab"
- );
- // Default sort order.
- add_settings_field(
- "default_order",
- esc_html__( "Default Order By", "quotes-llama" ),
- array( $this, "admin_order_callback" ),
- "quotes-llama",
- "quotes_tab"
- );
- // Source on new line.
- add_settings_field(
- "source_newline",
- esc_html__( "Source Separator", "quotes-llama" ),
- array( $this, "admin_source_newline_callback" ),
- "quotes-llama",
- "other"
- );
- // Permission level.
- add_settings_field(
- "permission_level",
- esc_html__( "Manage Plugin", "quotes-llama" ),
- array( $this, "admin_permission_level_callback" ),
- "quotes-llama",
- "other"
- );
- // Reset options.
- add_settings_field(
- "admin_reset",
- esc_html__( "Reset When Deactivating", "quotes-llama" ),
- array( $this, "admin_reset_callback" ),
- "quotes-llama",
- "other"
- );
- // CSV delimiter.
- add_settings_field(
- "export_delimiter",
- esc_html__( "CSV Delimiter", "quotes-llama" ),
- array( $this, "admin_export_delimiter_callback" ),
- "quotes-llama",
- "other"
- );
- // CSV delimiter.
- add_settings_field(
- "widget_page",
- esc_html__( "Widgets", "quotes-llama" ),
- array( $this, "admin_widget_page_callback" ),
- "quotes-llama",
- "other"
- );
- }
- }
- /*
- * Render tabs in admin page.
- */
- public function admin_page() {
- // First we need to check that the user has permisson to view the admin page.
- // http://codex.wordpress.org/Function_Reference/current_user_can
- if ( current_user_can( $this->plugin_options["permission_level"] ) ) {
- // Display our action msg if we have completed an action.
- echo $this->msg;
- echo "<div class='wrap'>";
- echo "<h2>" . $this->plugin_information( "Name" ) . "</h2>";
- echo "<h3>" . $this->plugin_information( "Description" ) . "</h3>";
- $this->admin_tabs();
- $this->admin_tab_quotes();
- $this->admin_tab_options();
- $this->admin_tab_add();
- $this->admin_tab_export_import();
- $this->admin_tab_short_codes();
- echo "</div>";
- } else {
- echo "<div class='error'>" . esc_html__("You do not have sufficient permissions to access this page.", "quotes-llama") . "</div>";
- }
- }
- /*
- * Admin tabs.
- */
- public function admin_tabs() {
- // This is where we build our admin tabs. ?>
- <h2 class="nav-tab-wrapper">
- <a href="?page=quotes-llama&tab=quotes"
- class="nav-tab <?php echo $this->active_tab == 'quotes' ? 'nav-tab-active' : ''; ?>">
- <?php // http://codex.wordpress.org/Function_Reference/esc_html_e
- esc_html_e( "Quotes List", "quotes-llama" ); ?>
- </a>
- <a href="?page=quotes-llama&tab=add"
- class="nav-tab <?php echo $this->active_tab == 'add' ? 'nav-tab-active' : ''; ?>">
- <?php esc_html_e( "New Quote", "quotes-llama" ); ?>
- </a>
- <a href="?page=quotes-llama&tab=options"
- class="nav-tab <?php echo $this->active_tab == 'options' ? 'nav-tab-active' : ''; ?>">
- <?php esc_html_e( "Options", "quotes-llama" ); ?>
- </a>
- <a href="?page=quotes-llama&tab=export"
- class="nav-tab <?php echo $this->active_tab == 'export' ? 'nav-tab-active' : ''; ?>">
- <?php esc_html_e( "Import/Export", "quotes-llama" ); ?>
- </a>
- <a href="?page=quotes-llama&tab=short_codes"
- class="nav-tab <?php echo $this->active_tab == 'short_codes' ? 'nav-tab-active' : ''; ?>">
- <?php esc_html_e( "Shortcode", "quotes-llama" ); ?>
- </a>
- </h2> <?php
- }
- /*
- * Quotes list tab.
- */
- public function admin_tab_quotes() {
- // Tab - Quotes list.
- if ( $this->active_tab == "quotes" ) {
- // Render page. ?>
- <div class="wrap"> <?php
- // $_REQUEST to get a quote for editing.
- // Placed here so edit form will render instead of quotes list.
- if ( isset( $_REQUEST["action"] ) && $_REQUEST["action"] == "quotes_llama_edit" ) {
- if ( wp_verify_nonce( $_REQUEST["_wpnonce"], "delete_edit" ) ) { ?>
- <div class="wrap">
- <h2><?php esc_html_e( "Edit Quote", "quotes-llama" ); ?></h2>
- <?php echo $this->quotes_form( $_REQUEST["quote_id"], $this->get_page_number() ); ?>
- </div><?php
- } else {
- echo "<div class='error'><p>" . "Security Token Mismatch. Please reload the page and try again." . "</p></div>";
- }
- return;
- }
- // Create an instance of our quotes table list class.
- $quotes_table = new QuotesLlamaTable();
- // If we are searching the quotes table.
- if( isset( $_REQUEST["quotes_llama_s"] ) ){
- $quotes_table->prepare_items( $_REQUEST["quotes_llama_s"], 20 );
- } else {
- // Or get all quotes.
- $quotes_table->prepare_items();
- }
- // Form that contains the quotes table. ?>
- <form id="quotes-filter" method="get" class="quotes-llama-admin-form">
- <!-- What column to search -->
- <select name="search_column" style="float: right;">
- <option value="quote"<?php if ( isset( $_REQUEST["search_column"] ) && $_REQUEST["search_column"] == "quote" ) { echo " selected"; } ?>>
- <?php esc_html_e( "Quote", "quotes-llama" ); ?>
- </option>
- <option value="first_name"<?php if ( isset( $_REQUEST["search_column"] ) && $_REQUEST["search_column"] == "first_name" ) { echo " selected"; }; ?>>
- <?php esc_html_e( "First Name", "quotes-llama" ); ?>
- </option>
- <option value="last_name"<?php if ( isset( $_REQUEST["search_column"] ) && $_REQUEST["search_column"] == "last_name" ) { echo " selected"; }; ?>>
- <?php esc_html_e( "Last Name", "quotes-llama" ); ?>
- </option>
- <option value="source"<?php if ( isset( $_REQUEST["search_column"] ) && $_REQUEST["search_column"] == "source" ) { echo " selected"; }; ?>>
- <?php esc_html_e( "Source", "quotes-llama" ); ?>
- </option>
- <option value="img_url"<?php if ( isset( $_REQUEST["search_column"] ) && $_REQUEST["search_column"] == "img_url" ) { echo " selected"; }; ?>>
- <?php esc_html_e( "Image URL", "quotes-llama" ); ?>
- </option>
- </select> <?php
- wp_nonce_field( "delete_bulk", "quotes-llama" );
- // Wordpress generated search form fields.
- // http://developer.wordpress.org/reference/classes/wp_list_table/search_box/
- $quotes_table->search_box( esc_html__( "Search", "quotes-llama" ), "quotes-llama-admin-search"); ?>
- <input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>"> <?php
- // Render table
- $quotes_table->display() ?>
- </form>
- </div><?php
- }
- }
- /*
- * Add new quote tab.
- */
- public function admin_tab_add() {
- // Tab - New quote.
- if ( $this->active_tab == "add" ) { ?>
- <div id="addnew" class="wrap quotes-llama-admin-form">
- <h2>
- <?php esc_html_e( "New Quote", "quotes-llama" ); ?>
- </h2>
- <?php echo $this->quotes_form( 0, "" ); ?>
- </div> <?php
- }
- }
- /*
- * Options tab.
- */
- public function admin_tab_options() {
- // Tab - options view.
- if ( $this->active_tab == "options" ) {
- // Save settings form and button. ?>
- <form action="options.php" method="POST" class="quotes-llama-admin-form">
- <!-- http://codex.wordpress.org/Function_Reference/settings_fields -->
- <?php settings_fields( "quotes-llama-settings" ); ?>
- <!-- http://codex.wordpress.org/Function_Reference/do_settings_sections -->
- <?php do_settings_sections( "quotes-llama" );
- "<li>" . esc_html__( "Widget options are set in the", "quotes-llama" ) . " " .
- "<a href='" . get_bloginfo( 'wpurl' ) . "/wp-admin/widgets.php'>" .
- esc_html__( "widgets screen.", "quotes-llama" ) .
- "</a></li>";
- // http://codex.wordpress.org/Function_Reference/submit_button
- //submit_button( $text, $type, $name, $wrap, $other_attributes )
- submit_button( esc_html__( "Save Options", "quotes-llama" ) ); ?>
- </form> <?php
- }
- }
- /*
- * Export/Import tab.
- */
- public function admin_tab_export_import() {
- if ( $this->active_tab == "export" ) { ?>
- <div class="quotes-llama-inline">
- <form method="post" action="<?php echo get_bloginfo( 'wpurl' ); ?>/wp-admin/admin.php?page=quotes-llama"> <?php
- echo "<h2>" . esc_html__( "Export" ) . "</h2>";
- echo "<p>" . esc_html__( "Backup your quotes to either .csv or .json formats.", "quotes-llama" ) . "</p>";
- wp_nonce_field( "export", "quotes-llama" );
- submit_button( esc_html__( "Export .csv", "quotes-llama" ), "large", "exportcsv", false, array( "exportcsv" => "quotes" ) );
- echo " ";
- submit_button( esc_html__( "Export .json", "quotes-llama" ), "large", "exportjson", false, array( "exportjson" => "quotes" ) );
- echo "<p>" . esc_html__( "The .csv delimiter can be set in the options tab.", "quotes-llama" ) . "</p>"; ?>
- </form>
- <form name="" method="post" action="<?php echo get_bloginfo( 'wpurl' ); ?>/wp-admin/options-general.php?page=quotes-llama" enctype="multipart/form-data"> <?php
- // http://codex.wordpress.org/Function_Reference/wp_nonce_field
- wp_nonce_field( "import", "quotes-llama" );
- echo "<h2>" . esc_html__( "Import" ) . "</h2>";
- echo "<p>" . esc_html__( "Restore your quotes from either .csv or .json formats. Browse for a file, then select the import button." ) . "</p>"; ?>
- <input type="file" name="quotes-llama-file"> <?php
- submit_button( esc_html__( "Import", "quotes-llama" ), "secondary", "import", true, array( "import" => "quotes" ) ); ?>
- </form>
- </div> <?php
- }
- }
- /*
- * Shortcodes tab.
- */
- public function admin_tab_short_codes() {
- if ( $this->active_tab == "short_codes" ) { ?>
- <div>
- <p>
- <?php esc_html_e( "To include this plugin in a page or post:", "quotes-llama" ); ?>
- <br><b><code>[quotes-llama mode='gallery']</code></b>
- <?php esc_html_e( "Gallery of quotes.", "quotes-llama" ); ?>
- <br><b><code>[quotes-llama mode='page']</code></b>
- <?php esc_html_e( "Authors page.", "quotes-llama" ); ?>
- <br><b><code>[quotes-llama]</code></b>
- <?php esc_html_e( "Random quote.", "quotes-llama" ); ?>
- <br><b><code>[quotes-llama id='id#']</code></b>
- <?php esc_html_e( "Static quote.", "quotes-llama" ); ?>
- </p>
- <p>
- <?php esc_html_e( "To include this plugin in a template file:", "quotes-llama" ); ?>
- <br><b><code>do_shortcode( "[quotes-llama mode='gallery']" );</code></b>
- <?php esc_html_e( "Gallery of quotes.", "quotes-llama" ); ?>
- <br><b><code>do_shortcode( "[quotes-llama mode='page']" );</code></b>
- <?php esc_html_e( "Authors page.", "quotes-llama" ); ?>
- <br><b><code>do_shortcode( "[quotes-llama]" );</code></b>
- <?php esc_html_e( "Random quote.", "quotes-llama" ); ?>
- <br><b><code>do_shortcode( "[quotes-llama id='id#']" );</code></b>
- <?php esc_html_e( "Static quote.", "quotes-llama" ); ?>
- </p>
- <div class="quotes-llama-admin-div">
- <hr class="quotes-llama-admin-hr">
- <?php esc_html_e( "Plugin Website:", "quotes-llama" ); ?>
- <a href="<?php echo $this->plugin_information( "PluginURI" ); ?>"
- target="_blank"
- title="<?php echo $this->strip_trim_esc_attr( $this->plugin_information( 'Name' ) ); ?>">
- <?php echo $this->strip_trim_wp_kses_post( $this->plugin_information( "Name" ) ); ?>
- </a>
- <br>
- <?php esc_html_e( "Authors Website:", "quotes-llama" ); ?>
- <a href="http://oooorgle.com/"
- target="_blank"
- title="<?php esc_attr_e( 'oooorgle.com', 'quotes-llama' ); ?>">
- <?php esc_html_e( "oooorgle", "quotes-llama" ); ?>
- </a>
- </div>
- <div class="quotes-llama-admin-div">
- <?php esc_html_e( "License:", "quotes-llama" ); ?>
- <a href="http://copyheart.org"
- target="_blank"
- title="<?php esc_attr_e( 'CoyHeart', 'quotes-llama' ); ?>">
- <?php esc_html_e( "Copyheart", "quotes-llama" ); ?>
- </a>
- <p>
- <?php echo $this->strip_trim_wp_kses_post( "Running version " . $this->plugin_information( "Version" ) ) . "<br>"; ?>
- <?php esc_html_e( "If you like this plugin: share it with someone!", "quotes-llama" );
- echo "<br> ";
- esc_html_e( "Perhaps consider ", "quotes-llama" ); ?>
- <a href="https://wordpress.org/support/view/plugin-reviews/quotes-llama"
- target="_blank"
- title="<?php esc_attr_e( 'Write a review to help promote this plugin', 'quotes-llama' ); ?>">
- <?php esc_html_e( "writing a review", "quotes-llama" ); ?>
- </a>
- <?php esc_html_e( " and rating the plugin to help promote it further in the WordPress community.", "quotes-llama" );
- echo "<br> ";
- esc_html_e( "And if that hasn't worn you out, you can always show your appreciation and support further development by making a small ", "quotes-llama" ); ?>
- <a href="http://oooorgle.com/plugins/wp/quotes-llama/"
- target="_blank"
- title="<?php esc_attr_e( 'Donations are appreciated!', 'quotes-llama' ); ?>">
- <?php esc_html_e( "donation.", "quotes-llama" ); ?>
- </a>
- </p>
- </div>
- </div> <?php
- }
- }
- /*
- * Section post.
- */
- public function admin_section_page_callback() {
- esc_html_e( "When using", "quotes-llama" );
- echo " <code>[quotes-llama]</code> or <code>[quotes-llama id='id#']</code> ";
- esc_html_e( "in posts, pages, and templates.", "quotes-llama" );
- }
- /*
- * Section gallery.
- */
- public function admin_section_gallery_callback() {
- esc_html_e( "When using", "quotes-llama" );
- echo " <code>[quotes-llama mode='gallery']</code> ";
- esc_html_e( "to display the gallery.", "quotes-llama" );
- }
- /*
- * Section page.
- */
- public function admin_section_authors_callback() {
- esc_html_e( "When using", "quotes-llama" );
- echo " <code>[quotes-llama mode='page']</code> ";
- esc_html_e( "to display the Authors page.", "quotes-llama" );
- }
- /*
- * Section page.
- */
- public function admin_section_limit_callback() {
- esc_html_e( "Display a limited # of characters for each quote.", "quotes-llama" );
- }
- /*
- * Section quotes tab.
- */
- public function admin_section_quotes_tab_callback() {
- esc_html_e( "Options for the Quotes List tab." );
- }
- /*
- * Section other options.
- */
- public function admin_section_other_callback() {
- esc_html__( "All other options." );
- }
- /*
- * Show post author checkbox.
- */
- public function admin_page_author_callback() { ?>
- <input type="checkbox"
- id="show_page_author"
- name="quotes-llama-settings[show_page_author]"
- style="display: inline;"
- <?php if ( $this->check_plugin_option( "show_page_author" ) ) echo "checked"; ?>>
- <label for="show_page_author">
- <?php esc_html_e( "Display author in single quotes.", "quotes-llama" ); ?>
- </label><?php
- }
- /*
- * Show post source checkbox.
- */
- public function admin_page_source_callback() { ?>
- <input type="checkbox"
- id="show_page_source"
- name="quotes-llama-settings[show_page_source]"
- style="display: inline;"
- <?php if ( $this->check_plugin_option( "show_page_source" ) ) echo "checked" ; ?>>
- <label for="show_page_source">
- <?php esc_html_e( "Display source in single quotes.", "quotes-llama" ); ?>
- </label><?php
- }
- /*
- * Show post image checkbox.
- */
- public function admin_page_image_callback() { ?>
- <input type="checkbox"
- id="show_page_image"
- name="quotes-llama-settings[show_page_image]"
- style="display: inline;"
- <?php if ( $this->check_plugin_option( "show_page_image" ) ) echo "checked" ; ?>>
- <label for="show_page_image">
- <?php esc_html_e( "Display image in single quotes.", "quotes-llama" ); ?>
- </label><?php
- }
- /*
- * Show gallery author checkbox.
- */
- public function admin_gallery_author_callback() { ?>
- <input type="checkbox"
- id="show_gallery_author"
- name="quotes-llama-settings[show_gallery_author]"
- style="display: inline;"
- <?php if ( $this->check_plugin_option( "show_gallery_author" ) ) echo "checked"; ?>>
- <label for="show_gallery_author">
- <?php esc_html_e( "Display authors in the gallery.", "quotes-llama" ); ?>
- </label><?php
- }
- /*
- * Show gallery source checkbox.
- */
- public function admin_gallery_source_callback() { ?>
- <input type="checkbox"
- id="show_gallery_source"
- name="quotes-llama-settings[show_gallery_source]"
- style="display: inline;"
- <?php if ( $this->check_plugin_option( "show_gallery_source" ) ) echo "checked" ; ?>>
- <label for="show_gallery_source">
- <?php esc_html_e( "Display sources in the gallery.", "quotes-llama" ); ?>
- </label><?php
- }
- /*
- * Gallery refresh interval adjuster.
- * This value is used in the JS function quotes_llama_quote()
- */
- public function admin_gallery_timer_interval_callback() {
- $gallery_timer_interval = $this->plugin_options["gallery_timer_interval"]; ?>
- <select name="quotes-llama-settings[gallery_timer_interval]"
- id="gallery_timer_interval">
- <option value="18"<?php if ( "18" == $gallery_timer_interval ) { echo " selected"; } ?>>
- <?php esc_html_e( "Fastest", "quotes-llama" ); ?>
- </option>
- <option value="16"<?php if ( "16" == $gallery_timer_interval ) { echo " selected"; } ?>>
- <?php esc_html_e( "Faster", "quotes-llama" ); ?>
- </option>
- <option value="14"<?php if ( "14" == $gallery_timer_interval ) { echo " selected"; } ?>>
- <?php esc_html_e( "Fast", "quotes-llama" ); ?>
- </option>
- <option value="12"<?php if ( "12" == $gallery_timer_interval ) { echo " selected"; } ?>>
- <?php esc_html_e( "Normal", "quotes-llama" ); ?>
- </option>
- <option value="10"<?php if ( "10" == $gallery_timer_interval ) { echo " selected"; } ?>>
- <?php esc_html_e( "Slow", "quotes-llama" ); ?>
- </option>
- <option value="8"<?php if ( "8" == $gallery_timer_interval ) { echo " selected"; } ?>>
- <?php esc_html_e( "Slower", "quotes-llama" ); ?>
- </option>
- <option value="6"<?php if ( "6" == $gallery_timer_interval ) { echo " selected"; } ?>>
- <?php esc_html_e( "Slowest", "quotes-llama" ); ?>
- </option>
- </select>
- <label for="gallery_timer_interval">
- <?php echo " " . esc_html__( "Gallery speed.", "quotes-llama" ); ?>
- </label><?php
- }
- /*
- * Gallery timer minimum time.
- * This value is used in the JS function quotes_llama_quote()
- */
- public function admin_gallery_timer_minimum_callback() { ?>
- <input type="text"
- id="gallery_timer_minimum"
- name="quotes-llama-settings[gallery_timer_minimum]"
- value="<?php echo $this->plugin_options['gallery_timer_minimum']; ?>"
- size="5">
- <label for="gallery_timer_minimum">
- <?php esc_html_e( "Display quotes for at least this many seconds.", "quotes-llama" ); ?>
- </label><?php
- }
- /*
- * Show gallery image checkbox.
- */
- public function admin_gallery_image_callback() { ?>
- <input type="checkbox"
- id="show_gallery_image"
- name="quotes-llama-settings[show_gallery_image]"
- style="display: inline;"
- <?php if ( $this->check_plugin_option( "show_gallery_image" ) ) echo "checked" ; ?>>
- <label for="show_gallery_image">
- <?php esc_html_e( "Display images in the gallery.", "quotes-llama" ); ?>
- </label><?php
- }
- /*
- * Sidebar position textfield.
- */
- public function admin_sidebar_position_callback() {
- $sidebar = $this->plugin_options["sidebar"]; ?>
- <select name="quotes-llama-settings[sidebar]"
- id="sidebar">
- <option value="left"<?php if ( "left" == $sidebar ) { echo " selected"; } ?>>
- <?php esc_html_e( "Left", "quotes-llama" ); ?>
- </option>
- <option value="right"<?php if ( "right" == $sidebar ) { echo " selected"; } ?>>
- <?php esc_html_e( "Right", "quotes-llama" ); ?>
- </option>
- </select>
- <label for="sidebar">
- <?php echo " " . esc_html__( "Align the sidebar.", "quotes-llama" ); ?>
- </label><?php
- }
- /*
- * Default orderby droplist.
- */
- public function admin_orderby_callback() {
- $default_sort = $this->plugin_options["default_sort"]; ?>
- <select name="quotes-llama-settings[default_sort]"
- id="default_sort">
- <option value="quote_id"<?php if ( "quote_id" == $default_sort ) { echo " selected"; } ?>>
- <?php esc_html_e( "ID", "quotes-llama" ); ?>
- </option>
- <option value="quote"<?php if ( "quote" == $default_sort ) { echo " selected"; } ?>>
- <?php esc_html_e( "Quote", "quotes-llama" ); ?>
- </option>
- <option value="last_name"<?php if ( "last_name" == $default_sort ) { echo " selected"; } ?>>
- <?php esc_html_e( "Author", "quotes-llama" ); ?>
- </option>
- <option value="source"<?php if ( "source" == $default_sort ) { echo " selected"; } ?>>
- <?php esc_html_e( "Source", "quotes-llama" ); ?>
- </option>
- <option value="img_url"<?php if ( "img_url" == $default_sort ) { echo " selected"; } ?>>
- <?php esc_html_e( "Image", "quotes-llama" ); ?>
- </option>
- </select>
- <label for="default_sort">
- <?php echo " " . esc_html__( "Sort by column.", "quotes-llama" ); ?>
- </label><?php
- }
- /*
- * Default order droplist.
- */
- public function admin_order_callback() {
- $default_order = $this->plugin_options["default_order"]; ?>
- <select name="quotes-llama-settings[default_order]"
- id="default_order">
- <option value="asc"<?php if ( "asc" == $default_order ) { echo " selected"; } ?>>
- <?php esc_html_e( "Asc", "quotes-llama" ); ?>
- </option>
- <option value="dsc"<?php if ( "dsc" == $default_order ) { echo " selected"; } ?>>
- <?php esc_html_e( "Dsc", "quotes-llama" ); ?>
- </option>
- </select>
- <label for="default_order">
- <?php echo " " . esc_html__( "Ascending/Descending.", "quotes-llama" ); ?>
- </label><?php
- }
- /*
- * Background color textfield.
- */
- public function admin_background_color_callback() { ?>
- <input type="text"
- id="background_color"
- name="quotes-llama-settings[background_color]"
- value="<?php echo $this->plugin_options['background_color']; ?>"
- size="5">
- <label for="background_color">
- <?php esc_html_e( "Sets the background color for the quotes page index.", "quotes-llama" ); ?>
- </label><?php
- }
- /*
- * Foreground color textfield.
- */
- public function admin_foreground_color_callback() { ?>
- <input type="text"
- id="foreground_color"
- name="quotes-llama-settings[foreground_color]"
- value="<?php echo $this->plugin_options['foreground_color']; ?>"
- size="5">
- <label for="foreground_color">
- <?php esc_html_e( "Sets the foreground color for the quotes page index.", "quotes-llama" ); ?>
- </label><?php
- }
- /*
- * Character limit for quotes display.
- */
- public function admin_character_limit_callback() { ?>
- <input type="text"
- id="character_limit"
- name="quotes-llama-settings[character_limit]"
- value="<?php echo $this->plugin_options['character_limit']; ?>"
- size="5">
- <label for="character_limit">
- <?php esc_html_e( "Limit quotes to # of characters. ( 0 = disable limit )", "quotes-llama" ); ?>
- </label><?php
- }
- /*
- * Ellipses text to display at end of character limit.
- */
- public function admin_ellipses_text_callback() { ?>
- <input type="text"
- id="ellipses_text"
- name="quotes-llama-settings[ellipses_text]"
- value="<?php echo $this->plugin_options['ellipses_text']; ?>"
- size="5">
- <label for="ellipses_text">
- <?php esc_html_e( "Text that ends the quote limit.", "quotes-llama" ); ?>
- </label><?php
- }
- /*
- * Read more text to display at end of limited quote.
- */
- public function admin_read_more_text_callback() { ?>
- <input type="text"
- id="read_more_text"
- name="quotes-llama-settings[read_more_text]"
- value="<?php echo $this->plugin_options['read_more_text']; ?>"
- size="5">
- <label for="read_more_text">
- <?php esc_html_e( "The text to expand the quote.", "quotes-llama" ); ?>
- </label><?php
- }
- /*
- * Read less text to display at end of limited quote.
- */
- public function admin_read_less_text_callback() { ?>
- <input type="text"
- id="read_less_text"
- name="quotes-llama-settings[read_less_text]"
- value="<?php echo $this->plugin_options['read_less_text']; ?>"
- size="5">
- <label for="read_less_text">
- <?php esc_html_e( "The text to collapse the quote.", "quotes-llama" ); ?>
- </label><?php
- }
- /*
- * Show quote source on a new line instead of comma sepration drop list.
- */
- public function admin_source_newline_callback() {
- $source_newline = $this->plugin_options["source_newline"]; ?>
- <select name="quotes-llama-settings[source_newline]"
- id="source_newline">
- <option value="comma"<?php if ( "comma" == $source_newline ) { echo " selected"; } ?>>
- <?php esc_html_e( "Comma [,]", "quotes-llama" ); ?>
- </option>
- <option value="br"<?php if ( "br" == $source_newline ) { echo " selected"; } ?>>
- <?php esc_html_e( "New Line [<br>]", "quotes-llama" ); ?>
- </option>
- </select>
- <label for="source_newline">
- <?php esc_html_e( "Separate the author from the source with either a comma or new line.", "quotes-llama" ); ?>
- </label><?php
- }
- /*
- * Permission level required to manage plugin
- * Administrator or editor only.
- * http://codex.wordpress.org/Roles_and_Capabilities
- */
- public function admin_permission_level_callback() {
- $permission_level = $this->plugin_options["permission_level"]; ?>
- <select name="quotes-llama-settings[permission_level]"
- id="permission_level">
- <option value="create_users"<?php if ( "create_users" == $permission_level ) { echo " selected"; } ?>>
- <?php esc_html_e( "Administrators", "quotes-llama" ); ?>
- </option>
- <option value="edit_pages"<?php if ( "edit_pages" == $permission_level ) { echo " selected"; } ?>>
- <?php esc_html_e( "Editors", "quotes-llama" ); ?>
- </option>
- </select>
- <label for="permission_level">
- <?php echo " " . esc_html__( "Set the role which has permission to manage this plugin.", "quotes-llama" ); ?>
- </label><?php
- }
- /*
- * Reset options checkbox in admin options.
- */
- public function admin_reset_callback() { ?>
- <input type="checkbox"
- id="admin_reset"
- name="quotes-llama-settings[admin_reset]"
- style="display: inline;"
- <?php if( isset( $this->plugin_options["admin_reset"] ) && $this->plugin_options["admin_reset"] == "on" ) echo "checked" ; ?>>
- <label for="admin_reset">
- <?php esc_html_e( "Reset plugin options to their defaults when deactivating this plugin.", "quotes-llama" ); ?>
- </label><?php
- }
- /*
- * CSV Export Delimiter.
- */
- public function admin_export_delimiter_callback() { ?>
- <input type="text"
- id="export_delimiter"
- name="quotes-llama-settings[export_delimiter]"
- style="display: inline;"
- value="<?php echo $this->check_plugin_option( "export_delimiter" ); ?>"
- size="3">
- <label for="export_delimiter">
- <?php esc_html_e( ".csv delimiter.", "quotes-llama" );
- echo "<cite> " . esc_html__( "Field separator for importing and exporting quotes in .csv format.", "quotes-llama" ) . "</cite>"; ?>
- </label><?php
- }
- /*
- * widget page link.
- */
- public function admin_widget_page_callback() {
- esc_html_e( "Widget options are set in the", "quotes-llama" );
- echo " <a href='" . get_bloginfo("wpurl") . "/wp-admin/widgets.php'>";
- esc_html_e( "widgets page", "quotes-llama" );
- echo "</a>.";
- }
- /*
- * Admin form sanitization.
- *
- * @param Array $input - Array of options to sanitize.
- *
- * @return Array - Sanitized array of options.
- */
- public function admin_sanitize( $input ) {
- if ( ! $input ) return;
- // Initialize the new array that will hold the sanitize values
- $new_input = array();
- // Loop through the input and sanitize each of the values
- // http://codex.wordpress.org/Function_Reference/sanitize_text_field
- foreach ( $input as $key => $val ) {
- $new_input[ $key ] = ( isset( $input[ $key ] ) ) ?
- sanitize_text_field( $val ) : "";
- }
- return $new_input;
- }
- /*
- * Add or edit a quote
- *
- * @param int $quote_id - id of quote to edit.
- *
- * heredocs http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
- * http://codex.wordpress.org/Function_Reference/wp_nonce_field
- */
- public function quotes_form( $quote_id = 0, $return_page = "" ) {
- // Default submit value is empty fields to add new quote.
- $submit_value = __( "Add Quote", "quotes-llama" );
- // Default form name.
- $form_name = "addquote";
- // Default action url.
- $action_url = get_bloginfo("wpurl")."/wp-admin/admin.php?page=quotes-llama#addnew";
- $quote = $first_name = $last_name = $source = $img_url = $hidden_input = $back = "";
- // If id, then editing a quote, set form name to edit.
- if ( $quote_id ) {
- $form_name = "editquote";
- $quote_data = $this->quotes_select( $quote_id );
- foreach ( $quote_data as $index => $value ) {
- $quote_data[$index] = $quote_data[$index];
- }
- $hidden_input = "<input type=\"hidden\" name=\"quote_id\" value=\"{$quote_id}\" />";
- $submit_value = esc_html__( "Save Quote", "quotes-llama" );
- $back = "<input type='submit' name='submit' value='" . esc_html__( 'Back', 'quotes-llama' ) . "'> ";
- $action_url = get_bloginfo( "wpurl" )."/wp-admin/admin.php?page=quotes-llama" . $return_page;
- // Set each table field from $quote_data.
- $quote = $this->strip_trim_wp_kses_post( $quote_data['quote'] );
- $first_name = $this->strip_trim_wp_kses_post( $quote_data['first_name'] );
- $last_name = $this->strip_trim_wp_kses_post( $quote_data['last_name'] );
- $source = $this->strip_trim_wp_kses_post( $quote_data['source'] );
- $img_url = $this->strip_trim_wp_kses_post( $quote_data['img_url'] );
- }
- $quote_label = esc_html__( "Quote", "quotes-llama" );
- $first_label = esc_html__( "First Name", "quotes-llama" );
- $last_label = esc_html__( "Last Name", "quotes-llama" );
- $source_label = esc_html__( "Source", "quotes-llama" );
- $imgurl_label = esc_html__( "Image URL", "quotes-llama" );
- $img_button = "<button class='quotes-llama-media-button'>Select image</button>";
- $security = wp_nonce_field( "quotes_llama_form", "security" );
- $quotes_edit_heredoc = <<< quotes_edit_heredoc
- <form name="{$form_name}"
- method="post"
- action="{$action_url}">
- {$hidden_input}
- {$security}
- <table class="form-table"
- cellpadding="5"
- cellspacing="2"
- width="100%">
- <tbody>
- <tr class="form-field form-required">
- <th style="text-align:left;"
- scope="row"
- valign="top">
- <label for="quotes_llama_quote">
- {$quote_label}
- </label>
- </th>
- <td>
- <textarea id="quotes_llama_quote"
- name="quote"
- rows="5"
- cols="50"
- style="width: 97%;">{$quote}</textarea>
- </td>
- </tr>
- <tr class="form-field">
- <th style="text-align:left;"
- scope="row"
- valign="top">
- <label for="quotes-llama-widget-author">
- {$first_label}
- </label>
- </th>
- <td>
- <input type="text"
- id="quotes-llama-widget-author"
- name="first_name"
- size="40"
- value="{$first_name}"
- placeholder="optional">
- </td>
- </tr>
- <tr class="form-field">
- <th style="text-align:left;"
- scope="row"
- valign="top">
- <label for="quotes-llama-widget-author">
- {$last_label}
- </label>
- </th>
- <td>
- <input type="text"
- id="quotes-llama-widget-author"
- name="last_name"
- size="40"
- value="{$last_name}"
- placeholder="optional">
- </td>
- </tr>
- <tr class="form-field">
- <th style="text-align:left;"
- scope="row"
- valign="top">
- <label for="quotes_llama_source">
- {$source_label}
- </label>
- </th>
- <td>
- <input type="text"
- id="quotes_llama_source"
- name="source"
- size="40"
- value="{$source}"
- placeholder="optional">
- </td>
- </tr>
- <tr class="form-field">
- <th style="text-align:left;"
- scope="row"
- valign="top">
- <label for="quotes_llama_imgurl">
- {$imgurl_label}
- </label>
- </th>
- <td>
- <input type="text"
- id="quotes_llama_imgurl"
- name="img_url"
- size="40"
- value="{$img_url}"
- placeholder="optional">
- {$img_button}
- </td>
- </tr>
- </tbody>
- </table>
- <p class="submit">
- {$back}
- <input name="submit"
- value="{$submit_value}"
- type="submit"
- class="button button-primary">
- </p>
- </form>
- quotes_edit_heredoc;
- // There can be no spaces or tabs or anything on same line as quotes_edit_heredoc;.
- return $quotes_edit_heredoc;
- } // End quotes_form
- /*
- * Add a new quote to the database.
- *
- * @param string $quote required - The text to be quoted.
- * @param string $first_name - Authors first and middle name.
- * @param string $last_name - Authors last name.
- * @param string $source - The source text.
- * @param string $img_url - The url to an image file.
- *
- * @return string - Message of result.
- */
- public function quotes_insert( $quote, $first_name = "", $last_name = "", $source = "", $img_url = "" ) {
- global $allowedposttags;
- if ( ! $quote ) {
- return "<div class='error'>" . __( "There was no quote to add to the database.", "quotes-llama" ) . "</div>";
- }
- global $wpdb;
- if ( $wpdb->get_var("SHOW TABLES LIKE '$this->table_name'") != $this->table_name ) {
- return "<div class='error'>" . esc_html__("Database table not found!", "quotes-llama") . "</div>";
- } else {
- // Validate data before insert.
- $quote = $this->validate( $quote );
- $first_name = $this->validate( $first_name );
- $last_name = $this->validate( $last_name );
- $source = $this->validate( $source );
- $img_url = $this->validate( $img_url );
- // Insert - handles prepare.
- // http://codex.wordpress.org/Class_Reference/wpdb#INSERT_row
- $results = $wpdb->insert(
- $this->table_name,
- array(
- "quote" => $quote,
- "first_name" => $first_name,
- "last_name" => $last_name,
- "source" => $source,
- "img_url" => $img_url
- ),
- array( "%s", "%s", "%s", "%s", "%s" )
- );
- if ( FALSE === $results ) {
- return "<div class='error'>" . esc_html__("An error occurred in the MySQL query.", "quotes-llama") . "</div>";
- } else {
- return "<div class='updated'>" . esc_html__("Quote Added", "quotes-llama") . "</div>";
- }
- }
- }
- /*
- * Update a quote.
- *
- * @param string $quote_id required - The id of the quote in the database.
- * @param string $quote required - The text to be quoted.
- * @param string $first_name - Authors first and middle name.
- * @param string $last_name - Authors last name.
- * @param string $source - The source text.
- * @param string $img_url - The url to an image file.
- *
- * @return string - Message of result.
- */
- public function quotes_update( $quote_id, $quote, $first_name = "", $last_name = "", $source = "", $img_url = "" ) {
- global $allowedposttags;
- if ( ! $quote ) {
- return "<div class='error'>" . esc_html__( "Quote not updated.", "quotes-llama" ) . "</div>";
- }
- global $wpdb;
- if ( $wpdb->get_var( "SHOW TABLES LIKE '$this->table_name'" ) != $this->table_name ) {
- return "<div class='error'>" . esc_html__( "Quotes llama database table not found", "quotes-llama" ) . "</div>";
- } else {
- // Validate data before insert.
- $quote = $this->validate( $quote );
- $first_name = $this->validate( $first_name );
- $last_name = $this->validate( $last_name );
- $source = $this->validate( $source );
- $img_url = $this->validate( $img_url );
- // http://codex.wordpress.org/Class_Reference/wpdb#UPDATE_rows
- $results = $wpdb->update(
- $this->table_name,
- array(
- "quote" => $quote,
- "first_name" => $first_name,
- "last_name" => $last_name,
- "source" => $source,
- "img_url" => $img_url
- ),
- array( "quote_id" => $quote_id ),
- array( "%s", "%s", "%s", "%s", "%s" )
- );
- if ( false === $results ) {
- return "<div class='error'>" . esc_html__( "There was an error in the MySQL query.", "quotes-llama" ) . "</div>";
- } else {
- return "<div class='updated'>" . esc_html__( "Quote Saved", "quotes-llama" ) . "</div>";
- }
- }
- }
- /*
- * Delete a single quote.
- *
- * @param int $quote_id - The quote_id of the quote to delete in the database.
- *
- * @return string - Message of result.
- */
- public function quotes_delete( $quote_id ) {
- if ( $quote_id ) {
- global $wpdb;
- // http://codex.wordpress.org/Class_Reference/wpdb#DELETE_Rows
- $result = $wpdb->delete( $this->table_name, array( "quote_id" => $quote_id ) );
- if ( false === $result ) {
- return "<div class='error'>" . esc_html__( "There was an error in the MySQL query.", "quotes-llama" ) . "</div>";
- } else {
- return "<div class='updated'>" . esc_html__( "Quote Deleted", "quotes-llama" ) . "</div>";
- }
- } else {
- return "<div class='error'>" . esc_html__( "Quote cannot be deleted.", "quotes-llama" ) . "</div>";
- }
- }
- /*
- * Bulk delete quotes.
- *
- * @param Array $quote_ids - Array of ids to delete.
- *
- * @return string message of result.
- */
- public function quotes_delete_bulk( $quote_ids ) {
- if ( $quote_ids ) {
- global $wpdb;
- // Count the number of checkboxes.
- $id_count = count( $quote_ids );
- // Prepare the placeholders array for ->prepare() it needs a string of the values.
- // http://php.net/manual/en/function.array-fill.php
- $id_holder = array_fill( 0, $id_count, "%s" );
- // Create one string from placeholders %s, %s, %s, %s, %s.
- $holders_id = "( " . implode( ", ", $id_holder ) . " )";
- // Create delete bulk query string.
- $query = "DELETE FROM " . $this->table_name . " WHERE quote_id IN " . $holders_id;
- // Delete rows.
- $result = $wpdb->query( $wpdb->prepare( $query, $quote_ids ) );
- if ( $result ) {
- return "<div class='updated'>" . esc_html__( "Quotes Deleted", "quotes-llama" ) . "</div>";
- } else {
- return "<div class='error'>" . esc_html__( "Unable to delete quotes.", "quotes-llama" ) . "</div>";
- }
- } else {
- return "<div class='error'>" . esc_html__( "No quotes selected.", "quotes-llama" ) . "</div>";
- }
- }
- /*
- * Get quotes for widget, gallery, page, search, and random requests
- *
- * @param int $quote_id - id of quote to get or type of quote
- *
- * @return result array.
- */
- public function quotes_select( $quote_id = 0 ) {
- global $wpdb;
- // Get quote by id for manage quotes page and static quote id on pages etc.
- if ( is_numeric( $quote_id ) && $quote_id > 0 ) {
- $quote = $wpdb->prepare( "SELECT
- quote_id,
- quote,
- first_name,
- last_name,
- source,
- img_url FROM " . $this->table_name .
- " WHERE quote_id = %d", $quote_id );
- $quote_data = $wpdb->get_row( $quote, ARRAY_A );
- return $quote_data;
- }
- // Page, Get authors first, last name for sidebar author list.
- if ( $quote_id == "author_list" ) {
- $authors_data = "SELECT first_name, last_name, count(first_name) AS quotecount FROM " .
- $this->table_name . " GROUP BY last_name, first_name ORDER BY last_name";
- $authors = $wpdb->get_results( $authors_data );
- return $authors;
- }
- // Widget and Gallery, Get random quote for .ajax request or function call.
- // Echo and cancel .ajax return or return data.
- if ( $quote_id == "random" || isset( $_REQUEST["random"] ) ) {
- $sql = "SELECT quote, first_name, last_name, source, img_url
- FROM " . $this->table_name .
- " ORDER BY RAND() LIMIT 1";
- $quote_data = $wpdb->get_row( $sql, ARRAY_A );
- if ( isset( $_REQUEST["random"] ) ) {
- echo json_encode( $quote_data );
- die();
- }
- if ( $quote_id == "random" ) {
- return $quote_data;
- }
- }
- // Page, Get all quotes for author in sidebar.
- if ( isset( $_REQUEST["quotes_by_author"] ) ) {
- // http://codex.wordpress.org/Function_Reference/wp_verify_nonce
- if ( isset( $_REQUEST["security"] ) && wp_verify_nonce( $_REQUEST["security"], "quotes_llama_page" ) ) {
- // .ajax request first name.
- if ( isset( $_REQUEST["first"] ) && $_REQUEST["first"] != "" ) {
- $first = $_REQUEST["first"];
- } else {
- $first = false;
- }
- // .ajax request last name.
- if ( isset( $_REQUEST["last"] ) && $_REQUEST["last"] != "" ) {
- $last = $_REQUEST["last"];
- } else {
- $last = false;
- }
- if ( $first && $last ) {
- $quotes = $wpdb->get_results( $wpdb->prepare( "SELECT quote,
- first_name,
- last_name,
- source,
- img_url FROM " . $this->table_name .
- " WHERE first_name = '%s' AND last_name = '%s'
- ORDER BY last_name, first_name", $first, $last ) );
- // http://php.net/manual/en/function.empty.php
- } elseif ( $first && empty( $last ) ) {
- $quotes = $wpdb->get_results( $wpdb->prepare( "SELECT quote,
- first_name,
- last_name,
- source,
- img_url FROM " . $this->table_name .
- " WHERE first_name = '%s' ORDER BY first_name", $first ) );
- } elseif ( empty( $first ) && $last ) {
- $quotes = $wpdb->get_results( $wpdb->prepare( "SELECT quote,
- first_name,
- last_name,
- source,
- img_url FROM " . $this->table_name .
- " WHERE last_name = '%s' ORDER BY last_name", $last ) );
- }
- // Render results.
- $this->template_page_author( $quotes );
- die();
- } else {
- echo "<div class='error'><p>" . __( "Security token mismatch, please reload the page and try again.", "quotes-llama" ) . "</p></div>";
- }
- }
- // Page, Search for quote.
- if ( isset ( $_REQUEST["search_for_quote"] ) ) {
- // Check nonce
- if ( isset( $_REQUEST["security"] ) && wp_verify_nonce( $_REQUEST["security"], "quotes_llama_page" ) ) {
- if ( isset ( $_REQUEST["term"] ) ) {
- $term = $_REQUEST["term"];
- } else {
- $term = "";
- }
- if ( isset ( $_REQUEST["search_column"] ) ) {
- $search_column = $_REQUEST["search_column"];
- } else {
- $search_column = "quote";
- }
- $quotes = $wpdb->get_results( $wpdb->prepare( "SELECT quote,
- first_name,
- last_name,
- source,
- img_url FROM " . $this->table_name .
- " WHERE $search_column LIKE '%%%s%%'
- ORDER BY last_name, first_name", $term ) );
- // Render results.
- $this->template_page_search( $quotes );
- die();
- } else {
- echo "<div class='error'><p>" . __( "Security token mismatch, please reload the page and try again.", "quotes-llama" ) . "</p></div>";
- }
- }
- }
- /*
- * Check if an option isset
- *
- * @param string $option - the option to check on.
- *
- * @return mixed - false if no option or option string if found.
- */
- public function check_plugin_option( $option ) {
- if ( ! $option ) {
- return false;
- }
- if ( isset ( $this->plugin_options["$option"] ) ) {
- return $this->plugin_options["$option"];
- } else {
- return false;
- }
- }
- /*
- * Run stripslashes(), trim() and wp_kses_post on string for output to display.
- *
- * @param string $str - String to sanitize.
- *
- * @return string - Escaped string with no /'s or whitespace but with allowed html tags.
- */
- public function strip_trim_wp_kses_post( $str ) {
- // http://php.net/manual/en/function.stripslashes.php
- // http://php.net/manual/en/function.trim.php
- // https://codex.wordpress.org/Function_Reference/wp_kses_post
- $str = stripslashes( wp_kses_post( trim( $str ) ) );
- return $str;
- }
- /*
- * Run stripslashes(), trim() and esc_attr on string for output to display.
- *
- * @param string $str - String to sanitize.
- *
- * @return string - Escaped string with no /'s or whitespace.
- */
- public function strip_trim_esc_attr( $str ) {
- // http://codex.wordpress.org/Function_Reference/esc_attr
- $str = stripslashes( esc_attr( trim( $str ) ) );
- return $str;
- }
- /*
- * Run stripslashes(), trim() and esc_url on string for output to display.
- *
- * @param string $str - String to sanitize.
- *
- * @return string - Escaped string with no /'s or whitespace.
- */
- public function strip_trim_esc_url( $str ) {
- // http://codex.wordpress.org/Function_Reference/esc_url
- $str = stripslashes( esc_url( trim( $str ) ) );
- return $str;
- }
- /*
- * Renders page view [quotes-llama mode='page']
- * Lists all the authors and search form.
- *
- * @return String - must return string, not echo or display or will render at top of page regardless of positioning.
- */
- public function template_page() {
- global $wpdb;
- // http://codex.wordpress.org/Function_Reference/wp_create_nonce
- $security = wp_create_nonce( "quotes_llama_page" );
- // Get sort from options.
- $default_sort = $this->plugin_options["default_sort"];
- // Our logged in heredoc holder.
- $template_page_loggedin = "";
- // Display search form if logged in.
- if ( is_user_logged_in() ) {
- // heredoc vars
- $search_text = esc_html__( "Search", "quotes-llama" );
- $search_column_quote = "";
- $search_column_quote_title = esc_html__( "Quote", "quotes-llama" );
- $search_column_first_name = "";
- $search_column_first_name_title = esc_html__( "First Name", "quotes-llama" );
- $search_column_last_name = "";
- $search_column_last_name_title = esc_html__( "Last Name", "quotes-llama" );
- $search_column_source = "";
- $search_column_source_title = esc_html__( "Source", "quotes-llama" );
- $search_column_img_url = "";
- $search_column_img_url_title = esc_html__( "Img URL", "quotes-llama" );
- if ( isset( $_REQUEST["search_column"] ) ) {
- switch ( $_REQUEST["search_column"] ) {
- case "quote":
- $search_column_quote = " selected";
- break;
- case "first_name":
- $search_column_first_name = " selected";
- break;
- case "last_name":
- $search_column_last_name = " selected";
- break;
- case "source":
- $search_column_source = " selected";
- break;
- case "img_url":
- $search_column_img_url = " selected";
- break;
- default:
- $search_column_quote = " selected";
- }
- }
- // Start logged in heredoc - display search box
- $template_page_loggedin = '<div class="quotes-llama-page-quotes-form">' .
- '<input type="text" ' .
- 'class="quotes-llama-page-quotesearch" ' .
- 'id="quotes-llama-page-quotesearch" ' .
- 'name="quotes-llama-page-quotesearch" ' .
- 'security="' . $security . '" ' .
- 'size="20">' .
- '<button ' .
- 'class="quotes-llama-page-searchbutton" ' .
- 'id="quotes-llama-page-searchbutton" ' .
- 'name="quotes-llama-page-searchbutton" ' .
- 'size="30">' .
- $search_text .
- '</button>' .
- '<select name="search_column" class="search_column">' .
- '<option value="quote">' .
- $search_column_quote_title .
- '</option>' .
- '<option value="first_name">' .
- $search_column_first_name_title .
- '</option>' .
- '<option value="last_name">' .
- $search_column_last_name_title .
- '</option>' .
- '<option value="source">' .
- $search_column_source_title .
- '</option>' .
- '<option value="img_url">' .
- $search_column_img_url_title .
- '</option>' .
- '</select>' .
- '</div>';
- }
- // Build list of authors for page selection.
- $quotesresult = $this->quotes_select( "author_list" );
- $firstLetterIndexedArray = array();
- $header_letter_list = "";
- $author_link_list = "";
- $quotes_title = esc_html__( "Quotes", "quotes-llama" );
- $current_letter = "";
- $initial_quote = do_shortcode( "[quotes-llama]" );
- // Iteration indicator for adding letter separator.
- $current_quote_data = "";
- foreach ( $quotesresult as $quoteresult ) {
- // Total number of quotes.
- $countofquote = $quoteresult->quotecount;
- // First and middle name.
- $first_name = trim( $quoteresult->first_name );
- // Last name.
- $last_name = trim( $quoteresult->last_name );
- // Does this author have both first and last name.
- if ( $last_name ) {
- $name_index = strtoupper( substr( $last_name, 0, 1 ) );
- } else {
- $name_index = strtoupper( substr( $first_name, 0, 1 ) );
- }
- // Add index letter to array.
- $firstLetterIndexedArray[] = array( "index" => $name_index, "last" => $last_name, "count" => $countofquote, "first" => $first_name );
- }
- // Sort the index array.
- sort( $firstLetterIndexedArray );
- // Build string of header letter links from index array.
- foreach ( $firstLetterIndexedArray as $letter ) {
- if ( $current_letter != $letter["index"] ) {
- $header_letter_list .= '<a href="#' . $letter["index"] . '"><button>' . $this->strip_trim_wp_kses_post( $letter["index"] ) . '</button></a>';
- $current_letter = $letter["index"];
- }
- }
- // Build string of author links from index array.
- foreach ( $firstLetterIndexedArray as $quote_data ) {
- if ( $current_quote_data == $quote_data["index"] ) {
- // Add just the author if separator already added.
- $author_link_list .= '<li><a class="quotes-llama-page-link" ' .
- 'name="' . $this->strip_trim_esc_attr( $quote_data["first"] . '" "' . $quote_data["last"] ) . '" ' .
- 'first="' . $this->strip_trim_esc_attr( $quote_data["first"] ) . '" ' .
- 'last="' . $this->strip_trim_esc_attr( $quote_data["last"] ) . '" ' .
- 'security="' . $security . '" ' .
- 'href="#quotes-llama-page-print" ' .
- 'title="' . $this->strip_trim_esc_attr( $quote_data["first"] . '" "' . $quote_data["last"] ) . '">';
- // if first and last name
- if ( $quote_data["last"] ) {
- $author_link_list .= $this->strip_trim_wp_kses_post( $quote_data["last"] . ", " . $quote_data["first"] );
- } else {
- $author_link_list .= $this->strip_trim_wp_kses_post( $quote_data["first"] );
- }
- $author_link_list .= '</a></li>';
- } else {
- // Add letter to sidebar separator and add author.
- $author_link_list .= '<div class="quotes-llama-page-letter">' .
- '<a name="' . $this->strip_trim_esc_attr( $quote_data["index"] ) . '">' .
- '<center>' .
- $this->strip_trim_wp_kses_post( $quote_data["index"] ) .
- '</center>' .
- '</a>' .
- '</div>' .
- '<li>' .
- '<a class="quotes-llama-page-link" ' .
- 'name="' . $this->strip_trim_esc_attr( $quote_data["first"] . '" "' . $quote_data["last"] ) . '" ' .
- 'first="' . $this->strip_trim_esc_attr( $quote_data["first"] ) . '" ' .
- 'last="' . $this->strip_trim_esc_attr( $quote_data["last"] ) . '" ' .
- 'security="' . $security . '" ' .
- 'href="#quotes-llama-page-print" ' .
- 'title="' . $this->strip_trim_esc_attr( $quote_data["first"] . '" "' . $quote_data["last"] ) . '">';
- // if first and last name display that text
- if ( $quote_data["last"] ) {
- $author_link_list .= $this->strip_trim_wp_kses_post( $quote_data["last"] . ", " . $quote_data["first"] );
- } else {
- $author_link_list .= $this->strip_trim_esc_attr( $quote_data["first"] );
- }
- $author_link_list .= '</a></li>';
- $current_quote_data = $quote_data["index"];
- }
- }
- // Now that we have all our data formatted build our output div.
- $template_page = '<div class="quotes-llama-page-status"></div>' .
- '<div class="quotes-llama-page-container">' .
- '<div class="quotes-llama-page-sidebarleft">' .
- '<div class="quotes-llama-page-title">' .
- '<center>' .
- '<h3>' .
- $quotes_title .
- '</h3>' .
- '</center>' .
- '<center class="quotes-llama-page-alpha">' .
- $header_letter_list .
- '</center>' .
- '</div>' .
- $author_link_list .
- '</div>' .
- '<div id="quotes-llama-printquote" class="quotes-llama-page-quote">' . $initial_quote . '</div>' .
- '</div>' .
- '<br clear="both">';
- return $template_page_loggedin . $template_page;
- }
- /*
- * Renders results of quotes search from the page view.
- *
- * @param Array $quotes - Array of search results.
- */
- public function template_page_search( $quotes ) {
- if ( $quotes ) {
- $author = "";
- foreach ( $quotes as $quote ) {
- if ( $author == $this->strip_trim_wp_kses_post( $quote->first_name . " " . $quote->last_name ) ) { ?>
- <div class="quotes-llama-quote-quote quotes-llama-page-more">
- <?php echo $this->strip_trim_wp_kses_post( $quote->quote ); ?>
- </div>
- <div class="quotes-llama-page-source">
- <?php // if there is a source
- if ( $quote->source ) {
- echo "<cite>" . make_clickable( $this->strip_trim_wp_kses_post( $quote->source ) ) . "</cite>";
- } ?>
- </div> <?php
- } else { ?>
- <div class="quotes-llama-quote-author">
- <h2>
- <?php // Check that we have an image url to use.
- if ( $quote->img_url ) { ?>
- <img src="<?php echo $quote->img_url; ?>"
- hspace="5"
- align="left"> <?php
- }
- echo $this->strip_trim_wp_kses_post( $quote->first_name . " " . $quote->last_name ); ?>
- </h2>
- </div>
- <div class="quotes-llama-quote-quote quotes-llama-page-more">
- <?php echo $this->strip_trim_wp_kses_post( $quote->quote ); ?>
- </div>
- <div class="quotes-llama-page-source">
- <?php // if there is a source
- if ( $quote->source ) {
- echo "<cite>" . make_clickable( $this->strip_trim_wp_kses_post( $quote->source ) ) . "</cite>";
- } ?>
- </div> <?php
- }
- $author = $this->strip_trim_wp_kses_post( $quote->first_name . " " . $quote->last_name );
- echo "<br clear='both'>";
- } ?>
- <div class="quotes-llama-quote-author-back quotes-llama-inline"> <?php
- echo "<input type='button' value='Print' class='quotes-llama-print')"; ?>
- </div> <?php
- } else {
- esc_html_e( "Search returned nothing", "quotes-llama" );
- }
- } // end template_page_search()
- /*
- * Renders a list of author quotes in the page view.
- *
- * @param Array $quotes - Array of authors quotes.
- */
- public function template_page_author( $quotes ) {
- $author = "";
- foreach ( $quotes as $quote ) {
- if ( $author == $this->strip_trim_wp_kses_post( $quote->first_name . " " . $quote->last_name ) ) { ?>
- <div class="quotes-llama-quote-quote quotes-llama-page-more">
- <?php echo $this->strip_trim_wp_kses_post( $quote->quote ); ?>
- </div>
- <div class="quotes-llama-page-source">
- <?php // if there is a source
- if ( $quote->source ) {
- echo "<cite>" . make_clickable( $this->strip_trim_wp_kses_post( $quote->source ) ) . "</cite>";
- } ?>
- </div> <?php
- } else {
- // include authors image. ?>
- <div class="quotes-llama-quote-author">
- <h2>
- <?php // Check that we have an image url to use.
- if ( $quote->img_url ) { ?>
- <img src="<?php echo $quote->img_url; ?>"
- hspace="5"
- align="left"> <?php
- }
- echo $this->strip_trim_wp_kses_post( $quote->first_name . " " . $quote->last_name ); ?>
- </h2>
- </div>
- <div class="quotes-llama-quote-quote quotes-llama-page-more">
- <?php echo $this->strip_trim_wp_kses_post( $quote->quote ); ?>
- </div>
- <div class="quotes-llama-page-source">
- <?php // if there is a source
- if ( $quote->source ) {
- echo "<cite>" . make_clickable( $this->strip_trim_wp_kses_post( $quote->source ) ) . "</cite>";
- } ?>
- </div> <?php
- }
- $author = $this->strip_trim_wp_kses_post( $quote->first_name . " " . $quote->last_name );
- echo "<br clear='both'><hr>";
- } ?>
- <div class="quotes-llama-quote-author-back quotes-llama-inline"> <?php
- echo "<a class='quotes-llama-quote-author-back quotes-llama-inline' title='" . esc_html__( 'Return to' ) . ' ' . $this->strip_trim_wp_kses_post( $author ) . "' href='#" . $author . "'><input type='button' value='←'></a>";
- echo "<input type='button' value='Print' class='quotes-llama-print')"; ?>
- </div>
- <?php die();
- }
- /*
- * Renders a static quote by id in page, post or template.
- *
- * @param int $id - quote id?
- * @param bool $show_author - show the author?
- * @param bool $show_source - show the source?
- * @param bool $show_image - show the image?
- *
- * @return String - must return string, not echo or display or will render at top of page regardless of positioning.
- */
- public function template_id( $id = 1, $show_author = false, $show_source = false, $show_image = false ) {
- $use_comma = false;
- $show_author = $this->check_plugin_option( "show_page_author" );
- $show_source = $this->check_plugin_option( "show_page_source" );
- $show_image = $this->check_plugin_option( "show_page_image" );
- $source_newline = $this->check_plugin_option( "source_newline" );
- // Get the quote by the id shortcode.
- $quote_data = $this->quotes_select( $id );
- $image = "";
- if ( $show_image ) {
- $image_exist = $this->strip_trim_wp_kses_post( $quote_data["img_url"] );
- if ( isset( $image_exist ) && !empty( $image_exist ) ) {
- $image = "<img src='" . $image_exist . "' align='left'>";
- }
- }
- // if showing author or source
- if ( $show_author || $show_source ) {
- $author_source = "<span class='quotes-llama-widget-author'>";
- // if showing author build string
- if ( $show_author && ( $quote_data["first_name"] || $quote_data["last_name"] ) ) {
- $use_comma = true;
- $author_source .= $this->strip_trim_wp_kses_post( $quote_data["first_name"] .
- " " . $quote_data["last_name"] );
- }
- // If showing both author and source, add a comma to author_source or new line if option set to new line.
- if ( $use_comma && ( $show_source && $quote_data["source"] ) ) {
- if ( $source_newline == "br" ) {
- $author_source .= "<br>";
- } else {
- $author_source .= ",";
- }
- }
- // If showing source build string.
- if ( $show_source ) {
- // Check that there is a source.
- if ( $quote_data["source"] ) {
- $author_source .= " <cite>" .
- make_clickable( $this->strip_trim_wp_kses_post( $quote_data["source"] ) ) . "</cite>";
- }
- }
- $author_source .= "</span>";
- } else {
- $author_source = "";
- }
- // Build our div
- return '<div class="quotes-llama-widget-random widget-text wp_widget_plugin_box">' .
- $image .
- '<span class="quotes-llama-widget-more">' .
- $this->strip_trim_wp_kses_post( $quote_data["quote"] ) .
- '</span>' .
- $author_source .
- '</div>';
- }
- /*
- * Renders a single random quote in page, post or template.
- *
- * @param bool $show_author - show the author?
- * @param bool $show_source - show the source?
- * @param bool $show_image - show the image?
- *
- * @return String - must return string, not echo or display or will render at top of page regardless of positioning.
- */
- public function template_post( $show_author = false, $show_source = false, $show_image = false ) {
- $use_comma = false;
- $show_author = $this->check_plugin_option( "show_page_author" );
- $show_source = $this->check_plugin_option( "show_page_source" );
- $show_image = $this->check_plugin_option( "show_page_image" );
- $source_newline = $this->check_plugin_option( "source_newline" );
- // Get a random quote.
- $quote_data = $this->quotes_select( "random" );
- $image = "";
- if ( $show_image ) {
- $image_exist = $this->strip_trim_wp_kses_post( $quote_data["img_url"] );
- if ( isset( $image_exist ) && !empty( $image_exist ) ) {
- $image = "<img src='" . $image_exist . "' align='left'>";
- }
- }
- // if showing author or source
- if ( $show_author || $show_source ) {
- $author_source = "<span class='quotes-llama-widget-author'>";
- // if showing author build string
- if ( $show_author && ( $quote_data["first_name"] || $quote_data["last_name"] ) ) {
- $use_comma = true;
- $author_source .= $this->strip_trim_wp_kses_post( $quote_data["first_name"] .
- " " . $quote_data["last_name"] );
- }
- // If showing both author and source, add a comma to author_source or new line if option set to new line.
- if ( $use_comma && ( $show_source && $quote_data["source"] ) ) {
- if ( $source_newline == "br" ) {
- $author_source .= "<br>";
- } else {
- $author_source .= ",";
- }
- }
- // If showing source build string.
- if ( $show_source ) {
- // Check that there is a source.
- if ( $quote_data["source"] ) {
- $author_source .= " <cite>" .
- make_clickable( $this->strip_trim_wp_kses_post( $quote_data["source"] ) ) . "</cite>";
- }
- }
- $author_source .= "</span>";
- } else {
- $author_source = "";
- }
- // Return the built div.
- return '<div class="quotes-llama-widget-random widget-text wp_widget_plugin_box">' .
- $image .
- '<span class="quotes-llama-widget-more">' .
- $this->strip_trim_wp_kses_post( $quote_data["quote"] ) .
- '</span>' .
- $author_source .
- '</div>';
- }
- /*
- * Gallery html contianer for shortcode call.
- * Must return string and not just display or echo in order to maintain load order of shortcode call.
- * See JS files Gallery sections for dynamic content and funtion.
- *
- * @return String - must return string, not echo or display or will render at top of page regardless of positioning.
- */
- public function template_gallery() {
- return '<div class="quotes-llama-gallery" ' .
- 'gauthor="' . $this->check_plugin_option( "show_gallery_author" ) . '"' .
- 'gsource="' . $this->check_plugin_option( "show_gallery_source" ) . '"' .
- 'gimage="' . $this->check_plugin_option( "show_gallery_image" ) . '">' .
- '<div class="quotes-llama-gallery-rotate">' .
- '<div class="quotes-llama-gallery-countdown quotes-llama-gallery-reenable"></div>' .
- '<div class="quotes-llama-gallery-quotebox"></div>' .
- '</div>' .
- '</div>';
- }
- /*
- * Registers the widget class.
- */
- public function register_widgets() {
- register_widget( "QuotesLlamaWidget" );
- }
- /*
- * Renders a widget instance in the sidebar.
- *
- * @param bool $show_author - show the author?
- * @param bool $show_source - show the source?
- * @param bool $show_image - show the image?
- * @param string $div_instance - previous instance id so we can replace it instead of nest into it.
- */
- public function widget_instance( $quote_id = 0, $show_author = true, $show_source = true, $show_image = true, $next_quote = true, $gallery = false, $div_instance = 0 ) {
- $use_comma = false;
- $source_newline = $this->check_plugin_option( "source_newline" );
- // If updating options in one of the widget instances.
- if ( isset( $_REQUEST["author"] ) ) $show_author = $_REQUEST["author"];
- if ( isset( $_REQUEST["source"] ) ) $show_source = $_REQUEST["source"];
- if ( isset( $_REQUEST["image"] ) ) $show_image = $_REQUEST["image"];
- if ( isset( $_REQUEST["next_quote"] ) ) $next_quote = $_REQUEST["next_quote"];
- if ( isset( $_REQUEST["gallery"] ) ) $gallery = $_REQUEST["gallery"];
- if ( isset( $_REQUEST["quote_id"] ) ) $quote_id = $_REQUEST["quote_id"];
- if ( isset( $_REQUEST["div_instance"] ) ) $div_instance = $_REQUEST["div_instance"];
- // Get a random quote if no static quote id is provided.
- if ( $quote_id > 0 ) {
- // disable auto-refresh
- $gallery = false;
- $quote_data = $this->quotes_select( $quote_id );
- } else {
- $quote_data = $this->quotes_select( "random" );
- }
- $image = "";
- $author_source = "";
- // Check for gallery widget mode. Return here if enabled.
- if ( $gallery ) {
- // Create div to load .ajax refresh into.
- $div_instance = "q" . rand( 1000, 100000 ); ?>
- <div id="<?php echo $div_instance; ?>"
- class="quotes-llama-widget-gallery widget-text wp_widget_plugin_box"
- wauthor="<?php echo $show_author; ?>"
- wsource="<?php echo $show_source; ?>"
- wimage="<?php echo $show_image; ?>">
- </div><?php
- return;
- }
- // If showing image, build string.
- if ( $show_image ) {
- $image_exist = $this->strip_trim_wp_kses_post( $quote_data["img_url"] );
- if ( isset( $image_exist ) && !empty( $image_exist ) ) {
- $image = "<img src='" . $image_exist . "' align='left'>";
- }
- }
- // Span for author and source.
- $author_source = "<span class='quotes-llama-widget-author'>";
- // If showing author, add to author_source.
- if ( $show_author && ( $quote_data["first_name"] || $quote_data["last_name"] ) ) {
- $use_comma = true;
- $author_source .= $this->strip_trim_wp_kses_post( $quote_data["first_name"] .
- " " . $quote_data["last_name"] );
- }
- // If showing both author and source, add a comma to author_source or new line if option set to new line.
- if ( $use_comma && ( $show_source && $quote_data["source"] ) ) {
- if ( $source_newline == "br" ) {
- $author_source .= "<br>";
- } else {
- $author_source .= ",";
- }
- }
- // If showing source, add to author_source. Also close span either way.
- if ( $show_source && $quote_data["source"] ) {
- $author_source .= " <cite>" . make_clickable( $this->strip_trim_wp_kses_post( $quote_data["source"] ) ) . "</cite></span>";
- } else {
- $author_source .= "</span>";
- }
- // If no id already, create div id for widget next link
- // so next quote click uses the same unique id. Skip this on next click.
- if ( !$div_instance ) {
- $div_instance = "q" . rand( 1000, 100000 );
- // Render initial widget div. ?>
- <div id="<?php echo $div_instance; ?>"
- class="quotes-llama-widget-random widget-text wp_widget_plugin_box"><?php
- }
- echo $image;
- // If quote id is provided set static class or just set next quote link class.
- if ( $quote_id > 0 ) {
- // If limiting quote
- echo '<span class="quotes-llama-widget-static-more">';
- } else {
- // full quote
- echo '<span class="quotes-llama-widget-next-more">';
- }
- echo $this->strip_trim_wp_kses_post( $quote_data['quote'] );
- echo "</span>";
- echo $author_source;
- // if showing static quote or if disabled in option, disable next quote link.
- if ( !$quote_id && $next_quote ) { ?>
- <span class="quotes-llama-widget-next"
- divid="<?php echo $div_instance; ?>"
- author="<?php echo $show_author; ?>"
- source="<?php echo $show_source; ?>"
- img="<?php echo $show_image; ?>">
- <i><?php esc_html_e( "next quote" ); ?></i>
- </span> <?php
- }
- echo "</div>";
- }
- /*
- * Sanitize a single element.
- *
- * @param string $data - String to sanitize.
- *
- * @return string - Sanitized string.
- */
- public function validate( $data ) {
- global $allowedposttags;
- // http://codex.wordpress.org/Function_Reference/wp_kses
- // http://php.net/manual/en/function.stripslashes.php
- $data = wp_kses( stripslashes( trim( $data ) ), $allowedposttags );
- // http://codex.wordpress.org/Function_Reference/esc_sql
- $data = esc_sql( $data );
- return $data;
- }
- /*
- * Adds screen options to admin page.
- * have to make sure that your derived class is instantiated
- * before the screen option panel is rendered so that the
- * parent class can retrieve the column names.
- * http://wpengineer.com/2426/wp_list_table-a-step-by-step-guide/
- */
- public function quotes_llama_add_option() {
- global $quotes_table;
- $option = 'per_page';
- $args = array(
- 'label' => 'Quotes per page',
- 'default' => 10,
- 'option' => 'quotes_llama_per_page'
- );
- add_screen_option( $option, $args );
- $quotes_table = new QuotesLlamaTable;
- }
- /*
- * Sets value for table screen options in admin page
- *
- * @param $status - ? IDK what status is for.
- * @param $option - The option name.
- * @param $value - The option value.
- *
- * @return string - Sanitized string.
- */
- public function quotes_llama_set_option( $status, $option, $value ) {
- return $value;
- }
- /*
- * Gets the current page number, search term, search column, sort coulmn, sort order.
- *
- * @return string - String ocurrent page number.
- */
- public function get_page_number() {
- $return_page = "";
- // Get the paged variable.
- if ( isset( $_REQUEST["paged"] ) ) {
- $return_page .= "&paged=" . $_REQUEST["paged"];
- }
- // Get the search term variable.
- if ( isset( $_REQUEST["quotes_llama_s"] ) ) {
- $return_page .= "&s=" . $_REQUEST["quotes_llama_s"];
- }
- // Get the search column variable.
- if ( isset( $_REQUEST["search_column"] ) ) {
- $return_page .= "&search_column=" . $_REQUEST["search_column"];
- }
- // Get the order variable.
- if ( isset( $_REQUEST["order"] ) ) {
- $return_page .= "&order=" . $_REQUEST["order"];
- }
- // Get the sort column variable.
- if ( isset( $_REQUEST["orderby"] ) ) {
- $return_page .= "&orderby=" . $_REQUEST["orderby"];
- }
- return $return_page;
- }
- /*
- * Return a selected image from the media library.
- *
- * Returns link to image.
- */
- public function get_image_from_media_library() {
- if ( $attachments = get_children( array(
- 'post_type' => 'attachment',
- 'post_mime_type'=>'image',
- 'numberposts' => 1,
- 'post_status' => null,
- //'post_parent' => $post->ID
- )));
- foreach ( $attachments as $attachment ) {
- // http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src
- $img = wp_get_attachment_image_src( $attachment->ID, "thumbnail" );
- }
- return $img;
- }
- } // end class QuotesLlama
- class QuotesLlamaWidget extends WP_Widget {
- /*
- * Widget - constructor
- * http://codex.wordpress.org/Widgets_API
- * http://developer.wordpress.org/reference/functions/wp_register_widget_control/
- */
- public function __construct() {
- $widget_ops = array( "description" => __( "Display a quote.", "quotes-llama" ) );
- parent::__construct( "quotesllamawidget", __( "Quotes llama", "quotes-llama" ), $widget_ops );
- }
- /*
- * Widget - Render admin page form.
- *
- * @param array $instance - Array of this particular widgets options
- */
- public function form( $instance ) {
- if ( $instance ) {
- $title = $instance["title"];
- $show_author = $instance["show_author"];
- $show_source = $instance["show_source"];
- $show_image = $instance["show_image"];
- $next_quote = $instance["next_quote"];
- $gallery = $instance["gallery"];
- $quote_id = $instance["quote_id"];
- } else {
- $title = "";
- $show_author = true;
- $show_source = true;
- $show_image = true;
- $next_quote = true;
- $gallery = false;
- $quote_id = "";
- } ?>
- <p>
- <label for="<?php echo $this->get_field_id( 'title' ); ?>">
- <?php esc_html_e( "Title", "quotes-llama" ); ?>
- </label>
- <?php // http://codex.wordpress.org/Function_Reference/get_field_name ?>
- <input class="widefat"
- id="<?php echo $this->get_field_id( 'title' ); ?>"
- name="<?php echo $this->get_field_name( 'title' ); ?>"
- type="text"
- value="<?php esc_attr_e( $title, 'quotes-llama' ); ?>"
- placeholder="<?php esc_html_e( '(Optional)' ); ?>">
- </p>
- <p>
- <input type="checkbox"
- id="<?php echo $this->get_field_id( 'show_author' ); ?>"
- name="<?php echo $this->get_field_name( 'show_author' ); ?>"
- value="1"
- <?php checked( "1", $show_author ); ?>>
- <label for="<?php echo $this->get_field_id( 'show_author' ); ?>">
- <?php esc_html_e( "Show author.", "quotes-llama" ); ?>
- </label>
- </p>
- <p>
- <input type="checkbox"
- id="<?php echo $this->get_field_id( 'show_source' ); ?>"
- name="<?php echo $this->get_field_name( 'show_source' ); ?>"
- value="1"
- <?php checked( "1", $show_source ); ?>>
- <label for="<?php echo $this->get_field_id( 'show_source' ); ?>">
- <?php esc_html_e( "Show source.", "quotes-llama" ); ?>
- </label>
- </p>
- <p>
- <input type="checkbox"
- id="<?php echo $this->get_field_id( 'show_image' ); ?>"
- name="<?php echo $this->get_field_name( 'show_image' ); ?>"
- value="1"
- <?php checked( "1", $show_image ); ?>>
- <label for="<?php echo $this->get_field_id( 'show_image' ); ?>">
- <?php esc_html_e( "Show image.", "quotes-llama" ); ?>
- </label>
- </p>
- <p>
- <input type="checkbox"
- id="<?php echo $this->get_field_id( 'next_quote' ); ?>"
- name="<?php echo $this->get_field_name( 'next_quote' ); ?>"
- value="1"
- <?php checked( "1", $next_quote ); ?>>
- <label for="<?php echo $this->get_field_id( 'next_quote' ); ?>">
- <?php esc_html_e( "Show 'next quote' link.", "quotes-llama" ); ?>
- </label>
- </p>
- <p>
- <input type="checkbox"
- id="<?php echo $this->get_field_id( 'gallery' ); ?>"
- name="<?php echo $this->get_field_name( 'gallery' ); ?>"
- value="1"
- <?php checked( "1", $gallery ); ?>>
- <label for="<?php echo $this->get_field_id( 'gallery' ); ?>">
- <?php esc_html_e( "Auto-Refresh", "quotes-llama" ); ?>
- </label>
- </p>
- <p>
- <label for="<?php echo $this->get_field_id( 'quote_id' ); ?>">
- <?php esc_html_e( "Quote ID", "quotes-llama" ); ?>
- </label>
- <input class="widefat"
- id="<?php echo $this->get_field_id( 'quote_id' ); ?>"
- name="<?php echo $this->get_field_name( 'quote_id' ); ?>"
- type="text"
- value="<?php esc_attr_e( $quote_id ); ?>"
- placeholder="<?php esc_html_e( 'To display a static quote, enter the ID here.' ); ?>">
- </p> <?php
- }
- /*
- * widget - update options
- */
- public function update( $new_instance, $old_instance ) {
- $instance = $old_instance;
- // Fields
- // http://php.net/manual/en/function.strip-tags.php
- $instance["title"] = strip_tags( $new_instance["title"] );
- $instance["show_author"] = strip_tags( $new_instance["show_author"] );
- $instance["show_source"] = strip_tags( $new_instance["show_source"] );
- $instance["show_image"] = strip_tags( $new_instance["show_image"] );
- $instance["next_quote"] = strip_tags( $new_instance["next_quote"] );
- $instance["gallery"] = strip_tags( $new_instance["gallery"] );
- $instance["quote_id"] = $new_instance["quote_id"];
- return $instance;
- }
- /*
- * Widget - Render sidebar.
- *
- * @param Array $args, The name of your widget class.
- * @param Array $instance - Contains options for this particular widget.
- */
- public function widget( $args, $instance ) {
- // http://php.net/manual/en/function.extract.php
- extract( $args );
- // These are the widget options.
- // http://codex.wordpress.org/Function_Reference/apply_filters
- $title = apply_filters( "widget_title", $instance["title"] );
- $show_author = $instance["show_author"];
- $show_source = $instance["show_source"];
- $show_image = $instance["show_image"];
- $next_quote = $instance["next_quote"];
- $gallery = $instance["gallery"];
- if ( isset( $instance["quote_id"] ) ) {
- $quote_id = $instance["quote_id"];
- } else {
- $quote_id = 0;
- }
- echo $before_widget;
- // Begin rendering the widget div.
- echo "<div class='widget-text wp_widget_plugin_box'>";
- // Check if title is set.
- if ( $title ) {
- echo $before_title . $title . $after_title;
- }
- // Display random quote widget in blog sidebar.
- echo "<div class='quotes-llama-widget-random'>";
- $widge = new QuotesLlama;
- $widge->widget_instance( $quote_id, $show_author, $show_source, $show_image, $next_quote, $gallery );
- echo "</div>";
- // End widget div.
- echo "</div>";
- echo $after_widget;
- }
- } // End class QuotesLlamaWidget
- class QuotesLlamaTable extends QuotesLlama_List_Table {
- private $strip_trim;
- /*
- * REQUIRED. Set up a constructor that references the parent constructor.
- */
- public function __construct() {
- // Set parent defaults for singular, plural, supports ajax
- parent::__construct( array(
- "singular" => "quote",
- "plural" => "quotes",
- "ajax" => false
- )
- );
- $this->strip_trim = new QuotesLlama;
- }
- /*
- * Set a default column if not one.
- *
- * @param array $item - A singular item (one full row's worth of data)
- * @param array $column_name - The name/slug of the column to be processed
- *
- * @return string - Default Text or HTML to be placed inside the column <td>
- */
- public function column_default( $item, $column_name ) {
- switch( $column_name ) {
- case "id":
- case "thequote":
- case "firstname":
- case "lastname":
- case "source":
- case "img":
- return $item[ $column_name ];
- default:
- // Show the whole array if no match
- return print_r( $item, true );
- }
- }
- /*
- * id column.
- */
- public function column_id( $item ) {
- return "<span style='color:silver'>" . $this->strip_trim->strip_trim_wp_kses_post( $item["quote_id"] ) . "</span>";
- }
- /*
- * Quote column.
- */
- public function column_thequote( $item ) {
- $return_page = "";
- // Get the paged variable.
- if ( isset( $_REQUEST["paged"] ) ) {
- $return_page .= "&paged=" . $_REQUEST["paged"];
- }
- // Get the search term variable.
- if ( isset( $_REQUEST["quotes_llama_s"] ) ) {
- $return_page .= "&s=" . $_REQUEST["quotes_llama_s"];
- }
- // Get the search column variable.
- if ( isset( $_REQUEST["search_column"] ) ) {
- $return_page .= "&search_column=" . $_REQUEST["search_column"];
- }
- // Get the order variable.
- if ( isset( $_REQUEST["order"] ) ) {
- $return_page .= "&order=" . $_REQUEST["order"];
- }
- // Get the sort column variable.
- if ( isset( $_REQUEST["orderby"] ) ) {
- $return_page .= "&orderby=" . $_REQUEST["orderby"];
- }
- // http://codex.wordpress.org/Function_Reference/wp_nonce_url
- $edit = wp_nonce_url("?page=" . $_REQUEST["page"] . $return_page . "&action=quotes_llama_edit"e_id=" . $item["quote_id"], "delete_edit");
- $delete = wp_nonce_url("?page=" . $_REQUEST["page"] . $return_page . "&action=quotes_llama_delete_single"e_id=" . $item["quote_id"], "delete_edit");
- // Links to edit / delete individual quotes.
- $actions = array(
- "edit" => "<a href='" . $edit . "'>Edit</a>",
- "delete" => "<a href='" . $delete . "' onclick='return confirm(\"" . esc_html__('Delete quote', 'quotes-llama') . " (" . $item["quote_id"] . ")? " . esc_html__('Select Cancel to stop, OK to delete.', 'quotes-llama') . "\")';>Delete</a>"
- );
- // Return the quote contents.
- return stripslashes( $item["quote"] ) .
- $this->row_actions( $actions );
- }
- /*
- * First name column.
- */
- public function column_firstname( $item ) {
- return $this->strip_trim->strip_trim_wp_kses_post( $item["first_name"] );
- }
- /*
- * Last name column.
- */
- public function column_lastname( $item ) {
- return $this->strip_trim->strip_trim_wp_kses_post( $item["last_name"] );
- }
- /*
- * Source column.
- */
- public function column_source( $item ) {
- return make_clickable( $this->strip_trim->strip_trim_wp_kses_post( $item["source"] ) );
- }
- /*
- * Image url column.
- */
- public function column_img( $item ) {
- // Check that we have an image url to use.
- if ( $item["img_url"] ) {
- return "<a href='" . $item["img_url"] . "' target='_blank' title='" . $this->strip_trim->strip_trim_wp_kses_post( $this->column_firstname( $item ) . " " . $this->column_lastname( $item ) ) . "\n" . $this->strip_trim->strip_trim_esc_url( $item["img_url"] ) . "'><img src='" . $this->strip_trim->strip_trim_esc_url( $item["img_url"] ) . "' width='75' title='" . $this->strip_trim->strip_trim_esc_attr( $this->column_firstname( $item ) . " " . $this->column_lastname( $item ) ) . "\n" . $this->strip_trim->strip_trim_esc_url( $item["img_url"] ) . "' alt='" . $this->strip_trim->strip_trim_esc_attr( $this->column_firstname( $item ) . " " . $this->column_lastname( $item ) ) . "\n" . $this->strip_trim->strip_trim_esc_url( $item["img_url"] ) . "'></a>";
- } else {
- return esc_html__( "No Image", "quotes-llama" );
- }
- }
- /*
- * REQUIRED if displaying checkboxes or using bulk actions! The 'cb' column
- * is given special treatment when columns are processed. It ALWAYS needs to
- * be defined and have it's own method.
- */
- public function column_cb( $item ) { ?>
- <input type="checkbox"
- name="bulkcheck[]"
- value="<?php echo $this->strip_trim->strip_trim_wp_kses_post( $item['quote_id'] ); ?>"> <?php
- }
- /*
- * The table's columns and titles.
- * The 'cb' column is treated differently than the rest. Including a checkbox
- * column in the table we must create a column_cb() method.
- */
- public function get_columns() {
- $columns = array(
- "cb" => "<input type='checkbox' />", // Render a checkbox.
- "id" => "ID",
- "thequote" => "Quote",
- "firstname" => "First Name",
- "lastname" => "Last Name",
- "source" => "Source",
- "img" => "Image"
- );
- return $columns;
- }
- /*
- * One or more columns to be sortable.
- * This merely defines which columns should be sortable and makes them
- * clickable - it does not handle the actual sorting.
- * Column => data field to sort.
- */
- public function get_sortable_columns() {
- $sortable_columns = array(
- "id" => array("quote_id",false), // True is already sorted.
- "thequote" => array("quote",false),
- "firstname" => array("first_name",false),
- "lastname" => array("last_name",false),
- "source" => array("source",false),
- "img" => array("img_url",false)
- );
- return $sortable_columns;
- }
- /*
- * Bulk actions dropdown.
- */
- public function get_bulk_actions() {
- $actions = array(
- "quotes-llama-bulk-delete" => __( "Delete", "quotes-llama" ),
- );
- return $actions;
- }
- /*
- * Prepare data for display.
- */
- public function prepare_items( $search = NULL, $per_page = 20 ) {
- global $wpdb;
- // Get our columns. Both hidden and sortable and
- // build column header array.
- $this->_column_headers = $this->get_column_info();
- /*
- * Nested function to usort table.
- */
- function usort_quotes( $a, $b ) {
- $quotes_usort_options = get_option( "quotes-llama-settings" );
- // If no sort, default to options.
- $orderby = ( !empty( $_REQUEST["orderby"] ) ) ? $_REQUEST["orderby"] : $quotes_usort_options["default_sort"];
- // If no order, default to options.
- $order = ( !empty( $_REQUEST["order"] ) ) ? $_REQUEST["order"] : $quotes_usort_options["default_order"];
- // Determine sort order
- // http://php.net/manual/en/function.strnatcmp.php
- $result = strnatcmp( $a[$orderby], $b[$orderby] );
- // Send final sort direction to usort.
- return ( $order==="asc" ) ? $result : -$result;
- }
- if( NULL != $search ) {
- if ( isset( $_REQUEST["quotes_llama_s"] ) ) {
- // http://codex.wordpress.org/Function_Reference/sanitize_text_field
- $search = sanitize_text_field( $_REQUEST["quotes_llama_s"] );
- }
- // Trim search term.
- $search = trim( $search );
- // Search column, default to quote.
- $search_column = trim( ( !empty( $_REQUEST["search_column"] ) ) ? $_REQUEST["search_column"] : "quote");
- // Query our search data to build table with.
- $data = $wpdb->get_results($wpdb->prepare("SELECT * FROM " .
- $wpdb->prefix . "quotes_llama WHERE $search_column LIKE '%%%s%%'", $search), ARRAY_A);
- } else {
- // Query all our quote data.
- $data = $wpdb->get_results( "SELECT * FROM " .
- $wpdb->prefix . "quotes_llama", ARRAY_A );
- }
- // Sort data.
- // http://php.net/manual/en/function.usort.php
- uasort ( $data, "usort_quotes" );
- // What page user is on.
- $current_page = $this->get_pagenum();
- // How many items in data array.
- $total_items = count( $data );
- // get the current user ID
- $user = get_current_user_id();
- // get the current admin screen
- $screen = get_current_screen();
- // retrieve the "per_page" option
- $screen_option_per_page = $screen->get_option('per_page', 'option');
- // retrieve the value of the option stored for the current user
- $per_page = get_user_meta($user, $screen_option_per_page, true);
- if ( empty ( $per_page) || $per_page < 1 ) {
- // get the default value if none is set
- $per_page = $screen->get_option( 'per_page', 'default' );
- }
- // Paginate data to fit our page count. wp_list_table's search_box() doesn't paginate.
- $data = array_slice( $data, ( ( $current_page-1 ) * $per_page ), $per_page );
- // Set sorted data to item var.
- $this->items = $data;
- // Register our pagination options.
- $this->set_pagination_args( array(
- "total_items" => $total_items,
- "per_page" => $per_page,
- "total_pages" => ceil( $total_items/$per_page )
- )
- );
- }
- } // End class QuotesLlamaTable
- class QuotesLlamaBackup {
- private $table_name;
- private $separator;
- private $filename;
- /*
- * @param array $table_n - Quotes table.
- * @param string $sep - csv delimiter.
- */
- public function __construct( $table_n, $sep ) {
- $this->table_name = $table_n;
- $this->separator = $sep;
- $this->filename = "quotes";
- }
- /*
- * Create .csv export.
- *
- * @param string $filename - Filename to write to.
- */
- public function create_csv() {
- $dateNow = date( "d-m-Y_His" );
- // Getting the text generated to download.
- $csvFile = $this->generate_csv();
- // http://php.net/manual/en/function.readfile.php
- header( "Pragma: public" );
- header( "Expires: 0" );
- header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
- // Set browser to download file.
- header( "Cache-Control: private", false );
- header( "Content-Type: application/octet-stream" );
- header( "Content-Disposition: attachment; filename=\"" . $this->filename . "_" . $dateNow . ".csv\";" );
- header( "Content-Transfer-Encoding: binary" );
- // echo create the csv file.
- echo $csvFile;
- die();
- }
- /*
- * Get data for .csv.
- *
- * @return string $csv_output - quote data with columns
- */
- public function generate_csv() {
- global $wpdb;
- // .csv data.
- $csv_output = "";
- // Get column names.
- $result = $wpdb->get_results( "SHOW COLUMNS FROM " . $this->table_name );
- if ( count( $result ) > 0 ) {
- // remove quote_id from header columns.
- // http://php.net/manual/en/function.unset.php
- unset( $result[0] );
- foreach( $result as $row ) {
- $csv_output = $csv_output . $row->Field . $this->separator;
- }
- // Trim trailing separator.
- // http://php.net/manual/en/function.substr.php
- $csv_output = substr( $csv_output, 0, -1 );
- }
- // Added our header so new line for data.
- $csv_output .= "\n";
- // Get all our quote data.
- $values = $wpdb->get_results( "SELECT * FROM " . $this->table_name, ARRAY_A );
- // http://php.net/manual/en/function.urldecode.php
- foreach ( $values as $value ) {
- // Remove the quote_id from each record.
- unset( $value["quote_id"] );
- // convert to numeric array.
- // http://php.net/manual/en/function.array-values.php
- $fields = array_values( $value );
- // Generating string with field separator.
- // http://php.net/manual/en/function.implode.php
- $csv_output .= stripslashes( implode( $this->separator, $fields ) );
- $csv_output .= "\n";
- }
- return $csv_output;
- }
- /*
- * Get data for .json
- * and create the file.
- */
- public function create_json() {
- global $wpdb;
- $dateNow = date( "d-m-Y_His" );
- $results = "SELECT * FROM " . $this->table_name;
- $quotes = $wpdb->get_results( $results, ARRAY_A );
- // remove the quote_id strip backslashes.
- foreach ( $quotes as $quote => $data ) {
- unset( $data["quote_id"] );
- $data["quote"] = stripslashes( $data["quote"] );
- $data["first_name"] = stripslashes( $data["first_name"] );
- $data["last_name"] = stripslashes( $data["last_name"] );
- $data["source"] = stripslashes( $data["source"] );
- $data["img_url"] = str_replace( "\"", "", $data["img_url"] );
- $quotes[$quote] = $data;
- }
- // http://php.net/manual/en/function.json-encode.php
- // http://php.net/manual/en/json.constants.php
- $json_output = json_encode( $quotes, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
- header( "Content-Type: text/json" );
- header( "Content-Disposition: attachment; filename=\"" . $this->filename . "_" . $dateNow . ".json\";" );
- echo $json_output;
- die();
- }
- /*
- * Generates the import query string for importing
- * from a valid json or csv file.
- */
- public function generate_import() {
- // http://php.net/manual/en/function.pathinfo.php
- // http://codex.wordpress.org/Function_Reference/sanitize_file_name
- $exts = array( "json", "JSON", "CSV", "csv" );
- $filename = sanitize_file_name( $_FILES["quotes-llama-file"]["name"] );
- $filetmp = $_FILES["quotes-llama-file"]["tmp_name"];
- $fileext = pathinfo( $_FILES["quotes-llama-file"]["name"], PATHINFO_EXTENSION );
- $fileerror = $_FILES["quotes-llama-file"]["error"];
- // Upload file and check it was successful
- // http://php.net/manual/en/features.file-upload.errors.php
- if ( $fileerror ) {
- $thismsg = $this->error_messages( $fileerror );
- echo "<div class='error'><p>" . $thismsg . "</p></div>";
- return;
- }
- // successfully uploaded file check type, has data, and is a valid file.
- if ( $fileerror === UPLOAD_ERR_OK && is_uploaded_file( $filetmp ) ) {
- // if our file is not of the type we are expecting.
- if( ! in_array( $fileext, $exts ) ) {
- echo "<div class='error'><p>" . __( "The file type ." . $fileext . " is not supported.", "quotes-llama" ) . "</p></div>";
- return;
- }
- // Check our upload is not an empty file.
- // http://php.net/manual/en/function.file-get-contents.php
- if ( ! ( $json_data = file_get_contents( $_FILES["quotes-llama-file"]["tmp_name"] ) ) ) {
- echo "<div class='error'><p>" . __( "Unable to import because the file is empty.", "quotes-llama" ) . "</p></div>";
- return;
- }
- // If this is a json file.
- if ( $fileext == "json" || $fileext == "JSON" ) {
- // Check that our data is in fact JSON data.
- if ( is_null( $quote_json = json_decode( $json_data, true ) ) ) {
- echo "<div class='error'><p>" . $this->error_messages() . "</p></div>";
- return;
- } else {
- // Import the json data.
- $result = $this->quotes_import($quote_json) . " from " . $filename;
- }
- // If this is a csv file.
- } elseif ( $fileext == "csv" || $fileext == "CSV" ) {
- // Parse the csv into an array.
- // http://php.net/manual/en/function.str-getcsv.php
- $header = NULL;
- $quote_entries = array();
- $count = 1;
- // http://php.net/manual/en/function.fopen.php
- if ( ( $handle = fopen( $filetmp, "r" ) ) !== FALSE ) {
- // http://php.net/manual/en/function.fgetcsv.php
- while ( ( $row = fgetcsv( $handle, 2000, $this->separator ) ) !== FALSE ) {
- // Check count of $row
- // fgetcsv() returns array of one row from the CSV file.
- // The array's count() is therefore the number of source columns.
- if ( count( $row ) <> 5 ) {
- echo "<div class='error'><p>" . __( "There was an error. Verification returned on line", "quotes-llama" );
- echo " " . $count . ". ";
- echo __( "Be sure the csv delimiter in the options tab is set to match your file. Your files encoding may not be supported. Errors within the file structure such as incorrect columns and fields can cause the import to fail as well.", "quotes-llama" ) . "</p></div>";
- // http://php.net/manual/en/function.fclose.php
- fclose( $handle );
- return;
- // combine our header and data arrays int one array.
- } else {
- // Assign first row data to header columns [header][row]
- if ( !$header ) {
- $header = $row;
- // If the .csv file was saved in utf8 then it probably
- // has BOM characters in the column headers.
- // We need to strip them or we get
- // echo pack("CCC", 0xEF, 0xBB, 0xBF) . "quote";
- // and other characters which fail to import the field.
- // Verifying 5 column headers.
- // http://php.net/manual/en/function.pack.php
- // http://stackoverflow.com/a/20692384/5730352
- for ( $i = 0; $i <= 4; $i++ ) {
- $header[$i] = preg_replace( '/[\x00-\x1F\x80-\xFF]/', '', $header[$i] );
- }
- } else {
- // http://php.net/manual/en/function.array-combine.php
- $quote_entries[] = array_combine( $header, $row );
- }
- }
- ++$count;
- }
- fclose( $handle );
- // Ready to import.
- $result = $this->quotes_import($quote_entries);
- }
- }
- // Check result of import.
- if ( ! $result ) {
- echo "<div class='error'><p>" . __( "Import failed. Please try again.", "quotes-llama" ) . "</p></div>";
- } else if ( 0 === $result) {
- echo "<div class='updated'><p>" . __( "No quotes imported", "quotes-llama" ) . "</p></div>";
- } else {
- // http://codex.wordpress.org/Function_Reference/_n
- echo "<div class='updated'><p>" .
- sprintf( _n( "Your quote was imported", "%d quotes imported", $result, "quotes-llama" ), $result ) .
- "</p></div>";
- }
- return;
- }
- }
- /*
- * Import quotes from array provided by either .csv or .json formats.
- *
- * @param array $quotes_data - Array of quote data.
- *
- * @return mixed - int results count, or error string.
- */
- public function quotes_import( $quotes_data = array() ) {
- global $wpdb;
- if ( ! $quotes_data ) {
- return 0;
- }
- // Create insert statement.
- $query = "INSERT INTO " . $this->table_name .
- " (quote, first_name, last_name, source, img_url)" .
- " VALUES ";
- // Array for insert values and placeholders.
- $values = array();
- // Prepare insert values.
- foreach ( $quotes_data as $quote ) {
- $values[] = $wpdb->prepare( "(%s,%s,%s,%s,%s)", $quote['quote'], $quote['first_name'], $quote['last_name'], $quote['source'], $quote['img_url']);
- }
- // Combine statement and values.
- // http://php.net/manual/en/function.implode.php
- $query .= implode( ",\n", $values );
- // Return success count or error.
- return $wpdb->query( $query );
- }
- /*
- * Handle error messages for file uploads and JSON decodes.
- *
- * @param int $error_code - The error code encountered.
- *
- * @return string - Description of the error.
- */
- public function error_messages( $error_code = "none" ) {
- // If no error code provided it is a JSON error.
- if ( $error_code == "none") {
- if ( ! function_exists( "json_last_error_msg" ) ) {
- /*
- * Handle error messages for validating JSON file data.
- * http://php.net/manual/en/function.json-last-error-msg.php
- *
- * @return string - Description of the error.
- */
- function json_last_error_msg() {
- // http://php.net/manual/en/language.variables.scope.php#language.variables.scope.static
- static $ERRORS = array(
- JSON_ERROR_NONE => "No error.",
- JSON_ERROR_DEPTH => "Maximum stack depth exceeded in the JSON file.",
- JSON_ERROR_STATE_MISMATCH => "State mismatch (invalid or malformed JSON).",
- JSON_ERROR_CTRL_CHAR => "Control character error, possibly incorrectly encoded JSON file.",
- JSON_ERROR_SYNTAX => "Import failed. Syntax error in the JSON file.",
- JSON_ERROR_UTF8 => "Malformed UTF-8 characters, possibly incorrectly encoded JSON file."
- );
- $error = json_last_error();
- return isset( $ERRORS[$error] ) ? $ERRORS[$error] : "Unknown error.";
- }
- }
- return json_last_error_msg();
- }
- switch ( $error_code ) {
- case UPLOAD_ERR_INI_SIZE:
- $message = "The uploaded file exceeds the upload_max_filesize directive in php.ini.";
- break;
- case UPLOAD_ERR_FORM_SIZE:
- $message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.";
- break;
- case UPLOAD_ERR_PARTIAL:
- $message = "The uploaded file was only partially uploaded..";
- break;
- case UPLOAD_ERR_NO_FILE:
- $message = "No file was uploaded.";
- break;
- case UPLOAD_ERR_NO_TMP_DIR:
- $message = "Missing a temporary folder.";
- break;
- case UPLOAD_ERR_CANT_WRITE:
- $message = "Failed to write file to disk.";
- break;
- case UPLOAD_ERR_EXTENSION:
- $message = "File upload stopped by extension.";
- break;
- default:
- $message = "File upload stopped by an unrecognized error.";
- break;
- }
- return $message;
- }
- } // End class QuotesLlamaBackup
- // Start the plugin.
- $quotes_llama = new QuotesLlama;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement