コード例 #1
0
 public function create_widgets()
 {
     $idx_api = new \IDX\Idx_Api();
     $widget_cache = $idx_api->get_transient('idx_widgetsrc_cache');
     $idx_widgets = $widget_cache ? $widget_cache : $idx_api->idx_api_get_widgetsrc();
     if (is_array($idx_widgets)) {
         foreach ($idx_widgets as $widget) {
             if (!is_numeric(str_replace('-', '', $widget->uid))) {
                 continue;
             }
             $widget_class = "widget_" . str_replace('-', '_', $widget->uid);
             // format class name, ex: widget_596-12345
             $widget_id = "idx" . str_replace('-', '_', $widget->uid);
             $bad_characters = array('{', '}', '[', ']', '%');
             // remove any potential braces or other script breaking characters, then escape them using WP's function esc_html
             $widget_title = "IDX " . esc_html(str_replace($bad_characters, '', $widget->name));
             // set widget title to "IDX [name]"
             $widget_ops = "array('classname' => '{$widget_class}',\r                                'description' => __('{$widget_title}', 'text domain'))";
             // to be eval'd upon class creation below
             // easiest manner to create a dynamically named class is to eval a string to do it for us.
             // all the variables above are escaped properly to prevent any breakage from using the eval function
             // upon creation of the new widget class, it will extend the Idx_Widget class created above, which extends WP_Widget
             $eval = "class {$widget_class} extends \\IDX\\Widgets\\Idx_Widget_Class {\r                    function __construct() {\r                       \\WP_Widget::__construct('{$widget_id}', __('{$widget_title}', 'text domain'), {$widget_ops});\r                        \$this->widget_url = '{$widget->url}';\r                        \$this->widget_class = '{$widget_class}';\r                        \$this->widget_id = '{$widget_id}';\r                    }}";
             eval($eval);
             add_action('widgets_init', create_function('', "return register_widget('{$widget_class}');"));
             // attach the newly created widget class to the WP widget initializier
         }
     }
 }