Layers_Widgets

Widget Initiation File.

Defined (1)

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

/core/widgets/init.php  
  1. class Layers_Widgets { 
  2.  
  3. private static $instance; 
  4.  
  5. /** 
  6. * Initiator 
  7. */ 
  8.  
  9. public static function init() { 
  10. return self::$instance; 
  11.  
  12. /** 
  13. * Constructor 
  14. */ 
  15.  
  16. public function __construct() { 
  17.  
  18. global $wp_customize; 
  19.  
  20. // Setup some folder variables 
  21. $widget_dir = '/core/widgets/'; 
  22. $module_dir = '/core/widgets/modules/'; 
  23.  
  24. // Setup some defined variables to use in each widget 
  25. define( 'LAYERS_WIDGET_WIDTH_TINY' , 500 ); 
  26. define( 'LAYERS_WIDGET_WIDTH_SMALL' , 660 ); 
  27. define( 'LAYERS_WIDGET_WIDTH_LARGE' , 980 ); 
  28.  
  29. // Include ajax functions 
  30. require_once get_template_directory() . $widget_dir . 'ajax.php'; 
  31.  
  32. // Include necessary widgets 
  33. require_once get_template_directory() . $module_dir . 'base.php'; // Basis of all Layers Widgets 
  34. require_once get_template_directory() . $module_dir . 'contact.php'; 
  35. require_once get_template_directory() . $module_dir . 'content.php'; 
  36. require_once get_template_directory() . $module_dir . 'post.php'; 
  37. require_once get_template_directory() . $module_dir . 'slider.php'; 
  38.  
  39. // Enqueue Styles 
  40. add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) , 50 ); 
  41. add_action( 'admin_enqueue_scripts', array( $this, 'admin_print_styles' ) , 50 ); 
  42. add_action( 'customize_controls_print_styles' , array( $this, 'admin_print_styles' ) ); 
  43.  
  44. //Backup widgets pre-theme switch 
  45. add_action( 'pre_set_theme_mod_sidebars_widgets', 'layers_backup_sidebars_widgets' ); 
  46.  
  47. // Add a widget backup function 
  48. add_action( 'customize_save_after', 'layers_backup_sidebars_widgets' , 50 ); 
  49. add_action( 'delete_post', array( $this, 'clear_page_widgets' ), 0 ); 
  50. add_action( 'wp_restore_post_revision' , array( $this, 'restore_backup' ), 10, 2 ); 
  51. add_action( 'init', array( $this, 'check_for_revisions' ), 50 ); 
  52.  
  53.  
  54. if( !isset( $wp_customize ) && !( isset( $_REQUEST['action'] ) && ( 'restore' == $_REQUEST['action'] || 'customize_save' == $_REQUEST['action'] ) ) ) { 
  55. add_action( 'save_post', array( $this, 'save_revision_fields_on_save' ), 10, 2 ); 
  56.  
  57. add_filter( '_wp_post_revision_fields', array( $this, 'add_revision_fields' ), TRUE ); 
  58. add_filter( '_wp_post_revision_field_widget_order', array( $this, 'add_widget_order_field' ), 10, 2 ); 
  59.  
  60. // Register Sidebars 
  61. $this->register_sidebars(); 
  62. $this->register_dynamic_sidebars(); 
  63.  
  64. /** 
  65. * Register Sidebars 
  66. */ 
  67.  
  68. public function register_sidebars() { 
  69. global $wp_customize , $temp_sidebars; 
  70.  
  71. // Loop the Builder Pages and create their sidebars 
  72. foreach( layers_get_builder_pages() as $page) { 
  73. $this->register_builder_sidebar( $page->ID, $page->post_title ); 
  74.  
  75. /** 
  76. * Register Builder Sidebar Function 
  77. */ 
  78.  
  79. public static function register_builder_sidebar( $post_id = 0, $post_title = '' ) { 
  80. register_sidebar( array( 
  81. 'id' => 'obox-layers-builder-' . $post_id,  
  82. 'name' => $post_title . __( ' Body' , 'layerswp' ),  
  83. 'before_widget' => '<div id="%1$s" class="widget container push-bottom-medium %2$s">',  
  84. 'after_widget' => '</div>',  
  85. 'before_title' => '<div class="section-title clearfix"><h4 class="heading">',  
  86. 'after_title' => '</h4></div>',  
  87. ) ); 
  88.  
  89. /** 
  90. * Get Dynamic Widget Areas 
  91. */ 
  92.  
  93. public function get_dynamic_sidebars() { 
  94.  
  95. // Set the widget ID to search for 
  96. $dynamic_widget_id = 'layers-widget-sidebar'; 
  97.  
  98. // Get registered sidebars 
  99. $sidebars = get_option( 'sidebars_widgets'); 
  100.  
  101. // Make sure side bars & widgets option is not empty 
  102. if( empty( $sidebars ) ) return; 
  103.  
  104. // Get the Dynamic Sidebar Widgets in use 
  105. $dynamic_widget_areas = get_option( 'widget_' . $dynamic_widget_id ); 
  106.  
  107. // If there are no sidebars to register, return; 
  108. if( empty( $dynamic_widget_areas ) ) return; 
  109.  
  110. $dynamic_sidebars = array(); 
  111.  
  112. // Loop over the Dynamic Sidebar Widgets 
  113. foreach ( $dynamic_widget_areas as $widget_key => $widget_area ) { 
  114.  
  115. // Check if this widget is inside an inactive sidebar (in which case skip) 
  116. if( !in_array( $dynamic_widget_id . '-' . $widget_key , $sidebars[ 'wp_inactive_widgets'] ) ) { 
  117. if( isset( $widget_area['sidebars'] ) ) { 
  118. foreach ( $widget_area['sidebars'] as $sidebar_key => $sidebar ) { 
  119. $sidebar_id = $dynamic_widget_id .'-' . $widget_key . '-' . $sidebar_key; 
  120.  
  121. $dynamic_sidebars[] = array( 
  122. 'id' => $sidebar_id,  
  123. 'title' => $sidebar[ 'title' ] 
  124. ); 
  125.  
  126. } // foreach $widget_area['modules'] 
  127. } // if isset $widget_area['modules'] 
  128. } // if !in_array( $dynamic_widget_id ) 
  129. } // foreach $dynamic_widget_areas 
  130.  
  131. return $dynamic_sidebars; 
  132.  
  133. /** 
  134. * Create Dynamic Widget Areas 
  135. */ 
  136.  
  137. public function register_dynamic_sidebars() { 
  138.  
  139. $dynamic_sidebars = $this->get_dynamic_sidebars(); 
  140.  
  141. if( empty( $dynamic_sidebars ) ) return; 
  142.  
  143. foreach( $dynamic_sidebars as $dynamic_sidebar ) { 
  144. register_sidebar( array( 
  145. 'id' => $dynamic_sidebar[ 'id' ],  
  146. 'name' => $dynamic_sidebar[ 'title' ],  
  147. 'description' => __( 'Layers Builder section.' , 'layerswp' ),  
  148. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',  
  149. 'after_widget' => '</aside>',  
  150. 'before_title' => '<h4 class="widget-title">',  
  151. 'after_title' => '</h4>',  
  152. ) ); 
  153.  
  154.  
  155. /** 
  156. * Clear page widgets (when deleting a post, to keep a clean db) 
  157. */ 
  158.  
  159. public function clear_page_widgets( $post_id ) { 
  160.  
  161. $layers_sidebar_key = 'obox-layers-builder-' . $post_id; 
  162.  
  163. if( class_exists( 'Layers_Widget_Migrator' ) ) { 
  164. $migrator = new Layers_Widget_Migrator(); 
  165. $migrator->clear_page_sidebars_widget( $layers_sidebar_key ); 
  166.  
  167. public function add_revision_fields( $fields ) { 
  168. global $current_screen; 
  169.  
  170. if( !( is_object( $current_screen) && 'revision' == $current_screen->base && !isset( $_GET[ 'action' ] ) ) ) { 
  171. $fields['post_content_filtered'] = __( 'Raw Page Data', 'layerswp' ); 
  172.  
  173. $new_fields = array(); 
  174. foreach( $fields as $f_key => $f_value ) { 
  175. if( $f_key == 'post_content' ) { 
  176. $new_fields['_layers_widget_order'] = __( 'Widget Order', 'layerswp' ); 
  177. $new_fields[ $f_key ] = $f_value; 
  178.  
  179. return $new_fields; 
  180.  
  181. /** 
  182. * Add widget order meta field 
  183. */ 
  184.  
  185. public function add_widget_order_field( $value, $field ) { 
  186.  
  187. global $revision; 
  188. if( is_object( $revision ) ) 
  189. return get_metadata( 'post', $revision->ID, '_layers_widget_order', true ); 
  190.  
  191. /** 
  192. * Add special revision meta fields when updating pages through Page > Edit 
  193. */ 
  194.  
  195. function save_revision_fields_on_save( $post_id, $post ) { 
  196.  
  197. $parent_id = wp_is_post_revision( $post_id ); 
  198.  
  199. if ( $parent_id ) { 
  200.  
  201. $parent = get_post( $parent_id ); 
  202.  
  203. if( class_exists( 'Layers_Widget_Migrator' ) ) { 
  204.  
  205. $migrator = new Layers_Widget_Migrator(); 
  206. $export_data = $migrator->page_widget_data( $parent ); 
  207.  
  208. if ( false !== $export_data ) 
  209. add_metadata( 'post', $post_id, '_layers_widget_order', $migrator->page_widgets_as_content( $export_data ) ); 
  210.  
  211.  
  212. /** 
  213. * Restore a Backup from the Layers Widget Revisions 
  214. */ 
  215.  
  216. public function restore_backup( $post_id, $revision_id) { 
  217.  
  218. $post = get_post( $post_id, OBJECT ); 
  219.  
  220. if( 'page' == $post->post_type && LAYERS_BUILDER_TEMPLATE !== get_post_meta( $post_id, '_wp_page_template', true ) ) return; 
  221.  
  222. // Get the revision information 
  223. $revision = get_post( $revision_id, OBJECT ); 
  224.  
  225. $layers_migrator = new Layers_Widget_Migrator(); 
  226.  
  227. $widget_data = $revision->post_content_filtered; 
  228.  
  229. if( is_wp_error( unserialize( $widget_data ) ) ) return; 
  230.  
  231. // Check for errors. 
  232. if ( '' == $widget_data || is_wp_error( unserialize( $widget_data ) ) ) return; 
  233.  
  234. $widget_data_array = unserialize( $widget_data ); 
  235.  
  236.  
  237. // Check if our data is empty. 
  238. if ( empty( $widget_data_array ) ) return; 
  239.  
  240. $import = $layers_migrator->import( unserialize( $widget_data ), TRUE, TRUE ); 
  241.  
  242. // Update widget order on the post 
  243. $widget_order = get_metadata( 'post', $revision_id, '_layers_widget_order', true ); 
  244.  
  245. $latest_revisions = wp_get_post_revisions( $post_id, array( 'post_type' => 'page' ) ); 
  246. $latest_revision = reset( $latest_revisions ); 
  247.  
  248.  
  249. if ( false !== $widget_order ) { 
  250. add_metadata( 'post', $latest_revision->ID, '_layers_widget_order', $widget_order ); 
  251. update_post_meta( $post_id, '_layers_widget_order', $widget_order ); 
  252.  
  253.  
  254. public function check_for_revisions() { 
  255.  
  256. global $wp_customize; 
  257.  
  258. if( get_option( 'layers_init_revisions' ) ) return; 
  259.  
  260. // Get a list of the migrator 
  261. $get_layers_pages = layers_get_builder_pages( 500 ); 
  262.  
  263. if( empty( $get_layers_pages ) ) return; 
  264.  
  265. $revisions_exist = FALSE; 
  266.  
  267. // Loop through the builder pages spooling up the widget data each time 
  268. foreach( $get_layers_pages as $page ) { 
  269.  
  270. if( '' !== get_post_meta( $page->ID, '_layers_hash', true ) ) { 
  271. $revisions_exist = TRUE; 
  272.  
  273. if( ! $revisions_exist ) { 
  274. add_filter( '_wp_post_revision_fields', array( $this, 'add_revision_fields' ) ); 
  275.  
  276. layers_backup_sidebars_widgets( TRUE ); 
  277.  
  278. add_option( 'layers_init_revisions', TRUE ); 
  279.  
  280.  
  281.  
  282. /** 
  283. * Enqueue Widget Scripts 
  284. */ 
  285.  
  286. public function admin_enqueue_scripts() { 
  287.  
  288. // Banner Widget 
  289. wp_register_script( 
  290. LAYERS_THEME_SLUG . '-admin-slider-widget' ,  
  291. get_template_directory_uri() . '/core/widgets/js/slider.js' ,  
  292. array(),  
  293. LAYERS_VERSION,  
  294. true 
  295. ); 
  296. wp_localize_script( LAYERS_THEME_SLUG . '-admin-slider-widget' , 'sliderwidgeti18n', array( 
  297. 'confirm_message' => __( 'Are you sure you want to remove this slide?' , 'layerswp' ) 
  298. ) ); 
  299.  
  300. // Content Widget 
  301. wp_register_script( 
  302. LAYERS_THEME_SLUG . '-admin-content-widget' ,  
  303. get_template_directory_uri() . '/core/widgets/js/content.js' ,  
  304. array(),  
  305. LAYERS_VERSION,  
  306. true 
  307. ); 
  308. wp_localize_script( LAYERS_THEME_SLUG . '-admin-content-widget' , 'contentwidgeti18n', array( 
  309. 'confirm_message' => __( 'Are you sure you want to remove this column?' , 'layerswp' ) 
  310. ) ); 
  311.  
  312. // Repeatable Widget 
  313. wp_register_script( 
  314. LAYERS_THEME_SLUG . '-admin-repeater-widget' ,  
  315. get_template_directory_uri() . '/core/widgets/js/repeater.js' ,  
  316. array(),  
  317. LAYERS_VERSION,  
  318. true 
  319. ); 
  320. wp_localize_script( LAYERS_THEME_SLUG . '-admin-repeater-widget' , 'repeateri18n', array( 
  321. 'confirm_message' => __( 'Are you sure you want to remove this column?' , 'layerswp' ),  
  322. 'duplicate_text' => __( 'Duplicate' , 'layerswp' ),  
  323. 'edit_text' => __( 'Edit' , 'layerswp' ),  
  324. ) ); 
  325. wp_enqueue_script( LAYERS_THEME_SLUG . '-admin-repeater-widget' ); 
  326.  
  327. // Widget accordians 
  328. wp_enqueue_script( 
  329. LAYERS_THEME_SLUG . '-admin-widgets' ,  
  330. get_template_directory_uri() . '/core/widgets/js/widget-accordians.js' ,  
  331. array( 
  332. LAYERS_THEME_SLUG . '-admin-slider-widget',  
  333. LAYERS_THEME_SLUG . '-admin-content-widget',  
  334. 'backbone',  
  335. 'jquery',  
  336. 'wp-color-picker',  
  337. 'media-upload' 
  338. ),  
  339. LAYERS_VERSION,  
  340. true 
  341. ); 
  342.  
  343. // Localize Scripts 
  344. wp_localize_script( 
  345. LAYERS_THEME_SLUG . '-admin-widgets' ,  
  346. 'layers_widget_params',  
  347. array( 
  348. 'nonce' => wp_create_nonce( 'layers-widget-actions' ),  
  349. ); 
  350.  
  351. /** 
  352. * Enqueue Widget Styles 
  353. */ 
  354.  
  355. public function admin_print_styles() { 
  356.  
  357. // Color Picker styles 
  358. wp_enqueue_style( 'wp-color-picker' );