| 1 |
<?php |
| 2 |
/** |
| 3 |
* Insert Pages widget. |
| 4 |
* |
| 5 |
* @package insert-pages |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Class: InsertPagesWidget extends WP_Widget |
| 10 |
* |
| 11 |
* Provides a widget for inserting a page into a widget area. |
| 12 |
* |
| 13 |
* @package insert-pages |
| 14 |
*/ |
| 15 |
class InsertPagesWidget extends WP_Widget { |
| 16 |
|
| 17 |
/** |
| 18 |
* Set up the widget. |
| 19 |
*/ |
| 20 |
public function __construct() { |
| 21 |
// Load admin javascript for Widget options on admin page (widgets.php). |
| 22 |
add_action( 'sidebar_admin_page', array( $this, 'widget_admin_js' ) ); |
| 23 |
|
| 24 |
// Load widget javascript in supported builders (Beaver Builder, Elementor). |
| 25 |
add_action( 'fl_builder_layout_style_dependencies', array( $this, 'widget_admin_js' ) ); |
| 26 |
add_action( 'elementor/editor/before_enqueue_scripts', array( $this, 'widget_admin_js' ) ); |
| 27 |
|
| 28 |
// Load admin javascript for Widget options on theme customize page (customize.php). |
| 29 |
add_action( 'customize_controls_enqueue_scripts', array( $this, 'widget_admin_js' ) ); |
| 30 |
|
| 31 |
// Call parent constructor to initialize the widget. |
| 32 |
parent::__construct( 'ipw', __( 'Insert Page', 'insert-pages' ), array( 'description' => __( 'Insert a page into a widget area.', 'insert-pages' ) ) ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Load javascript for interacting with the Insert Page widget. |
| 37 |
*/ |
| 38 |
public function widget_admin_js() { |
| 39 |
wp_enqueue_script( 'insertpages_widget', plugins_url( '/js/widget.js', __FILE__ ), array( 'jquery' ), '20221115', array( 'in_footer' => false ) ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Output the content of the widget. |
| 44 |
* |
| 45 |
* @param array $args Widget args. |
| 46 |
* @param array $instance Widget instance. |
| 47 |
*/ |
| 48 |
public function widget( $args, $instance ) { |
| 49 |
global $insert_pages_plugin; |
| 50 |
|
| 51 |
// Print widget wrapper. |
| 52 |
echo wp_kses_post( $args['before_widget'] ); |
| 53 |
|
| 54 |
// Build the shortcode attributes array from the widget args. |
| 55 |
$atts = array(); |
| 56 |
if ( array_key_exists( 'page', $instance ) ) { |
| 57 |
$atts['page'] = $instance['page']; |
| 58 |
} |
| 59 |
if ( array_key_exists( 'display', $instance ) ) { |
| 60 |
$atts['display'] = $instance['display']; |
| 61 |
} |
| 62 |
if ( array_key_exists( 'template', $instance ) && 'template' === $instance['display'] ) { |
| 63 |
$atts['display'] = $instance['template']; |
| 64 |
} |
| 65 |
if ( array_key_exists( 'class', $instance ) ) { |
| 66 |
$atts['class'] = $instance['class']; |
| 67 |
} |
| 68 |
if ( array_key_exists( 'id', $instance ) ) { |
| 69 |
$atts['id'] = $instance['id']; |
| 70 |
} |
| 71 |
if ( array_key_exists( 'inline', $instance ) ) { |
| 72 |
$atts['inline'] = '1' === $instance['inline']; |
| 73 |
} |
| 74 |
if ( array_key_exists( 'querystring', $instance ) ) { |
| 75 |
$atts['querystring'] = $instance['querystring']; |
| 76 |
} |
| 77 |
if ( array_key_exists( 'size', $instance ) && 'post-thumbnail' === $instance['display'] ) { |
| 78 |
$atts['size'] = $instance['size']; |
| 79 |
} |
| 80 |
if ( array_key_exists( 'public', $instance ) ) { |
| 81 |
$atts['public'] = '1' === $instance['public']; |
| 82 |
} |
| 83 |
|
| 84 |
// Render the inserted page using the plugin's shortcode handler. |
| 85 |
$content = $insert_pages_plugin->insert_pages_handle_shortcode_insert( $atts ); |
| 86 |
|
| 87 |
// Print inserted page. |
| 88 |
echo $content; |
| 89 |
|
| 90 |
// Print widget wrapper. |
| 91 |
echo wp_kses_post( $args['after_widget'] ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Output the options form on admin. |
| 96 |
* |
| 97 |
* @param array $instance The widget options. |
| 98 |
*/ |
| 99 |
public function form( $instance ) { |
| 100 |
|
| 101 |
// Beaver Builder loads the widget without some required wp-admin |
| 102 |
// dependencies. Add them here. |
| 103 |
if ( ! function_exists( 'page_template_dropdown' ) ) { |
| 104 |
// The function page_template_dropdown() is defined in template.php. |
| 105 |
/** WordPress Template Administration API */ |
| 106 |
require_once ABSPATH . 'wp-admin/includes/template.php'; |
| 107 |
// The function get_page_templates() is defined in theme.php. |
| 108 |
/** WordPress Theme Administration API */ |
| 109 |
require_once ABSPATH . 'wp-admin/includes/theme.php'; |
| 110 |
} |
| 111 |
|
| 112 |
$instance = wp_parse_args( |
| 113 |
(array) $instance, |
| 114 |
array( |
| 115 |
'page' => '', |
| 116 |
'display' => 'link', |
| 117 |
'template' => '', |
| 118 |
'class' => '', |
| 119 |
'id' => '', |
| 120 |
'inline' => '', |
| 121 |
'querystring' => '', |
| 122 |
'size' => '', |
| 123 |
'public' => '', |
| 124 |
) |
| 125 |
); ?> |
| 126 |
<p> |
| 127 |
<label for="<?php echo esc_attr( $this->get_field_id( 'page' ) ); ?>"><?php esc_html_e( 'Page/Post ID or Slug', 'insert-pages' ); ?>:</label> |
| 128 |
<input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'page' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'page' ) ); ?>" value="<?php echo esc_attr( $instance['page'] ); ?>" /> |
| 129 |
</p> |
| 130 |
<p> |
| 131 |
<label for="<?php echo esc_attr( $this->get_field_id( 'display' ) ); ?>"><?php esc_html_e( 'Display', 'insert-pages' ); ?>:</label><br /> |
| 132 |
<select class="insertpage-format-select" name="<?php echo esc_attr( $this->get_field_name( 'display' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'display' ) ); ?>"> |
| 133 |
<option value='title' <?php selected( $instance['display'], 'title' ); ?>><?php esc_html_e( 'Title', 'insert-pages' ); ?></option> |
| 134 |
<option value='title-content' <?php selected( $instance['display'], 'title-content' ); ?>><?php esc_html_e( 'Title and content', 'insert-pages' ); ?></option> |
| 135 |
<option value='link' <?php selected( $instance['display'], 'link' ); ?>><?php esc_html_e( 'Link', 'insert-pages' ); ?></option> |
| 136 |
<option value='excerpt' <?php selected( $instance['display'], 'excerpt' ); ?>><?php esc_html_e( 'Excerpt', 'insert-pages' ); ?></option> |
| 137 |
<option value='excerpt-only' <?php selected( $instance['display'], 'excerpt-only' ); ?>><?php esc_html_e( 'Excerpt only (no title)', 'insert-pages' ); ?></option> |
| 138 |
<option value='content' <?php selected( $instance['display'], 'content' ); ?>><?php esc_html_e( 'Content', 'insert-pages' ); ?></option> |
| 139 |
<option value='post-thumbnail' <?php selected( $instance['display'], 'post-thumbnail' ); ?>><?php esc_html_e( 'Post Thumbnail', 'insert-pages' ); ?></option> |
| 140 |
<option value='all' <?php selected( $instance['display'], 'all' ); ?>><?php esc_html_e( 'All (includes custom fields)', 'insert-pages' ); ?></option> |
| 141 |
<option value='template' <?php selected( $instance['display'], 'template' ); ?>><?php esc_html_e( 'Use a custom template', 'insert-pages' ); ?> »</option> |
| 142 |
</select> |
| 143 |
<select class="insertpage-template-select" name="<?php echo esc_attr( $this->get_field_name( 'template' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'template' ) ); ?>" disabled="disabled"> |
| 144 |
<option value='all'><?php esc_html_e( 'Default Template', 'insert-pages' ); ?></option> |
| 145 |
<?php if ( function_exists( 'page_template_dropdown' ) ) : |
| 146 |
page_template_dropdown( $instance['template'] ); |
| 147 |
endif; ?> |
| 148 |
</select> |
| 149 |
<select class="insertpage-size-select" name="<?php echo esc_attr( $this->get_field_name( 'size' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'size' ) ); ?>" disabled="disabled"> |
| 150 |
<option value="post-thumbnail"><?php esc_html_e( 'post-thumbnail', 'insert-pages' ); ?></option> |
| 151 |
<?php if ( function_exists( 'wp_get_registered_image_subsizes' ) ) : ?> |
| 152 |
<?php foreach ( wp_get_registered_image_subsizes() as $size_name => $size_data ) : ?> |
| 153 |
<option value="<?php echo esc_attr( $size_name ); ?>" <?php selected( $instance['size'], $size_name ); ?>><?php echo esc_html( $size_name ); ?></option> |
| 154 |
<?php endforeach; ?> |
| 155 |
<?php else : ?> |
| 156 |
<?php foreach ( get_intermediate_image_sizes() as $size_name ) : ?> |
| 157 |
<option value="<?php echo esc_attr( $size_name ); ?>" <?php selected( $instance['size'], $size_name ); ?>><?php echo esc_html( $size_name ); ?></option> |
| 158 |
<?php endforeach; ?> |
| 159 |
<?php endif; ?> |
| 160 |
</select> |
| 161 |
</p> |
| 162 |
<p> |
| 163 |
<label for="<?php echo esc_attr( $this->get_field_id( 'class' ) ); ?>"><?php esc_html_e( 'Extra Classes', 'insert-pages' ); ?>:</label> |
| 164 |
<input type="text" class="widefat" autocomplete="off" name="<?php echo esc_attr( $this->get_field_name( 'class' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'class' ) ); ?>" value="<?php echo esc_attr( $instance['class'] ); ?>" /> |
| 165 |
</p> |
| 166 |
<p> |
| 167 |
<label for="<?php echo esc_attr( $this->get_field_id( 'id' ) ); ?>"><?php esc_html_e( 'ID', 'insert-pages' ); ?>:</label> |
| 168 |
<input type="text" class="widefat" autocomplete="off" name="<?php echo esc_attr( $this->get_field_name( 'id' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'id' ) ); ?>" value="<?php echo esc_attr( $instance['id'] ); ?>" /> |
| 169 |
</p> |
| 170 |
<p> |
| 171 |
<input class="checkbox" type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'inline' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'inline' ) ); ?>" value="1" <?php checked( $instance['inline'], '1' ); ?> /> |
| 172 |
<label for="<?php echo esc_attr( $this->get_field_id( 'inline' ) ); ?>"><?php esc_html_e( 'Inline?', 'insert-pages' ); ?></label> |
| 173 |
</p> |
| 174 |
<p> |
| 175 |
<label for="<?php echo esc_attr( $this->get_field_id( 'querystring' ) ); ?>"><?php esc_html_e( 'Querystring', 'insert-pages' ); ?>:</label> |
| 176 |
<input type="text" class="widefat" autocomplete="off" name="<?php echo esc_attr( $this->get_field_name( 'querystring' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'querystring' ) ); ?>" value="<?php echo esc_attr( $instance['querystring'] ); ?>" /> |
| 177 |
</p> |
| 178 |
<p> |
| 179 |
<input class="checkbox" type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'public' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'public' ) ); ?>" value="1" <?php checked( $instance['public'], '1' ); ?> /> |
| 180 |
<label for="<?php echo esc_attr( $this->get_field_id( 'public' ) ); ?>"><?php esc_html_e( 'Anonymous users can see this inserted even if its status is private', 'insert-pages' ); ?></label> |
| 181 |
</p> |
| 182 |
<?php |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Process widget options on save. |
| 187 |
* |
| 188 |
* @param array $new_instance The new options. |
| 189 |
* @param array $old_instance The previous options. |
| 190 |
*/ |
| 191 |
public function update( $new_instance, $old_instance ) { |
| 192 |
// Sanitize form options. |
| 193 |
$instance = $old_instance; |
| 194 |
$instance['page'] = array_key_exists( 'page', $new_instance ) ? wp_strip_all_tags( $new_instance['page'] ) : ''; |
| 195 |
$instance['display'] = array_key_exists( 'display', $new_instance ) ? wp_strip_all_tags( $new_instance['display'] ) : ''; |
| 196 |
$instance['template'] = array_key_exists( 'template', $new_instance ) ? wp_strip_all_tags( $new_instance['template'] ) : ''; |
| 197 |
$instance['class'] = array_key_exists( 'class', $new_instance ) ? wp_strip_all_tags( $new_instance['class'] ) : ''; |
| 198 |
$instance['id'] = array_key_exists( 'id', $new_instance ) ? wp_strip_all_tags( $new_instance['id'] ) : ''; |
| 199 |
$instance['inline'] = array_key_exists( 'inline', $new_instance ) ? wp_strip_all_tags( $new_instance['inline'] ) : ''; |
| 200 |
$instance['querystring'] = array_key_exists( 'querystring', $new_instance ) ? wp_strip_all_tags( $new_instance['querystring'] ) : ''; |
| 201 |
$instance['size'] = array_key_exists( 'size', $new_instance ) ? wp_strip_all_tags( $new_instance['size'] ) : ''; |
| 202 |
$instance['public'] = array_key_exists( 'public', $new_instance ) ? wp_strip_all_tags( $new_instance['public'] ) : ''; |
| 203 |
|
| 204 |
return $instance; |
| 205 |
} |
| 206 |
} |
| 207 |
|