* @license GPL-2.0+ * @link http://www.aeroleads.com * @copyright 2015 Anurag Singh * */ // Prevent direct file access if ( ! defined ( 'ABSPATH' ) ) { exit; } // TODO: change 'Widget_Name' to the name of your plugin class Alcud_Widget extends WP_Widget { /** * @TODO - Rename "widget-name" to the name your your widget * * Unique identifier for your widget. * * * The variable name is used as the text domain when internationalizing strings * of text. Its value should match the Text Domain file header in the main * widget file. * * @since 1.0.0 * * @var string */ protected $widget_slug = 'alcud-widget'; /*--------------------------------------------------*/ /* Constructor /*--------------------------------------------------*/ /** * Specifies the classname and description, instantiates the widget, * loads localization files, and includes necessary stylesheets and JavaScript. */ public function __construct() { // load plugin text domain add_action( 'init', array( $this, 'widget_textdomain' ) ); // Hooks fired when the Widget is activated and deactivated register_activation_hook( __FILE__, array( $this, 'activate' ) ); register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) ); // TODO: update description parent::__construct( $this->get_widget_slug(), __( 'AeroLeads Contact Us Details', $this->get_widget_slug() ), array( 'classname' => $this->get_widget_slug().'-class', 'description' => __( 'AL-CUD displays contact details in sidebar', $this->get_widget_slug() ) ) ); // Register admin styles and scripts add_action( 'admin_print_styles', array( $this, 'register_admin_styles' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_scripts' ) ); // Register site styles and scripts add_action( 'wp_enqueue_scripts', array( $this, 'register_widget_styles' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'register_widget_scripts' ) ); // Refreshing the widget's cached output with each new post add_action( 'save_post', array( $this, 'flush_widget_cache' ) ); add_action( 'deleted_post', array( $this, 'flush_widget_cache' ) ); add_action( 'switch_theme', array( $this, 'flush_widget_cache' ) ); } // end constructor /** * Return the widget slug. * * @since 1.0.0 * * @return Plugin slug variable. */ public function get_widget_slug() { return $this->widget_slug; } /*--------------------------------------------------*/ /* Widget API Functions /*--------------------------------------------------*/ /** * Outputs the content of the widget. * * @param array args The array of form elements * @param array instance The current instance of the widget */ public function widget( $args, $instance ) { // Check if there is a cached output $cache = wp_cache_get( $this->get_widget_slug(), 'widget' ); if ( !is_array( $cache ) ) $cache = array(); if ( ! isset ( $args['widget_id'] ) ) $args['widget_id'] = $this->id; if ( isset ( $cache[ $args['widget_id'] ] ) ) return print $cache[ $args['widget_id'] ]; // go on with your widget logic, put everything into a string and … global $wpdb; $table_name = $wpdb->prefix . "alcud"; $detail = $wpdb->get_row( "SELECT * FROM $table_name WHERE id = 1"); $business_name = ""; $contact_person = ""; $email_address = ""; $contact_number = ""; $address = ""; $open_hours = ""; $note = ""; if($detail){ $business_name = $detail->business_name; $contact_person = $detail->contact_person; $email_address = $detail->email_address; $contact_number = $detail->contact_number; $address = $detail->address; $open_hours = $detail->open_hours; $note = $detail->note; } global $wpdb; $table_name = $wpdb->prefix . "alcud_options"; $active_widget = $wpdb->get_var( "SELECT widget_type FROM $table_name WHERE id = 1"); extract( $args, EXTR_SKIP ); $title = apply_filters('widget_title', $instance['title']); $widget_string = $before_widget; // TODO: Here is where you manipulate your widget's values based on their input fields ob_start(); if($active_widget == "1"){ include( plugin_dir_path( dirname( __FILE__ ) ) . 'widget/views/widget.php' ); } if($active_widget == "2"){ include( plugin_dir_path( dirname( __FILE__ ) ) . 'widget/views/widget2.php' ); } $widget_string .= ob_get_clean(); $widget_string .= $after_widget; $cache[ $args['widget_id'] ] = $widget_string; wp_cache_set( $this->get_widget_slug(), $cache, 'widget' ); print $widget_string; } // end widget public function flush_widget_cache() { wp_cache_delete( $this->get_widget_slug(), 'widget' ); } /** * Processes the widget's options to be saved. * * @param array new_instance The new instance of values to be generated via the update. * @param array old_instance The previous instance of values before the update. */ public function update( $new_instance, $old_instance ) { $instance = $old_instance; // TODO: Here is where you update your widget's old values with the new, incoming values $instance['title'] = strip_tags($new_instance['title']); return $instance; } // end widget /** * Generates the administration form for the widget. * * @param array instance The array of keys and values for the widget. */ public function form( $instance ) { // TODO: Define default values for your variables $instance = wp_parse_args( (array) $instance ); if( $instance) { $title = esc_attr($instance['title']); } else { $title = ""; } // TODO: Store the values of the widget in their own variable // Display the admin form include( plugin_dir_path( dirname( __FILE__ ) ) . 'widget/views/admin.php' ); } // end form /*--------------------------------------------------*/ /* Public Functions /*--------------------------------------------------*/ /** * Loads the Widget's text domain for localization and translation. */ public function widget_textdomain() { // TODO be sure to change 'widget-name' to the name of *your* plugin load_plugin_textdomain( $this->get_widget_slug(), false, plugin_dir_path( dirname( __FILE__ ) ) . 'widget/lang/' ); } // end widget_textdomain /** * Fired when the plugin is activated. * * @param boolean $network_wide True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog. */ public function activate( $network_wide ) { // TODO define activation functionality here } // end activate /** * Fired when the plugin is deactivated. * * @param boolean $network_wide True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog */ public function deactivate( $network_wide ) { // TODO define deactivation functionality here } // end deactivate /** * Registers and enqueues admin-specific styles. */ public function register_admin_styles() { wp_enqueue_style( $this->get_widget_slug().'-admin-styles', plugins_url( 'widget/css/admin.css', dirname( __FILE__ ) ) ); } // end register_admin_styles /** * Registers and enqueues admin-specific JavaScript. */ public function register_admin_scripts() { wp_enqueue_script( $this->get_widget_slug().'-admin-script', plugins_url( 'widget/js/admin.js', dirname( __FILE__ ) ), array('jquery') ); } // end register_admin_scripts /** * Registers and enqueues widget-specific styles. */ public function register_widget_styles() { wp_enqueue_style( $this->get_widget_slug().'-widget-styles', plugins_url( 'widget/css/widget.css', dirname( __FILE__ ) ) ); } // end register_widget_styles /** * Registers and enqueues widget-specific scripts. */ public function register_widget_scripts() { wp_enqueue_script( $this->get_widget_slug(). '-admin-script-serializeJSON', plugins_url( 'admin/js/jquery_serializeJSON.js', dirname( __FILE__ ) ), array( 'jquery' ) ); wp_enqueue_script( $this->get_widget_slug().'-script', plugins_url( 'widget/js/widget.js', dirname( __FILE__ ) ), array('jquery') ); } // end register_widget_scripts } // end class // TODO: Remember to change 'Widget_Name' to match the class name definition add_action( 'widgets_init', create_function( '', 'register_widget("Alcud_Widget");' ) );