@@ -5166,3 +5166,63 @@ function unserialize_data(?string $path, mixed $default = null, ?array $options
51665166 return $ data ;
51675167}
51685168
5169+ /* Get an array of active widgets and metadata from user settings */
5170+ function get_active_widgets ($ user_settings ) {
5171+ $ widgets = [];
5172+
5173+ /* Break up the sequence string into an array of widget definitions */
5174+ $ widget_sep = ', ' ;
5175+ $ widget_seq_array = explode ($ widget_sep , rtrim ($ user_settings ['widgets ' ]['sequence ' ], $ widget_sep ));
5176+
5177+ foreach ($ widget_seq_array as $ widget_seq_data ) {
5178+ /* Break each widget definition into its component values */
5179+ [$ name , $ column , $ display , $ instance ] = explode (': ' , $ widget_seq_data );
5180+ $ widgets [] = [
5181+ 'name ' => $ name ,
5182+ 'column ' => $ column ,
5183+ 'display ' => $ display ,
5184+ 'instance ' => $ instance
5185+ ];
5186+ }
5187+ return $ widgets ;
5188+ }
5189+
5190+ /* Test the validity of a given widget key based on user settings. */
5191+ function is_valid_widgetkey ($ widgetkey , $ user_settings , $ widgetfile = null ) {
5192+ /* Proper form of a widgetkey is <widget-name>-<instance-id>
5193+ * Where:
5194+ * widget-name : Name of an active widget, which should be found in
5195+ * the current sequence list.
5196+ * instance-id : An integer 0 or higher identifying a widget instance
5197+ *
5198+ * Additionally, for a widget to be valid in this context it must also
5199+ * be present on the current Dashboard layout.
5200+ */
5201+
5202+ /* Break the given widgetkey into its component parts */
5203+ [$ wname , $ wid ] = explode ('- ' , $ widgetkey , 2 );
5204+
5205+ /* Test for basic validity conditions */
5206+ if (empty ($ wname ) ||
5207+ !is_numericint ($ wid ) ||
5208+ empty ($ user_settings )) {
5209+ return false ;
5210+ }
5211+
5212+ /* Check if this widget also matches a specific widget name */
5213+ if (!empty ($ widgetfile ) &&
5214+ ($ wname != basename ($ widgetfile , '.widget.php ' ))) {
5215+ return false ;
5216+ }
5217+
5218+ /* Ensure the key is for a widget which is in the Dashboard
5219+ * configuration. */
5220+ $ widgets = get_active_widgets ($ user_settings );
5221+ foreach ($ widgets as $ widget ) {
5222+ if (($ widget ['name ' ] == $ wname ) &&
5223+ ($ widget ['instance ' ] == $ wid )) {
5224+ return true ;
5225+ }
5226+ }
5227+ return false ;
5228+ }
0 commit comments