English (United Kingdom)
Knowledge Base  >  Vik Appointments  >  Hooks  >  Dashboard  >  Fetch Widget Classname

apply_filters( 'vikappointments_fetch_statistics_widget_classname', string $classname, string $widget )

Fires while instantiating a third-party widget for the Dashboard and Analytics pages.


Description

The plugins MUST include here all the resources needed to the widget, otherwise it wouldn't be possible to instantiate the returned classes.

The event should return the PHP class name related to the requested widget.

In case the specified class doesn't exist, an exception will be thrown. The class MUST extend the VAPStatisticsWidget abstract class.

NOTE: the file in which the widget class is located MUST be explicitly loaded.


Parameters

$classname

(string)  The classname that will be used to instantiate the widget object.

$widget

(string)  The name of the widget to include, which is equals to the base name of the file (without .php).


Example

The example provides the instantiation of the "test" widget located within the VikWP plugin.

/wp-content/plugins/vikwp/widgets/test.php
/**
 * Trigger filter to let the plugins include external widgets.
 * The plugins MUST include the resources needed, otherwise
 * it wouldn't be possible to instantiate the returned classes.
 *
 * @param  string  $classname  The classname of the widget.
 * @param  string  $widget     The name of the widget to include.
 */
add_filter('vikappointments_fetch_statistics_widget_classname', function($classname, $widget)
{
    // define list of supported widgets
    $supported = array(
        'foo_test',
    );

    if (in_array($widget, $supported))
    {
        // include widget class handler
        require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'widgets' . DIRECTORY_SEPARATOR . $widget . '.php';
	
        // convert widget name in Pascal Case: "foo_test" becomes "FooTest"
        $widget = str_replace(' ', '', ucwords(str_replace('_', ' ', $widget)));

        // set widget class name (e.g. "VAPStatisticsWidgetFooTest")
        $classname = 'VAPStatisticsWidget' . $widget;
    }

    return $classname;
}, 10, 2);

Changelog

Version Description
1.2 Introduced.
Last Update: 2021-09-30
Helpful?
This site uses cookies. By continuing to browse you accept their use. Further information