| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class: InsertPagesWidget extends WP_Widget |
| 5 |
* Provides a widget for inserting a page into a widget area. |
| 6 |
*/ |
| 7 |
|
| 8 |
class InsertPagesWidget extends WP_Widget { |
| 9 |
|
| 10 |
/** |
| 11 |
* Set up the widget. |
| 12 |
*/ |
| 13 |
public function __construct() { |
| 14 |
// Load admin javascript for Widget options. |
| 15 |
if ( is_admin() ) { |
| 16 |
wp_enqueue_script( 'insertpages_widget', plugins_url( '/js/widget.js', __FILE__ ), array( 'jquery' ), '20160105' ); |
| 17 |
} |
| 18 |
|
| 19 |
// Call parent constructor to initialize the widget. |
| 20 |
parent::__construct( 'ipw', 'Insert Page', array( 'description' => 'Insert a page into a widget area.' ) ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Output the content of the widget. |
| 25 |
* |
| 26 |
* @param array $args |
| 27 |
* @param array $instance |
| 28 |
*/ |
| 29 |
public function widget( $args, $instance ) { |
| 30 |
global $insertPages_plugin; |
| 31 |
|
| 32 |
// Print widget wrapper. |
| 33 |
echo $args['before_widget']; |
| 34 |
|
| 35 |
// Build the shortcode attributes array from the widget args. |
| 36 |
$atts = array(); |
| 37 |
if ( array_key_exists( 'page', $instance ) ) { |
| 38 |
$atts['page'] = $instance['page']; |
| 39 |
} |
| 40 |
if ( array_key_exists( 'display', $instance ) ) { |
| 41 |
$atts['display'] = $instance['display']; |
| 42 |
} |
| 43 |
if ( array_key_exists( 'template', $instance ) && $instance['display'] === 'template' ) { |
| 44 |
$atts['display'] = $instance['template']; |
| 45 |
} |
| 46 |
if ( array_key_exists( 'class', $instance ) ) { |
| 47 |
$atts['class'] = $instance['class']; |
| 48 |
} |
| 49 |
if ( array_key_exists( 'inline', $instance ) ) { |
| 50 |
$atts['inline'] = $instance['inline'] === '1'; |
| 51 |
} |
| 52 |
|
| 53 |
// Render the inserted page using the plugin's shortcode handler. |
| 54 |
$content = $insertPages_plugin->insertPages_handleShortcode_insert( $atts ); |
| 55 |
|
| 56 |
// Print inserted page. |
| 57 |
echo $content; |
| 58 |
|
| 59 |
// Print widget wrapper. |
| 60 |
echo $args['after_widget']; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Output the options form on admin. |
| 65 |
* |
| 66 |
* @param array $instance The widget options |
| 67 |
*/ |
| 68 |
public function form( $instance ) { |
| 69 |
$instance = wp_parse_args( (array)$instance, array( |
| 70 |
'page' => '', |
| 71 |
'display' => 'link', |
| 72 |
'template' => '', |
| 73 |
'class' => '', |
| 74 |
'inline' => '', |
| 75 |
)); ?> |
| 76 |
<p> |
| 77 |
<label for="<?php echo $this->get_field_id( 'page' ); ?>"><?php _e( 'Page/Post ID or Slug' ); ?>:</label> |
| 78 |
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'page' ); ?>" name="<?php echo $this->get_field_name( 'page' ); ?>" value="<?php echo $instance['page']; ?>" /> |
| 79 |
</p> |
| 80 |
<p> |
| 81 |
<label for="<?php echo $this->get_field_id( 'display' ); ?>"><?php _e( 'Display' ); ?>:</label><br /> |
| 82 |
<select class="insertpage-format-select" name="<?php echo $this->get_field_name( 'display' ); ?>" id="<?php echo $this->get_field_id( 'display' ); ?>"> |
| 83 |
<option value='title' <?php selected( $instance['display'], 'title' ); ?>>Title</option> |
| 84 |
<option value='link' <?php selected( $instance['display'], 'link' ); ?>>Link</option> |
| 85 |
<option value='excerpt' <?php selected( $instance['display'], 'excerpt' ); ?>>Excerpt</option> |
| 86 |
<option value='excerpt-only' <?php selected( $instance['display'], 'excerpt-only' ); ?>>Excerpt only (no title)</option> |
| 87 |
<option value='content' <?php selected( $instance['display'], 'content' ); ?>>Content</option> |
| 88 |
<option value='all' <?php selected( $instance['display'], 'all' ); ?>>All (includes custom fields)</option> |
| 89 |
<option value='template' <?php selected( $instance['display'], 'template' ); ?>>Use a custom template »</option> |
| 90 |
</select> |
| 91 |
<select class="insertpage-template-select" name="<?php echo $this->get_field_name( 'template' ); ?>" id="<?php echo $this->get_field_id( 'template' ); ?>" disabled="disabled"> |
| 92 |
<option value='all'><?php _e( 'Default Template' ); ?></option> |
| 93 |
<?php page_template_dropdown( $instance['template'] ); ?> |
| 94 |
</select> |
| 95 |
</p> |
| 96 |
<p> |
| 97 |
<label for="<?php echo $this->get_field_id( 'class' ); ?>"><?php _e( 'Extra Classes' ); ?>:</label> |
| 98 |
<input type="text" class="widefat" autocomplete="off" name="<?php echo $this->get_field_name( 'class' ); ?>" id="<?php echo $this->get_field_id( 'class' ); ?>" value="<?php echo esc_attr( $instance['class'] ); ?>" /> |
| 99 |
</p> |
| 100 |
<p> |
| 101 |
<input class="checkbox" type="checkbox" name="<?php echo $this->get_field_name( 'inline' ); ?>" id="<?php echo $this->get_field_id( 'inline' ); ?>" value="1" <?php checked( $instance['inline'], '1' ); ?> /> |
| 102 |
<label for="<?php echo $this->get_field_id( 'inline' ); ?>"><?php _e( 'Inline?' ); ?></label> |
| 103 |
</p><?php |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Process widget options on save. |
| 108 |
* |
| 109 |
* @param array $new_instance The new options |
| 110 |
* @param array $old_instance The previous options |
| 111 |
*/ |
| 112 |
public function update( $new_instance, $old_instance ) { |
| 113 |
// Sanitize form options. |
| 114 |
$instance = $old_instance; |
| 115 |
$instance['page'] = strip_tags( $new_instance['page'] ); |
| 116 |
$instance['display'] = strip_tags( $new_instance['display'] ); |
| 117 |
$instance['template'] = strip_tags( $new_instance['template'] ); |
| 118 |
$instance['class'] = strip_tags( $new_instance['class'] ); |
| 119 |
$instance['inline'] = strip_tags( $new_instance['inline'] ); |
| 120 |
|
| 121 |
return $instance; |
| 122 |
} |
| 123 |
} |
| 124 |
|