1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
<?php
namespace SIW;
class Widgets {
protected $widgets = [
'accordion' => 'Accordion',
'calendar' => 'Calendar',
'carousel' => 'Carousel',
'contact' => 'Contact',
'cta' => 'CTA',
'dutch-projects' => 'Dutch_Projects',
'features' => 'Features',
'google-maps' => 'Google_Maps',
'infobox' => 'Infobox',
'map' => 'Map',
'newsletter' => 'Newsletter',
'organisation' => 'Organisation',
'pie-chart' => 'Pie_Chart',
'quick-search-form' => 'Quick_Search_Form',
'quick-search-results' => 'Quick_Search_Results',
'quote' => 'Quote',
'tabs' => 'Tabs',
];
protected $widgets_folder_base;
public static function init() {
if ( ! class_exists( '\SiteOrigin_Widgets_Bundle' ) ) {
return;
}
$self = new self();
$self->widgets_folder_base = SIW_INCLUDES_DIR . '/widgets';
add_filter( 'siteorigin_widgets_widget_folders', [ $self, 'set_widgets_folders' ] );
add_filter( 'siteorigin_widgets_active_widgets', [ $self, 'set_active_widgets' ] );
add_filter( 'siteorigin_panels_data', [ $self, 'handle_renamed_widgets'] );
$self->register_widgets();
}
public function set_widgets_folders( array $folders ) {
$folders = [];
$folders[] = $this->widgets_folder_base . '/';
return $folders;
}
public function set_active_widgets( array $active_widgets ) {
foreach ( $this->widgets as $id_base => $class_base ) {
$active_widgets[ $id_base ] = true;
}
return $active_widgets;
}
protected function register_widgets() {
foreach ( $this->widgets as $id_base => $class_base ) {
siteorigin_widget_register( "siw-{$id_base}-widget", $this->widgets_folder_base . "/{$id_base}/{$id_base}.php", "\\SIW\\Widgets\\{$class_base}");
}
}
public function handle_renamed_widgets( $panels_data ) {
if ( ! is_array( $panels_data ) ) {
return $panels_data;
}
foreach( $panels_data['widgets'] as &$widget ) {
if ( 0 === strpos( $widget['panels_info']['class'], 'SIW_Widget_' ) ) {
$widget['panels_info']['class'] = str_replace( 'SIW_Widget_', "\\SIW\\Widgets\\", $widget['panels_info']['class'] );
}
}
return $panels_data;
}
}