*/ defined( 'ABSPATH' ) || exit; /** * LP_Widget * * @version 4.0.0 * @extends WP_Widget */ class LP_Widget extends WP_Widget { /** * CSS class. * * @var string */ public $widget_cssclass; /** * Widget description. * * @var string */ public $widget_description; /** * Widget ID. * * @var string */ public $widget_id; /** * Widget name. * * @var string */ public $widget_name; /** * Enable rest_api for LearnPress. * * @var string */ public $widget_in_rest = true; /** * New param add to rest api need for data. * * @var array */ public $widget_data_attr = array(); /** * Settings. * * @var array */ public $settings; /** * Constructor. */ public function __construct() { $widget_ops = array( 'classname' => $this->widget_cssclass, 'description' => $this->widget_description, 'customize_selective_refresh' => true, ); parent::__construct( $this->widget_id, $this->widget_name, $widget_ops ); } /** * Get this widgets title. */ protected function get_instance_title( $instance ) { if ( isset( $instance['title'] ) ) { return $instance['title']; } if ( isset( $this->settings, $this->settings['title'], $this->settings['title']['std'] ) ) { return $this->settings['title']['std']; } return ''; } /** * Output the html at the start of a widget. * * @param array $args Arguments. * @param array $instance Instance. */ public function widget_start( $args, $instance ) { echo wp_kses_post( $args['before_widget'] ); $title = apply_filters( 'widget_title', $this->get_instance_title( $instance ), $instance, $this->id_base ); if ( $title ) { echo wp_kses_post( $args['before_title'] . $title . $args['after_title'] ); } } /** * Output the html at the end of a widget. * * @param array $args Arguments. */ public function widget_end( $args ) { echo wp_kses_post( $args['after_widget'] ); } /** * Output Widgets HTML. * * @param [type] $args * @param [type] $instance * @return void */ public function widget( $args, $instance ) { wp_enqueue_script( 'lp-widgets' ); do_action( 'before_show_lp_widget_content' ); $data = array_merge( $this->widget_data_attr, array( 'widget' => $this->widget_id, 'instance' => wp_json_encode( $instance ), ) ); echo wp_kses_post( $this->lp_widget_content( $data, $args, $instance ) ); } /** * Show widget content. * * @param array $data Data attribute HTML for Rest API js. * @param [type] $args Default Widget Args * @param [type] $instance Default Widget Instance * @return string HTML */ public function lp_widget_content( $data, $args, $instance ) { ob_start(); $this->widget_start( $args, $instance ); if ( ! is_admin() && $this->widget_in_rest ) { ?>
lp_rest_api_content( $instance, array() ); echo ''; } $this->widget_end( $args ); return ob_get_clean(); } /** * Send content for API * * @param array $instance Widget Instance * @param array $params RestAPI param need for content. * @return string || WP_Error */ public function lp_rest_api_content( $instance, $params ) { return 'No content for Rest API'; } /** * Updates a particular instance of a widget. */ public function update( $new_instance, $old_instance ) { $instance = $old_instance; if ( empty( $this->settings ) ) { return $instance; } foreach ( $this->settings as $key => $setting ) { if ( ! isset( $setting['type'] ) ) { continue; } switch ( $setting['type'] ) { case 'number': $instance[ $key ] = absint( $new_instance[ $key ] ); if ( isset( $setting['min'] ) && '' !== $setting['min'] ) { $instance[ $key ] = max( $instance[ $key ], $setting['min'] ); } if ( isset( $setting['max'] ) && '' !== $setting['max'] ) { $instance[ $key ] = min( $instance[ $key ], $setting['max'] ); } break; case 'textarea': $instance[ $key ] = wp_kses( trim( wp_unslash( $new_instance[ $key ] ) ), wp_kses_allowed_html( 'post' ) ); break; case 'checkbox': $instance[ $key ] = empty( $new_instance[ $key ] ) ? 0 : 1; break; default: $instance[ $key ] = isset( $new_instance[ $key ] ) ? sanitize_text_field( $new_instance[ $key ] ) : $setting['std']; break; } /** * Sanitize the value of a setting. */ $instance[ $key ] = apply_filters( 'learnpress_widget_settings_sanitize_option', $instance[ $key ], $new_instance, $key, $setting ); } return $instance; } /** * Outputs the settings update form. */ public function form( $instance ) { if ( empty( $this->settings ) ) { echo '' . esc_html_e( 'There are no options for this widget.', 'learnpress' ) . '
'; return; } foreach ( $this->settings as $key => $setting ) { $class = isset( $setting['class'] ) ? $setting['class'] : ''; $value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std']; switch ( $setting['type'] ) { case 'text': ?>
/>