NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Widget.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Adds compatibility methods between WP_Widget and scbForms.
  4. */
  5. abstract class scbWidget extends WP_Widget {
  6. /**
  7. * Widget defaults.
  8. * @var array
  9. */
  10. protected $defaults = array();
  11. /**
  12. * Widgets to register.
  13. * @var array
  14. */
  15. private static $scb_widgets = array();
  16. /**
  17. * Initializes widget.
  18. *
  19. * @param string $class
  20. * @param string $file (optional)
  21. * @param string $base (optional)
  22. *
  23. * @return void
  24. */
  25. public static function init( $class, $file = '', $base = '' ) {
  26. self::$scb_widgets[] = $class;
  27. add_action( 'widgets_init', array( __CLASS__, '_scb_register' ) );
  28. // for auto-uninstall
  29. if ( $file && $base && class_exists( 'scbOptions' ) ) {
  30. new scbOptions( "widget_$base", $file );
  31. }
  32. }
  33. /**
  34. * Registers widgets.
  35. *
  36. * @return void
  37. */
  38. public static function _scb_register() {
  39. foreach ( self::$scb_widgets as $widget ) {
  40. register_widget( $widget );
  41. }
  42. }
  43. /**
  44. * Displays widget content.
  45. *
  46. * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
  47. * @param array $instance The settings for the particular instance of the widget.
  48. *
  49. * @return void
  50. */
  51. public function widget( $args, $instance ) {
  52. $instance = wp_parse_args( $instance, $this->defaults );
  53. extract( $args );
  54. echo $before_widget;
  55. $title = apply_filters( 'widget_title', isset( $instance['title'] ) ? $instance['title'] : '', $instance, $this->id_base );
  56. if ( ! empty( $title ) ) {
  57. echo $before_title . $title . $after_title;
  58. }
  59. $this->content( $instance );
  60. echo $after_widget;
  61. }
  62. /**
  63. * This is where the actual widget content goes.
  64. *
  65. * @param array $instance The settings for the particular instance of the widget.
  66. *
  67. * @return void
  68. */
  69. protected function content( $instance ) { }
  70. //_____HELPER METHODS_____
  71. /**
  72. * Generates a input form field.
  73. *
  74. * @param array $args
  75. * @param array $formdata (optional)
  76. *
  77. * @return string
  78. */
  79. protected function input( $args, $formdata = array() ) {
  80. $prefix = array( 'widget-' . $this->id_base, $this->number );
  81. $form = new scbForm( $formdata, $prefix );
  82. // Add default class
  83. if ( ! isset( $args['extra'] ) && 'text' == $args['type'] ) {
  84. $args['extra'] = array( 'class' => 'widefat' );
  85. }
  86. // Add default label position
  87. if ( ! in_array( $args['type'], array( 'checkbox', 'radio' ) ) && empty( $args['desc_pos'] ) ) {
  88. $args['desc_pos'] = 'before';
  89. }
  90. $name = $args['name'];
  91. if ( ! is_array( $name ) && '[]' == substr( $name, -2 ) ) {
  92. $name = array( substr( $name, 0, -2 ), '' );
  93. }
  94. $args['name'] = $name;
  95. return $form->input( $args );
  96. }
  97. }