readmill.php
| 1 | <?php |
| 2 | class Jetpack_Readmill_Widget extends WP_Widget { |
| 3 | var $default_title, $default_size; |
| 4 | |
| 5 | /** |
| 6 | * Registers the widget with WordPress. |
| 7 | */ |
| 8 | function __construct() { |
| 9 | parent::__construct( |
| 10 | 'jetpack_readmill_widget', // Base ID |
| 11 | apply_filters( 'jetpack_widget_name', esc_html__( 'Send To Readmill', 'jetpack' ) ), |
| 12 | array( |
| 13 | 'description' => esc_html__( 'Readmill is the best book reader for phones and tablets. With this widget you can enable users to send a book to their device with one click.', 'jetpack' ), |
| 14 | ) |
| 15 | ); |
| 16 | |
| 17 | if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) ) { |
| 18 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_script' ) ); |
| 19 | } |
| 20 | |
| 21 | $this->default_title = __( 'Send To Readmill', 'jetpack' ); |
| 22 | $this->default_size = 'large'; |
| 23 | } |
| 24 | |
| 25 | function enqueue_script() { |
| 26 | wp_enqueue_script( 'readmill', 'https://platform.readmill.com/send.js', array(), '20130220', false ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Back-end widget form. |
| 31 | * |
| 32 | * @see WP_Widget::form() |
| 33 | * |
| 34 | * @param array $instance Previously saved values from database. |
| 35 | */ |
| 36 | function form( $instance ) { |
| 37 | $title = isset( $instance['title' ] ) ? $instance['title'] : false; |
| 38 | if ( false === $title ) { |
| 39 | $title = $this->default_title; |
| 40 | } |
| 41 | |
| 42 | $epub_link = isset( $instance['epub_link'] ) ? $instance['epub_link'] : ''; |
| 43 | $buy_link = isset( $instance['buy_link'] ) ? $instance['buy_link'] : ''; |
| 44 | $size = isset( $instance['size'] ) ? $instance['size'] : $this->default_size; |
| 45 | ?> |
| 46 | |
| 47 | <p><?php printf( __( "Just enter the URL to your book, make sure it's a PDF or EPUB file, and you are ready to go. For more help, head to <a href='%s'>the Readmill WordPress Widget support page</a>.", 'jetpack' ), 'http://en.support.wordpress.com/widgets/readmill/' ); ?></p> |
| 48 | |
| 49 | <p> |
| 50 | <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label> |
| 51 | <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> |
| 52 | </p> |
| 53 | |
| 54 | <p> |
| 55 | <label for="<?php echo $this->get_field_id( 'epub_link' ); ?>"><?php esc_html_e( 'Download URL:', 'jetpack' ); ?></label> |
| 56 | <input class="widefat" id="<?php echo $this->get_field_id( 'epub_link' ); ?>" name="<?php echo $this->get_field_name( 'epub_link' ); ?>" type="text" value="<?php echo esc_attr( $epub_link ); ?>" /> |
| 57 | </p> |
| 58 | |
| 59 | <p> |
| 60 | <label for="<?php echo $this->get_field_id( 'buy_link' ); ?>"><?php esc_html_e( 'Item URL:', 'jetpack' ); ?></label> |
| 61 | <input class="widefat" id="<?php echo $this->get_field_id( 'buy_link' ); ?>" name="<?php echo $this->get_field_name( 'buy_link' ); ?>" type="text" value="<?php echo esc_attr( $buy_link ); ?>" /> |
| 62 | </p> |
| 63 | |
| 64 | <p> |
| 65 | <label><?php esc_html_e( 'What size icon?', 'jetpack' ); ?></label> |
| 66 | <ul> |
| 67 | <li><label><input id="<?php echo $this->get_field_id( 'size' ); ?>-few" name="<?php echo $this->get_field_name( 'size' ); ?>" type="radio" value="large" <?php checked( 'large', $size ); ?> /> <?php esc_html_e( 'Large', 'jetpack' ); ?></label></li> |
| 68 | <li><label><input id="<?php echo $this->get_field_id( 'size' ); ?>-lots" name="<?php echo $this->get_field_name( 'size' ); ?>" type="radio" value="small" <?php checked( 'small', $size ); ?> /> <?php esc_html_e( 'Small', 'jetpack' ); ?></label></li> |
| 69 | </ul> |
| 70 | </p> |
| 71 | |
| 72 | <?php |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Sanitize widget form values as they are saved. |
| 77 | * |
| 78 | * @see WP_Widget::update() |
| 79 | * |
| 80 | * @param array $new_instance Values just sent to be saved. |
| 81 | * @param array $old_instance Previously saved values from database. |
| 82 | * |
| 83 | * @return array Updated safe values to be saved. |
| 84 | */ |
| 85 | function update( $new_instance, $old_instance ) { |
| 86 | $instance = array(); |
| 87 | $instance['title'] = wp_kses( $new_instance['title'], array() ); |
| 88 | $instance['epub_link'] = wp_kses( $new_instance['epub_link'], array() ); |
| 89 | $instance['buy_link'] = wp_kses( $new_instance['buy_link'], array() ); |
| 90 | $instance['size'] = wp_kses( $new_instance['size'], array() ); |
| 91 | |
| 92 | if ( $this->default_title === $instance['title'] ) { |
| 93 | $instance['title'] = false; // Store as false in case of language change |
| 94 | } |
| 95 | |
| 96 | return $instance; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Front-end display of widget. |
| 101 | * |
| 102 | * @see WP_Widget::widget() |
| 103 | * |
| 104 | * @param array $args Widget arguments. |
| 105 | * @param array $instance Saved values from database. |
| 106 | */ |
| 107 | function widget( $args, $instance ) { |
| 108 | $title = isset( $instance['title' ] ) ? $instance['title'] : false; |
| 109 | |
| 110 | if ( false === $title ) |
| 111 | $title = $this->default_title; |
| 112 | |
| 113 | $title = apply_filters( 'widget_title', $title ); |
| 114 | |
| 115 | echo $args['before_widget']; |
| 116 | |
| 117 | if ( ! empty( $title ) ) |
| 118 | echo $args['before_title'] . $title . $args['after_title']; |
| 119 | |
| 120 | $epub_link = isset( $instance['epub_link'] ) ? $instance['epub_link'] : ''; |
| 121 | $buy_link = isset( $instance['buy_link'] ) ? $instance['buy_link'] : ''; |
| 122 | $size = isset( $instance['size'] ) ? $instance['size'] : $this->default_size; |
| 123 | |
| 124 | if ( empty( $epub_link ) && current_user_can( 'edit_theme_options' ) ) : |
| 125 | ?><p><?php esc_html_e( 'Your ePub link is empty. Provide an ePub link to display the Send to Readmill widget.', 'jetpack' ); ?></p><?php |
| 126 | else : |
| 127 | ?><a class="send-to-readmill" href="https://readmill.com" data-download-url="<?php echo esc_attr( $epub_link ); ?>" data-buy-url="<?php echo esc_attr( $epub_link ); ?>" data-display="<?php echo esc_attr( $size ); ?>">Send to Readmill</a><?php |
| 128 | endif; |
| 129 | |
| 130 | echo $args['after_widget']; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | function jetpack_readmill_widget_init() { |
| 135 | register_widget( 'Jetpack_Readmill_Widget' ); |
| 136 | } |
| 137 | |
| 138 | add_action( 'widgets_init', 'jetpack_readmill_widget_init' ); |
| 139 |