Skip to content

Commit a337fc5

Browse files
committed
feat(engine): improved widget loading
1 parent e85a026 commit a337fc5

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

plugin-name/engine/Initialize.php

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,41 @@ private function initialize_plugin_class( $classtovalidate ) {
120120
return;
121121
}
122122

123-
$temp = new $classtovalidate;
124-
\add_filter(
125-
'plugin_name_instance_' . $classtovalidate,
126-
function() use ( $temp ) {
127-
return $temp;
123+
if ( strpos( $classtovalidate, 'Widgets' ) !== false ) {
124+
\add_action(
125+
'widgets_init',
126+
function() use ( $classtovalidate ) {
127+
$temp = new $classtovalidate;
128+
129+
if ( !\method_exists( $temp, 'initialize' ) ) {
130+
return;
131+
}
132+
133+
$temp->initialize();
134+
\add_filter(
135+
'plugin_name_instance_' . $classtovalidate,
136+
function() use ( $temp ) {
137+
return $temp;
138+
}
139+
);
140+
}
141+
);
142+
} else {
143+
$temp = new $classtovalidate;
144+
145+
if ( !\method_exists( $temp, 'initialize' ) ) {
146+
return;
128147
}
129-
);
130148

131-
if ( !\method_exists( $temp, 'initialize' ) ) {
132-
return;
133-
}
149+
$temp->initialize();
134150

135-
$temp->initialize();
151+
\add_filter(
152+
'plugin_name_instance_' . $classtovalidate,
153+
function() use ( $temp ) {
154+
return $temp;
155+
}
156+
);
157+
}
136158
}
137159

138160
/**

plugin-name/integrations/Widgets/My_Recent_Posts_Widget.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,7 @@ public function widget( $args, $instance ) { //phpcs:ignore
187187
* @return void
188188
*/
189189
public function initialize() {
190-
\add_action(
191-
'widgets_init',
192-
static function() {
193-
\register_widget( 'Plugin_Name\Integrations\Widgets\My_Recent_Posts_Widget' );
194-
}
195-
);
190+
\register_widget( 'Plugin_Name\Integrations\Widgets\My_Recent_Posts_Widget' );
196191
}
197192

198193
}

0 commit comments

Comments
 (0)