| 1 |
<?php |
| 2 |
namespace Wdk\DashWidgets\Widgets; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly. |
| 6 |
} |
| 7 |
|
| 8 |
class WDK_DashWidgets { |
| 9 |
|
| 10 |
protected $data = array(); |
| 11 |
protected $version = 1.0; |
| 12 |
|
| 13 |
/** |
| 14 |
* Initialize the class and set its properties. |
| 15 |
* |
| 16 |
* @since 1.0.0 |
| 17 |
* @param string $plugin_name The name of this plugin. |
| 18 |
* @param string $version The version of this plugin. |
| 19 |
*/ |
| 20 |
public function __construct( ) {} |
| 21 |
|
| 22 |
/** |
| 23 |
* Start Elementor Addon |
| 24 |
*/ |
| 25 |
public function run() { |
| 26 |
|
| 27 |
add_action( 'admin_enqueue_scripts',[ $this, 'enqueue_styles' ]); |
| 28 |
add_action( 'admin_enqueue_scripts',[ $this, 'enqueue_scripts' ]); |
| 29 |
|
| 30 |
$this->includes(); |
| 31 |
$this->add_widgets(); |
| 32 |
|
| 33 |
do_action('wpdirectorykit/dash-widgets/run'); |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Register the stylesheets for the admin area. |
| 39 |
* |
| 40 |
* @since 1.0.0 |
| 41 |
*/ |
| 42 |
public function enqueue_styles() { |
| 43 |
/** |
| 44 |
* This function is provided for demonstration purposes only. |
| 45 |
* |
| 46 |
* An instance of this class should be passed to the run() function |
| 47 |
* defined in Wpdirectorykit_Loader as all of the hooks are defined |
| 48 |
* in that particular class. |
| 49 |
* |
| 50 |
* The Wpdirectorykit_Loader will then create the relationship |
| 51 |
* between the defined hooks and the functions defined in this |
| 52 |
* class. |
| 53 |
*/ |
| 54 |
wp_enqueue_style( 'dash-widgets-statistics-listings', plugin_dir_url( __FILE__ ) . 'assets/css/dash-widgets-statistics-listings.css' ); |
| 55 |
wp_enqueue_style( 'dash-widgets-statistics-earning', plugin_dir_url( __FILE__ ) . 'assets/css/dash-widgets-statistics-earning.css' ); |
| 56 |
wp_enqueue_style( 'dash-widgets-statistics-usage', plugin_dir_url( __FILE__ ) . 'assets/css/dash-widgets-statistics-usage.css' ); |
| 57 |
wp_enqueue_style( 'wdk-dash-widgets-news', plugin_dir_url( __FILE__ ) . 'assets/css/wdk-news.css' ); |
| 58 |
wp_enqueue_style( 'wdk-dash-widgets-list-listings', plugin_dir_url( __FILE__ ) . 'assets/css/wdk-latest-listings.css' ); |
| 59 |
wp_enqueue_style( 'wdk-dash-widgets-map-listings', plugin_dir_url( __FILE__ ) . 'assets/css/wdk-map-listings.css' ); |
| 60 |
wp_enqueue_style( 'wdk-dash-widgets-main', plugin_dir_url( __FILE__ ) . 'assets/css/wdk-main.css' ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Register the JavaScript for the admin area. |
| 65 |
* |
| 66 |
* @since 1.0.0 |
| 67 |
*/ |
| 68 |
public function enqueue_scripts() { |
| 69 |
/** |
| 70 |
* This function is provided for demonstration purposes only. |
| 71 |
* |
| 72 |
* An instance of this class should be passed to the run() function |
| 73 |
* defined in Wpdirectorykit_Loader as all of the hooks are defined |
| 74 |
* in that particular class. |
| 75 |
* |
| 76 |
* The Wpdirectorykit_Loader will then create the relationship |
| 77 |
* between the defined hooks and the functions defined in this |
| 78 |
* class. |
| 79 |
*/ |
| 80 |
|
| 81 |
$params = array( |
| 82 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 83 |
'empty_results' => esc_html__('Loading issue, please check news on our website','wpdirectorykit'), |
| 84 |
); |
| 85 |
|
| 86 |
wp_register_script( 'wdk-dash-widgets-news', plugin_dir_url( __FILE__ ) . 'assets/js/wdk-widget-news.js', array(), $this->version, true ); |
| 87 |
wp_localize_script('wdk-dash-widgets-news', 'script_parameters', $params); |
| 88 |
wp_register_script( 'wdk-dash-widgets-map-listings', plugin_dir_url( __FILE__ ) . 'assets/js/wdk-widget-map-listings.js', array(), $this->version, true ); |
| 89 |
wp_enqueue_script( 'wdk-dash-widgets-main', plugin_dir_url( __FILE__ ) . 'assets/js/wdk-main.js', array(), $this->version, true ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Includes |
| 94 |
* |
| 95 |
* @since 1.0.0 |
| 96 |
* |
| 97 |
* @access private |
| 98 |
*/ |
| 99 |
private function includes() { |
| 100 |
require_once plugin_dir_path( __FILE__ ) . '/dash-widgets-news.php'; |
| 101 |
require_once plugin_dir_path( __FILE__ ) . '/dash-widgets-list-listings.php'; |
| 102 |
require_once plugin_dir_path( __FILE__ ) . '/dash-widgets-map-listings.php'; |
| 103 |
require_once plugin_dir_path( __FILE__ ) . '/dash-widgets-statistics-usage.php'; |
| 104 |
require_once plugin_dir_path( __FILE__ ) . '/dash-widgets-statistics-earning.php'; |
| 105 |
require_once plugin_dir_path( __FILE__ ) . '/dash-widgets-statistics-listings.php'; |
| 106 |
|
| 107 |
do_action('wpdirectorykit/dash-widgets/includes'); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Includes |
| 112 |
* |
| 113 |
* @since 1.0.0 |
| 114 |
* |
| 115 |
* @access private |
| 116 |
*/ |
| 117 |
private function add_widgets() { |
| 118 |
|
| 119 |
add_action('wp_dashboard_setup', array('Wdk\DashWidgets\Widgets\WDK_Dashboard_Widget_News','init') ); |
| 120 |
add_action('wp_dashboard_setup', array('Wdk\DashWidgets\Widgets\WDK_Dashboard_Widget_Latest_Listings','init') ); |
| 121 |
add_action('wp_dashboard_setup', array('Wdk\DashWidgets\Widgets\WDK_Dashboard_Widget_Map_Listings','init') ); |
| 122 |
add_action('wp_dashboard_setup', array('Wdk\DashWidgets\Widgets\WDK_Dashboard_Widget_Stats_Usage','init') ); |
| 123 |
add_action('wp_dashboard_setup', array('Wdk\DashWidgets\Widgets\WDK_Dashboard_Widget_Stats_Earning','init') ); |
| 124 |
add_action('wp_dashboard_setup', array('Wdk\DashWidgets\Widgets\WDK_Dashboard_Widget_Stats_Listings','init') ); |
| 125 |
|
| 126 |
do_action('wpdirectorykit/dash-widgets/add_widgets'); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
/** |
| 131 |
* Hook to wp_dashboard_setup to add the widget. |
| 132 |
*/ |
| 133 |
public static function init() {} |
| 134 |
|
| 135 |
/** |
| 136 |
* Load the widget code |
| 137 |
*/ |
| 138 |
public static function widget() { } |
| 139 |
|
| 140 |
/** |
| 141 |
* Load widget config code. |
| 142 |
* |
| 143 |
* This is what will display when an admin clicks |
| 144 |
*/ |
| 145 |
public static function config() { |
| 146 |
wp_redirect(''); exit; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Gets the options for a widget of the specified name. |
| 151 |
* |
| 152 |
* @param string $widget_id Optional. If provided, will only get options for the specified widget. |
| 153 |
* @return array An associative array containing the widget's options and values. False if no opts. |
| 154 |
*/ |
| 155 |
public static function get_dashboard_widget_options( $widget_id='' ) |
| 156 |
{ |
| 157 |
//Fetch ALL dashboard widget options from the db... |
| 158 |
$opts = get_option( 'dashboard_widget_options' ); |
| 159 |
|
| 160 |
//If no widget is specified, return everything |
| 161 |
if ( empty( $widget_id ) ) |
| 162 |
return $opts; |
| 163 |
|
| 164 |
//If we request a widget and it exists, return it |
| 165 |
if ( isset( $opts[$widget_id] ) ) |
| 166 |
return $opts[$widget_id]; |
| 167 |
|
| 168 |
//Something went wrong... |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Gets one specific option for the specified widget. |
| 174 |
* @param $widget_id |
| 175 |
* @param $option |
| 176 |
* @param null $default |
| 177 |
* |
| 178 |
* @return string |
| 179 |
*/ |
| 180 |
public static function get_dashboard_widget_option( $widget_id, $option, $default=NULL ) { |
| 181 |
|
| 182 |
$opts = self::get_dashboard_widget_options($widget_id); |
| 183 |
|
| 184 |
//If widget opts dont exist, return false |
| 185 |
if ( ! $opts ) |
| 186 |
return false; |
| 187 |
|
| 188 |
//Otherwise fetch the option or use default |
| 189 |
if ( isset( $opts[$option] ) && ! empty($opts[$option]) ) |
| 190 |
return $opts[$option]; |
| 191 |
else |
| 192 |
return ( isset($default) ) ? $default : false; |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Saves an array of options for a single dashboard widget to the database. |
| 198 |
* Can also be used to define default values for a widget. |
| 199 |
* |
| 200 |
* @param string $widget_id The name of the widget being updated |
| 201 |
* @param array $args An associative array of options being saved. |
| 202 |
* @param bool $add_only If true, options will not be added if widget options already exist |
| 203 |
*/ |
| 204 |
public static function update_dashboard_widget_options( $widget_id , $args=array(), $add_only=false ) |
| 205 |
{ |
| 206 |
//Fetch ALL dashboard widget options from the db... |
| 207 |
$opts = get_option( 'dashboard_widget_options' ); |
| 208 |
|
| 209 |
if(!$opts) $opts = array(); |
| 210 |
|
| 211 |
//Get just our widget's options, or set empty array |
| 212 |
$w_opts = ( isset( $opts[$widget_id] ) ) ? $opts[$widget_id] : array(); |
| 213 |
|
| 214 |
if ( $add_only ) { |
| 215 |
//Flesh out any missing options (existing ones overwrite new ones) |
| 216 |
$opts[$widget_id] = array_merge($args,$w_opts); |
| 217 |
} |
| 218 |
else { |
| 219 |
//Merge new options with existing ones, and add it back to the widgets array |
| 220 |
$opts[$widget_id] = array_merge($w_opts,$args); |
| 221 |
} |
| 222 |
|
| 223 |
//Save the entire widgets array back to the db |
| 224 |
return update_option('dashboard_widget_options', $opts); |
| 225 |
} |
| 226 |
|
| 227 |
public static function view($view_file = '', $element = array(), $print = false) |
| 228 |
{ |
| 229 |
if(empty($view_file)) return false; |
| 230 |
$file = false; |
| 231 |
if(is_child_theme() && file_exists(get_stylesheet_directory().'/wpdirectorykit/dash-widgets/views/'.$view_file.'.php')) |
| 232 |
{ |
| 233 |
$file = get_stylesheet_directory().'/wpdirectorykit/dash-widgets/views/'.$view_file.'.php'; |
| 234 |
} |
| 235 |
elseif(file_exists(get_template_directory().'/wpdirectorykit/dash-widgets/views/'.$view_file.'.php')) |
| 236 |
{ |
| 237 |
$file = get_template_directory().'/wpdirectorykit/dash-widgets/views/'.$view_file.'.php'; |
| 238 |
} |
| 239 |
elseif(file_exists(WPDIRECTORYKIT_PATH.'dash-widgets/views/'.$view_file.'.php')) |
| 240 |
{ |
| 241 |
$file = WPDIRECTORYKIT_PATH.'dash-widgets/views/'.$view_file.'.php'; |
| 242 |
} |
| 243 |
|
| 244 |
if($file) |
| 245 |
{ |
| 246 |
extract($element); |
| 247 |
if($print) { |
| 248 |
include $file; |
| 249 |
} else { |
| 250 |
ob_start(); |
| 251 |
include $file; |
| 252 |
return ob_get_clean(); |
| 253 |
} |
| 254 |
} |
| 255 |
else |
| 256 |
{ |
| 257 |
if($print) { |
| 258 |
echo 'View file not found in: '.esc_html(WPDIRECTORYKIT_PATH.'dash-widgets/views/'.$view_file.'.php'); |
| 259 |
} else { |
| 260 |
return 'View file not found in: '.WPDIRECTORYKIT_PATH.'dash-widgets/views/'.$view_file.'.php'; |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
$WDK_DashWidgets = new WDK_DashWidgets( ); |
| 268 |
$WDK_DashWidgets->run(); |
| 269 |
|
| 270 |
?> |