YIT_Plugin_Panel_Sidebar

YIT Plugin Panel Sidebar.

Defined (1)

The class is defined in the following location(s).

/plugin-fw/lib/yit-plugin-panel-sidebar.php  
  1. class YIT_Plugin_Panel_Sidebar { 
  2. /** 
  3. * @var string version of class 
  4. */ 
  5. public $version = '1.0.1'; 
  6.  
  7.  
  8. /** 
  9. * @var array array of widgets 
  10. */ 
  11. public $widgets = array(); 
  12.  
  13. /** 
  14. * @type string 
  15. */ 
  16. protected $_remote_widget_xml = 'http://bit.ly/1UJ83xN'; 
  17.  
  18. /** 
  19. * default priority for Remote Widgets 
  20. * @type int 
  21. */ 
  22. public $default_remote_widget_priority = 40; 
  23.  
  24. /** 
  25. * parent panel 
  26. * @var YIT_Plugin_Panel 
  27. */ 
  28. public $panel; 
  29.  
  30. private $_is_collapsed; 
  31.  
  32. /** 
  33. * @type string 
  34. */ 
  35. public static $transient_remote_widgets = 'yit_panel_sidebar_remote_widgets'; 
  36.  
  37. /** 
  38. * @type string 
  39. */ 
  40. public static $transient_updated_remote_widgets = 'yit_panel_sidebar_remote_widgets_update'; 
  41.  
  42. /** 
  43. * @type string 
  44. */ 
  45. public static $collapse_option = 'yith_plugin_panel_sidebar_collapse'; 
  46.  
  47. /** 
  48. * Constructor 
  49. * @param YIT_Plugin_Panel $panel the parent panel 
  50. * @since 1.0 
  51. * @author Leanza Francesco <leanzafrancesco@gmail.com> 
  52. */ 
  53. private function __construct( $panel ) { 
  54. $this->panel = $panel; 
  55.  
  56. /** load and sort default widgets and remote widgets */ 
  57. $this->load_widgets(); 
  58.  
  59. add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); 
  60. add_filter( 'yit_admin_panel_content_class', array( $this, 'filter_admin_panel_content_class' ) ); 
  61.  
  62. /** 
  63. * get instance 
  64. * @static 
  65. * @return YIT_Plugin_Panel_Sidebar 
  66. * @since 1.0 
  67. * @author Leanza Francesco <leanzafrancesco@gmail.com> 
  68. */ 
  69. public static function instance( $panel ) { 
  70. return new self( $panel ); 
  71.  
  72. /** 
  73. * return true if is collapsed by option 
  74. * @return bool 
  75. */ 
  76. public function is_collapsed() { 
  77. if ( !isset( $this->_is_collapsed ) ) { 
  78. $this->_is_collapsed = get_option( self::$collapse_option, 'no' ) === 'yes'; 
  79.  
  80. return $this->_is_collapsed; 
  81.  
  82. /** 
  83. * if is collapsed add a class to panel wrapper 
  84. * @param $class 
  85. * @return string 
  86. */ 
  87. public function filter_admin_panel_content_class( $class ) { 
  88. if ( $this->is_collapsed() ) { 
  89. $class .= ' yit-admin-panel-content-wrap-full'; 
  90.  
  91. return $class; 
  92.  
  93. /** 
  94. * Add one or more widgets to $this->widgets 
  95. * @param array $widgets 
  96. */ 
  97. public function add_widgets( $widgets ) { 
  98. $this->widgets = array_merge( $this->widgets, $widgets ); 
  99.  
  100. /** 
  101. * delete transients 
  102. */ 
  103. public static function delete_transients() { 
  104. delete_transient( self::$transient_remote_widgets ); 
  105. delete_transient( self::$transient_updated_remote_widgets ); 
  106.  
  107. /** 
  108. * filter and sort widgets 
  109. */ 
  110. private function _filter_and_sort_widgets() { 
  111. /** filter widgets */ 
  112. $page_name = isset( $this->panel->settings[ 'page' ] ) ? $this->panel->settings[ 'page' ] : ''; 
  113. $this->widgets = apply_filters( 'yit_plugin_panel_sidebar_widgets', $this->widgets, $page_name ); 
  114.  
  115. /**sort widgets*/ 
  116. uasort( $this->widgets, array( $this, 'sort_widgets' ) ); 
  117.  
  118. /** 
  119. * get Remote Widget by XML from YIThemes 
  120. * @return array 
  121. * @since 1.0 
  122. * @author Leanza Francesco <leanzafrancesco@gmail.com> 
  123. */ 
  124. public function get_remote_widgets() { 
  125. $load_remote_widgets = apply_filters( 'yit_panel_sidebar_load_remote_widgets', true ); 
  126. if ( !$load_remote_widgets ) 
  127. return array(); 
  128.  
  129. $remote_widgets = get_transient( self::$transient_remote_widgets ); 
  130. $updated = get_transient( self::$transient_updated_remote_widgets ); 
  131. $is_debug = defined( 'YIT_FW_REMOTE_WIDGETS_DEBUG' ) && YIT_FW_REMOTE_WIDGETS_DEBUG; 
  132.  
  133. if ( $is_debug || $updated === false || $remote_widgets === false ) { 
  134. $remote_widgets = array(); 
  135. } else { 
  136. return $remote_widgets; 
  137.  
  138. $expiration = 1 * DAY_IN_SECONDS; 
  139. $updated_expiration = DAY_IN_SECONDS; // update frequency 
  140.  
  141. $remote_xml = wp_remote_get( $this->_remote_widget_xml ); 
  142. if ( !is_wp_error( $remote_xml ) && isset( $remote_xml[ 'response' ][ 'code' ] ) && '200' == $remote_xml[ 'response' ][ 'code' ] && class_exists( 'SimpleXmlElement' ) ) { 
  143. try { 
  144. // suppress all XML errors when loading the document 
  145. libxml_use_internal_errors( true ); 
  146.  
  147. $xml_data = new SimpleXmlElement( $remote_xml[ 'body' ] ); 
  148. $xml_remote_widgets = isset( $xml_data->widget ) ? $xml_data->widget : array(); 
  149.  
  150. $enabled_args = array( 
  151. 'title',  
  152. 'icon',  
  153. 'content',  
  154. 'class',  
  155. 'title_class',  
  156. 'badge',  
  157. 'badge_text',  
  158. 'image',  
  159. 'image_class',  
  160. 'priority',  
  161. 'starting',  
  162. 'expiration',  
  163. ); 
  164.  
  165. $last_remote_priority = $this->default_remote_widget_priority; 
  166.  
  167. foreach ( $xml_remote_widgets as $xml_widget ) { 
  168. if ( !isset( $xml_widget->id ) ) 
  169. continue; 
  170.  
  171. $widget_id = (string) $xml_widget->id; 
  172. $widget_array = array(); 
  173. foreach ( $enabled_args as $key ) { 
  174. if ( isset( $xml_widget->$key ) ) { 
  175. $widget_array[ $key ] = (string) $xml_widget->$key; 
  176. } else { 
  177. if ( $key == 'priority' ) { 
  178. $widget_array[ $key ] = $last_remote_priority; 
  179. $last_remote_priority += 10; 
  180. $remote_widgets[ $widget_id ] = $widget_array; 
  181.  
  182. $xml_expiration = isset( $xml_data->expiration ) ? (string) $xml_data->expiration : ''; 
  183. if ( !empty( $xml_expiration ) ) { 
  184. $expiration = strtotime( $xml_expiration ) - strtotime( 'now' ); 
  185. // if the XML is expired removes widgets 
  186. if ( $expiration < 1 ) 
  187. $remote_widgets = array(); 
  188.  
  189. $is_urgent = isset( $xml_data->urgent ) ? !!$xml_data->urgent : false; 
  190. $is_urgent = true; 
  191. if ( !$is_urgent ) { 
  192. $four_days_random = mt_rand( 0, 4 * DAY_IN_SECONDS ); 
  193. $expiration += $four_days_random; 
  194. } else { 
  195. /** 
  196. * - - - - - U R G E N T - - - - - 
  197. * it will be updated the exact day, BUT in different time! :) 
  198. * [to prevent too many request at the same time] 
  199. */ 
  200. $one_day_random = mt_rand( 0, DAY_IN_SECONDS ); 
  201. $expiration += $one_day_random; 
  202.  
  203. $four_days_random = mt_rand( 0, 4 * DAY_IN_SECONDS ); 
  204.  
  205. /** to prevent multiple request if it's expired */ 
  206. if ( $expiration < 1 ) { 
  207. $expiration = 1 * DAY_IN_SECONDS + $four_days_random; 
  208. } catch ( Exception $e ) { 
  209.  
  210.  
  211. //$updated_expiration = 30 * DAY_IN_SECONDS + $four_days_random; 
  212.  
  213. set_transient( self::$transient_remote_widgets, $remote_widgets, $expiration ); 
  214. set_transient( self::$transient_updated_remote_widgets, true, $updated_expiration ); 
  215.  
  216. return $remote_widgets; 
  217.  
  218. /** 
  219. * load and sort default widgets and remote widgets 
  220. * @since 1.0 
  221. * @author Leanza Francesco <leanzafrancesco@gmail.com> 
  222. */ 
  223. public function load_widgets() { 
  224. /** get static widgets */ 
  225. $this->widgets = include( YIT_CORE_PLUGIN_TEMPLATE_PATH . '/panel/sidebar/widgets/widgets.php' ); 
  226.  
  227. /** get remote widgets */ 
  228. $remote_widgets = $this->get_remote_widgets(); 
  229. $this->add_widgets( $remote_widgets ); 
  230.  
  231. /** 
  232. * Print the panel sidebar 
  233. * @return void 
  234. * @since 1.0 
  235. * @author Leanza Francesco <leanzafrancesco@gmail.com> 
  236. */ 
  237. public function print_panel_sidebar() { 
  238. do_action( 'yit_panel_before_sidebar' ); 
  239.  
  240. include( YIT_CORE_PLUGIN_TEMPLATE_PATH . '/panel/sidebar/sidebar.php' ); 
  241.  
  242. do_action( 'yit_panel_after_sidebar' ); 
  243.  
  244. /** 
  245. * Print the panel sidebar widgets 
  246. * @return void 
  247. * @since 1.0 
  248. * @author Leanza Francesco <leanzafrancesco@gmail.com> 
  249. */ 
  250. public function print_panel_sidebar_widgets() { 
  251. $basename = YIT_CORE_PLUGIN_PATH; 
  252. $path = '/panel/sidebar/widget.php'; 
  253.  
  254. $default_args = array( 
  255. 'id' => '',  
  256. 'title' => '',  
  257. 'icon' => '',  
  258. 'content' => '',  
  259. 'class' => '',  
  260. 'title_class' => '',  
  261. 'template' => '',  
  262. 'badge' => '',  
  263. 'badge_text' => '',  
  264. 'image' => '',  
  265. 'image_class' => '',  
  266. 'args' => array(),  
  267. 'hide_if_empty_args' => '',  
  268. 'priority' => 10,  
  269. 'starting' => '',  
  270. 'expiration' => '',  
  271. ); 
  272.  
  273. $this->_filter_and_sort_widgets(); 
  274.  
  275. foreach ( $this->widgets as $widget_id => $widget ) { 
  276. $args = array_merge( $widget, array( 'id' => $widget_id ) ); 
  277. $args = wp_parse_args( $args, $default_args ); 
  278.  
  279. $is_started = empty( $args[ 'starting' ] ) || strtotime( $args[ 'starting' ] . ' midnight' ) <= strtotime( 'midnight' ); 
  280. $is_expired = !empty( $args[ 'expiration' ] ) && strtotime( $args[ 'expiration' ] . ' midnight' ) < strtotime( 'midnight' ); 
  281.  
  282. if ( $is_expired || !$is_started ) 
  283. continue; 
  284.  
  285. if ( !empty( $args[ 'hide_if_empty_args' ] ) ) { 
  286. $hide_if_empty_args = $args[ 'hide_if_empty_args' ]; 
  287. $continue = false; 
  288. foreach ( $hide_if_empty_args as $hide_if_empty_arg ) { 
  289. if ( empty( $args[ 'args' ][ $hide_if_empty_arg ] ) ) { 
  290. $continue = true; 
  291. break; 
  292. if ( $continue ) 
  293. continue; 
  294.  
  295. do_action( 'yit_panel_sidebar_before_widget', $widget_id, $widget ); 
  296.  
  297. yit_plugin_get_template( $basename, $path, $args ); 
  298.  
  299. do_action( 'yit_panel_sidebar_after_widget', $widget_id, $widget ); 
  300.  
  301. /** 
  302. * set transient for first activation 
  303. * to prevent too many calls to YIThemes 
  304. */ 
  305. public static function set_transient_for_first_activation() { 
  306. $remote_widgets = get_transient( self::$transient_remote_widgets ); 
  307. $updated = get_transient( self::$transient_updated_remote_widgets ); 
  308.  
  309. $first_activation = $updated === false && $remote_widgets === false; 
  310. if ( $first_activation ) { 
  311. $seven_days_random = mt_rand( 0, 7 * DAY_IN_SECONDS ); 
  312. $expiration = 1 * DAY_IN_SECONDS + $seven_days_random; 
  313.  
  314. set_transient( self::$transient_remote_widgets, array(), $expiration ); 
  315. set_transient( self::$transient_updated_remote_widgets, true, $expiration ); 
  316.  
  317. /** 
  318. * Sort widgets by priority 
  319. * @param $a 
  320. * @param $b 
  321. * @return bool 
  322. * @since 1.0 
  323. * @author Leanza Francesco <leanzafrancesco@gmail.com> 
  324. */ 
  325. public function sort_widgets( $a, $b ) { 
  326. $priority_a = isset( $a[ 'priority' ] ) ? intval( $a[ 'priority' ] ) : $this->default_remote_widget_priority; 
  327. $priority_b = isset( $b[ 'priority' ] ) ? intval( $b[ 'priority' ] ) : $this->default_remote_widget_priority; 
  328. if ( $priority_a == $priority_b ) { 
  329. return 0; 
  330. } elseif ( $priority_a > $priority_b ) { 
  331. return 1; 
  332. } else { 
  333. return -1; 
  334.  
  335. /** 
  336. * Add Admin WC Style and Scripts 
  337. * @return void 
  338. * @since 1.0 
  339. * @author Leanza Francesco <leanzafrancesco@gmail.com 
  340. */ 
  341. public function admin_enqueue_scripts() { 
  342. $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; 
  343.  
  344. wp_enqueue_style( 'opensans-font', '//fonts.googleapis.com/css?family=Open+Sans:400, 500, 600, 700, 800, 100, 200, 300, 900' ); 
  345. wp_enqueue_style( 'yit-plugin-sidebar-style', YIT_CORE_PLUGIN_URL . '/assets/css/yit-plugin-panel-sidebar.css', $this->version ); 
  346. wp_enqueue_script( 'yit-plugin-sidebar-js', YIT_CORE_PLUGIN_URL . '/assets/js/yit-plugin-panel-sidebar' . $min . '.js', array( 'jquery' ), $this->version, true ); 
  347. wp_localize_script( 'yit-plugin-sidebar-js', 'sidebar_labels', array( 
  348. 'hide_sidebar' => __( 'Hide sidebar', 'yith-plugin-fw' ),  
  349. 'show_sidebar' => __( 'Show sidebar', 'yith-plugin-fw' ),  
  350. 'wrapper_class' => 'yit-admin-panel-content-wrap',  
  351. ) );