1
difenduandada
2024-10-15 7fd2948ee35c8e147ed35ce6d8502f94a98ddd22
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
<?php
 
$widget_factory;
 
$registered_sidebars = array();
 
class Widget {
    public $name;
    public $id_base;
    public $description = '';
 
    public function __construct() {
        // actual widget processes
    }
 
    public function widget( $instance, $args  ) {
        // outputs the content of the widget
    }
 
    public function form( $instance ) {
        echo '<p class="no-options-widget">' . __( 'There are no options for this widget.' ) . '</p>';
        return 'noform';
    }
 
    public function update( $new_instance, $old_instance ) {
        return $new_instance;
    }
}
 
class Widget_Factory {
    public $widgets = array();
 
    public function register( $widget ) {
        if ( $widget instanceof Widget ) {
            $this->widgets[ spl_object_hash( $widget ) ] = $widget;
        } else {
            $this->widgets[ $widget ] = new $widget();
        }
    }
 
    public function unregister( $widget ) {
        if ( $widget instanceof WP_Widget ) {
            unset( $this->widgets[ spl_object_hash( $widget ) ] );
        } else {
            unset( $this->widgets[ $widget ] );
        }
    }
}
 
$widget_factory = new Widget_Factory();
 
function register_widget($widget){
    global $widget_factory;
    $widget_factory->register($widget);
}
 
function the_widget( $widget, $instance = array(), $args = array() ){
    global $widget_factory;
    if ( ! isset( $widget_factory->widgets[ $widget ] ) ) {
        return;
    }
 
    $widget_obj = $widget_factory->widgets[ $widget ];
    if ( ! ( $widget_obj instanceof Widget ) ) {
        return;
    }
 
    $widget_obj->widget( $instance, $args );
}
 
function get_widget( $widget, $instance = array(), $args = array() ){
    global $widget_factory;
    if ( ! isset( $widget_factory->widgets[ $widget ] ) ) {
        return;
    }
 
    $widget_obj = $widget_factory->widgets[ $widget ];
    if ( ! ( $widget_obj instanceof Widget ) ) {
        return;
    }
 
    return $widget_obj;
}
 
function widget_exists($widget){
    global $widget_factory;
    if(isset($widget_factory->widgets[ $widget ])){
        return true;
    } else {
        return false;
    }
}
 
require( ABSPATH . 'includes/widgets.php' );
 
?>