diff --git a/view-all-posts-pages.php b/view-all-posts-pages.php old mode 100644 new mode 100755 index dd2f7b685f116726d433ccaf9d176c0047a850f9..2bf6a31d22a6ec2d2a03283c699faec337d5e5f8 --- a/view-all-posts-pages.php +++ b/view-all-posts-pages.php @@ -39,7 +39,7 @@ class view_all_posts_pages { private $ns = 'view_all_posts_pages'; - private $settings_key = 'vapp'; + private $settings_key = 'vapp'; private $settings_defaults = null; private $notice_key = 'vapp_admin_notice_dismissed'; @@ -57,7 +57,7 @@ class view_all_posts_pages { */ public static function get_instance() { if ( ! is_a( self::$__instance, __CLASS__ ) ) { - self::$__instance = new self; + self::$__instance = new self(); self::$__instance->setup(); } @@ -101,6 +101,7 @@ class view_all_posts_pages { /** * Register plugin option and disable rewrite rule flush warning. + * * @uses register_setting * @uses apply_filters * @uses update_option @@ -110,8 +111,9 @@ class view_all_posts_pages { public function action_admin_init() { register_setting( $this->settings_key, $this->settings_key, array( $this, 'admin_options_validate' ) ); - if ( isset( $_GET[ $this->notice_key ] ) && apply_filters( 'vapp_display_rewrite_rules_notice', true ) ) + if ( isset( $_GET[ $this->notice_key ] ) && apply_filters( 'vapp_display_rewrite_rules_notice', true ) ) { update_option( $this->notice_key, 1 ); + } } /** @@ -147,14 +149,14 @@ class view_all_posts_pages { 'wlp_text' => __( 'View All', 'view_all_posts_pages' ), 'wlp_class' => 'vapp', 'wlp_post_types' => array( - 'post' + 'post', ), 'link' => false, 'link_position' => 'below', 'link_text' => __( 'View All', 'view_all_posts_pages' ), 'link_class' => 'vapp', 'link_post_types' => array( - 'post' + 'post', ), 'link_priority' => 10, ); @@ -162,27 +164,30 @@ class view_all_posts_pages { // Register additional plugin actions if settings call for them. $options = $this->get_options(); - if ( array_key_exists( 'wlp', $options ) && true === $options[ 'wlp' ] ) + if ( array_key_exists( 'wlp', $options ) && true === $options['wlp'] ) { add_filter( 'wp_link_pages_args', array( $this, 'filter_wp_link_pages_args_early' ), 0 ); + } - if ( $options[ 'link' ] ) - add_filter( 'the_content', array( $this, 'filter_the_content_auto' ), $options[ 'link_priority' ] ); + if ( $options['link'] ) { + add_filter( 'the_content', array( $this, 'filter_the_content_auto' ), $options['link_priority'] ); + } - if ( apply_filters( 'vapp_display_rewrite_rules_notice', true ) && ! get_option( $this->notice_key ) ) + if ( apply_filters( 'vapp_display_rewrite_rules_notice', true ) && ! get_option( $this->notice_key ) ) { add_action( 'admin_notices', array( $this, 'action_admin_notices_activation' ) ); + } // Register rewrite endpoint, which handles most of our rewrite needs add_rewrite_endpoint( $this->query_var, EP_ALL ); - //Extra rules needed if verbose page rules are requested + // Extra rules needed if verbose page rules are requested global $wp_rewrite; if ( $wp_rewrite->use_verbose_page_rules ) { - //Build regex - $regex = substr( str_replace( $wp_rewrite->rewritecode, $wp_rewrite->rewritereplace, $wp_rewrite->permalink_structure ), 1 ); - $regex = trailingslashit( $regex ); + // Build regex + $regex = substr( str_replace( $wp_rewrite->rewritecode, $wp_rewrite->rewritereplace, $wp_rewrite->permalink_structure ), 1 ); + $regex = trailingslashit( $regex ); $regex .= $this->query_var . '/?$'; - //Build corresponding query string + // Build corresponding query string $query = substr( str_replace( $wp_rewrite->rewritecode, $wp_rewrite->queryreplace, $wp_rewrite->permalink_structure ), 1 ); $query = explode( '/', $query ); $query = array_filter( $query ); @@ -197,7 +202,7 @@ class view_all_posts_pages { $query = implode( '&', $query ); - //Add rule + // Add rule add_rewrite_rule( $regex, $wp_rewrite->index . '?' . $query, 'top' ); } } @@ -211,8 +216,9 @@ class view_all_posts_pages { * @return string or false */ public function filter_redirect_canonical( $url ) { - if ( $this->is_view_all() ) + if ( $this->is_view_all() ) { $url = false; + } return $url; } @@ -233,11 +239,11 @@ class view_all_posts_pages { $post->post_content = str_replace( "\n<!--nextpage-->\n", "\n\n", $post->post_content ); $post->post_content = str_replace( "\n<!--nextpage-->", "\n", $post->post_content ); $post->post_content = str_replace( "<!--nextpage-->\n", "\n", $post->post_content ); - $post->post_content = str_replace( "<!--nextpage-->", ' ', $post->post_content ); + $post->post_content = str_replace( '<!--nextpage-->', ' ', $post->post_content ); $pages = array( $post->post_content ); - $more = 1; + $more = 1; $multipage = 0; } } @@ -259,8 +265,9 @@ class view_all_posts_pages { $options = $this->get_options(); - if ( in_array( $post->post_type, $options[ 'wlp_post_types' ] ) && apply_filters( 'vapp_display_link', true, (int) $post->ID, $options, $post ) ) + if ( in_array( $post->post_type, $options['wlp_post_types'] ) && apply_filters( 'vapp_display_link', true, (int) $post->ID, $options, $post ) ) { add_filter( 'wp_link_pages_args', array( $this, 'filter_wp_link_pages_args' ), 999 ); + } return $args; } @@ -282,24 +289,26 @@ class view_all_posts_pages { if ( is_array( $options ) ) { extract( $options ); - //Set global $more to false so that wp_link_pages outputs links for all pages when viewing full post page - if ( $this->is_view_all() ) - $GLOBALS[ 'more' ] = false; + // Set global $more to false so that wp_link_pages outputs links for all pages when viewing full post page + if ( $this->is_view_all() ) { + $GLOBALS['more'] = false; + } - //Process link text, respecting pagelink parameter. - $link_text = str_replace( '%', $wlp_text, $args[ 'pagelink' ] ); + // Process link text, respecting pagelink parameter. + $link_text = str_replace( '%', $wlp_text, $args['pagelink'] ); - //View all - $link = ' ' . $args[ 'link_before' ]; + // View all + $link = ' ' . $args['link_before']; - if ( $this->is_view_all() ) + if ( $this->is_view_all() ) { $link .= '<span class="' . esc_attr( $wlp_class ) . '">' . $link_text . '</span><!-- .' . esc_attr( $wlp_class ) . ' -->'; - else + } else { $link .= '<a class="' . esc_attr( $wlp_class ) . '" href="' . esc_url( $this->url() ) . '">' . $link_text . '</a><!-- .' . esc_attr( $wlp_class ) . ' -->'; + } - $link .= $args[ 'link_after' ] . ' '; + $link .= $args['link_after'] . ' '; - $args[ 'after' ] = $link . $args[ 'after' ]; + $args['after'] = $link . $args['after']; } return $args; @@ -323,17 +332,18 @@ class view_all_posts_pages { global $post; - if ( ! $this->is_view_all() && is_array( $options ) && array_key_exists( 'link', $options ) && true === $options[ 'link' ] && in_array( $post->post_type, $options[ 'link_post_types' ] ) ) { + if ( ! $this->is_view_all() && is_array( $options ) && array_key_exists( 'link', $options ) && true === $options['link'] && in_array( $post->post_type, $options['link_post_types'] ) ) { extract( $options ); $link = '<p class="vapp_wrapper"><a class="' . esc_attr( $link_class ) . '" href="' . esc_url( $this->url() ) . '">' . esc_html( $link_text ) . '</a></p><!-- .vapp_wrapper -->'; - if ( 'above' == $link_position ) + if ( 'above' == $link_position ) { $content = $link . $content; - elseif ( 'below' == $link_position ) + } elseif ( 'below' == $link_position ) { $content = $content . $link; - elseif ( 'both' == $link_position ) + } elseif ( 'both' == $link_position ) { $content = $link . $content . $link; + } } return $content; @@ -367,7 +377,7 @@ class view_all_posts_pages { public function url( $post_id = false ) { $link = false; - //Get link base specific to page type being viewed + // Get link base specific to page type being viewed if ( is_singular() || in_the_loop() ) { $post_id = intval( $post_id ); @@ -376,17 +386,18 @@ class view_all_posts_pages { $post_id = $post->ID; } - if ( ! $post_id ) + if ( ! $post_id ) { return false; + } $link = get_permalink( $post_id ); - } - elseif ( is_home() || is_front_page() ) + } elseif ( is_home() || is_front_page() ) { $link = home_url( '/' ); - elseif ( is_category() ) + } elseif ( is_category() ) { $link = get_category_link( get_query_var( 'cat' ) ); - elseif ( is_tag() ) + } elseif ( is_tag() ) { $link = get_tag_link( get_query_var( 'tag_id' ) ); + } /** DISABLED FOR NOW AS PRINTING OF DATE-BASED ARCHIVES DOESN'T WORK YET elseif ( is_date() ) { $year = get_query_var( 'year' ); @@ -403,21 +414,22 @@ class view_all_posts_pages { elseif ( is_tax() ) { $queried_object = get_queried_object(); - if ( is_object( $queried_object ) && property_exists( $queried_object, 'taxonomy' ) && property_exists( $queried_object, 'term_id' ) ) - $link = get_term_link( (int)$queried_object->term_id, $queried_object->taxonomy ); + if ( is_object( $queried_object ) && property_exists( $queried_object, 'taxonomy' ) && property_exists( $queried_object, 'term_id' ) ) { + $link = get_term_link( (int) $queried_object->term_id, $queried_object->taxonomy ); + } } - //If link base is set, build link + // If link base is set, build link if ( false !== $link ) { global $wp_rewrite; if ( $wp_rewrite->using_permalinks() ) { $link = path_join( $link, $this->query_var ); - if ( $wp_rewrite->use_trailing_slashes ) + if ( $wp_rewrite->use_trailing_slashes ) { $link = trailingslashit( $link ); - } - else { + } + } else { $link = add_query_arg( $this->query_var, 1, $link ); } } @@ -451,7 +463,7 @@ class view_all_posts_pages { * @return string */ public function admin_options() { - ?> + ?> <div class="wrap"> <h2>View All Post's Pages</h2> @@ -471,20 +483,20 @@ class view_all_posts_pages { <tr> <th scope="row"><?php _e( 'Automatically append link to post\'s page navigation?', 'view_all_posts_pages' ); ?></th> <td> - <input type="radio" name="<?php echo $this->settings_key; ?>[wlp]" id="wlp-true" value="1"<?php checked( $options[ 'wlp' ], true, true ); ?> /> <label for="wlp-true"><?php _e( 'Yes', 'view_all_posts_pages' ); ?></label><br /> - <input type="radio" name="<?php echo $this->settings_key; ?>[wlp]" id="wlp-false" value="0"<?php checked( $options[ 'wlp' ], false, true ); ?> /> <label for="wlp-false"><?php _e( 'No', 'view_all_posts_pages' ); ?></label> + <input type="radio" name="<?php echo $this->settings_key; ?>[wlp]" id="wlp-true" value="1"<?php checked( $options['wlp'], true, true ); ?> /> <label for="wlp-true"><?php _e( 'Yes', 'view_all_posts_pages' ); ?></label><br /> + <input type="radio" name="<?php echo $this->settings_key; ?>[wlp]" id="wlp-false" value="0"<?php checked( $options['wlp'], false, true ); ?> /> <label for="wlp-false"><?php _e( 'No', 'view_all_posts_pages' ); ?></label> </td> </tr> <tr> <th scope="row"><label for="wlp_text"><?php _e( 'Link text:', 'view_all_posts_pages' ); ?></label></th> <td> - <input type="text" name="<?php echo $this->settings_key; ?>[wlp_text]" id="wlp_text" value="<?php echo esc_attr( $options[ 'wlp_text' ] ); ?>" class="regular-text" /> + <input type="text" name="<?php echo $this->settings_key; ?>[wlp_text]" id="wlp_text" value="<?php echo esc_attr( $options['wlp_text'] ); ?>" class="regular-text" /> </td> </tr> <tr> <th scope="row"><label for="wlp_class"><?php _e( 'Link\'s CSS class(es):', 'view_all_posts_pages' ); ?></label></th> <td> - <input type="text" name="<?php echo $this->settings_key;?>[wlp_class]" id="wlp_class" value="<?php echo esc_attr( $options[ 'wlp_class' ] ); ?>" class="regular-text" /> + <input type="text" name="<?php echo $this->settings_key; ?>[wlp_class]" id="wlp_class" value="<?php echo esc_attr( $options['wlp_class'] ); ?>" class="regular-text" /> <p class="description"><?php _e( 'Be aware that Internet Explorer will only interpret the first two CSS classes.', 'view_all_posts_pages' ); ?></p> </td> @@ -493,7 +505,12 @@ class view_all_posts_pages { <th scope="row"><?php _e( 'Display automatically on:', 'view_all_posts_pages' ); ?></th> <td> <?php foreach ( $post_types as $post_type ) : ?> - <input type="checkbox" name="<?php echo $this->settings_key; ?>[wlp_post_types][]" id="wlp-pt-<?php echo $post_type->name; ?>" value="<?php echo $post_type->name; ?>"<?php if ( in_array( $post_type->name, $options[ 'wlp_post_types' ] ) ) echo ' checked="checked"'; ?> /> <label for="wlp-pt-<?php echo $post_type->name; ?>"><?php echo $post_type->labels->name; ?></label><br /> + <input type="checkbox" name="<?php echo $this->settings_key; ?>[wlp_post_types][]" id="wlp-pt-<?php echo $post_type->name; ?>" value="<?php echo $post_type->name; ?>" + <?php + if ( in_array( $post_type->name, $options['wlp_post_types'] ) ) { + echo ' checked="checked"';} + ?> + /> <label for="wlp-pt-<?php echo $post_type->name; ?>"><?php echo $post_type->labels->name; ?></label><br /> <?php endforeach; ?> </td> </tr> @@ -507,36 +524,41 @@ class view_all_posts_pages { <tr> <th scope="row"><?php _e( 'Automatically add links based on settings below?', 'view_all_posts_pages' ); ?></th> <td> - <input type="radio" name="<?php echo $this->settings_key; ?>[link]" id="link-true" value="1"<?php checked( $options[ 'link' ], true, true ); ?> /> <label for="link-true"><?php _e( 'Yes', 'view_all_posts_pages' ); ?></label><br /> - <input type="radio" name="<?php echo $this->settings_key; ?>[link]" id="link-false" value="0"<?php checked( $options[ 'link' ], false, true ); ?> /> <label for="link-false"><?php _e( 'No', 'view_all_posts_pages' ); ?></label> + <input type="radio" name="<?php echo $this->settings_key; ?>[link]" id="link-true" value="1"<?php checked( $options['link'], true, true ); ?> /> <label for="link-true"><?php _e( 'Yes', 'view_all_posts_pages' ); ?></label><br /> + <input type="radio" name="<?php echo $this->settings_key; ?>[link]" id="link-false" value="0"<?php checked( $options['link'], false, true ); ?> /> <label for="link-false"><?php _e( 'No', 'view_all_posts_pages' ); ?></label> </td> </tr> <tr> <th scope="row"><?php _e( 'Automatically place link:', 'view_all_posts_pages' ); ?></th> <td> - <input type="radio" name="<?php echo $this->settings_key; ?>[link_position]" id="link_position-above" value="above"<?php checked( $options[ 'link_position' ], 'above', true ); ?> /> <label for="link_position-above"><?php _e( 'Above content', 'view_all_posts_pages' ); ?></label><br /> - <input type="radio" name="<?php echo $this->settings_key; ?>[link_position]" id="link_position-below" value="below"<?php checked( $options[ 'link_position' ], 'below', true ); ?> /> <label for="link_position-below"><?php _e( 'Below content', 'view_all_posts_pages' ); ?></label><br /> - <input type="radio" name="<?php echo $this->settings_key; ?>[link_position]" id="link_position-both" value="both"<?php checked( $options[ 'link_position' ], 'both', true ); ?> /> <label for="link_position-both"><?php _e( 'Above and below content', 'view_all_posts_pages' ); ?></label> + <input type="radio" name="<?php echo $this->settings_key; ?>[link_position]" id="link_position-above" value="above"<?php checked( $options['link_position'], 'above', true ); ?> /> <label for="link_position-above"><?php _e( 'Above content', 'view_all_posts_pages' ); ?></label><br /> + <input type="radio" name="<?php echo $this->settings_key; ?>[link_position]" id="link_position-below" value="below"<?php checked( $options['link_position'], 'below', true ); ?> /> <label for="link_position-below"><?php _e( 'Below content', 'view_all_posts_pages' ); ?></label><br /> + <input type="radio" name="<?php echo $this->settings_key; ?>[link_position]" id="link_position-both" value="both"<?php checked( $options['link_position'], 'both', true ); ?> /> <label for="link_position-both"><?php _e( 'Above and below content', 'view_all_posts_pages' ); ?></label> </td> </tr> <tr> <th scope="row"><?php _e( 'Display automatically on:', 'view_all_posts_pages' ); ?></th> <td> <?php foreach ( $post_types as $post_type ) : ?> - <input type="checkbox" name="<?php echo $this->settings_key; ?>[link_post_types][]" id="link-pt-<?php echo $post_type->name; ?>" value="<?php echo $post_type->name; ?>"<?php if ( in_array( $post_type->name, $options[ 'link_post_types' ] ) ) echo ' checked="checked"'; ?> /> <label for="link-pt-<?php echo $post_type->name; ?>"><?php echo $post_type->labels->name; ?></label><br /> + <input type="checkbox" name="<?php echo $this->settings_key; ?>[link_post_types][]" id="link-pt-<?php echo $post_type->name; ?>" value="<?php echo $post_type->name; ?>" + <?php + if ( in_array( $post_type->name, $options['link_post_types'] ) ) { + echo ' checked="checked"';} + ?> + /> <label for="link-pt-<?php echo $post_type->name; ?>"><?php echo $post_type->labels->name; ?></label><br /> <?php endforeach; ?> </td> </tr> <tr> <th scope="row"><label for="link_text"><?php _e( 'Link text:', 'view_all_posts_pages' ); ?></label></th> <td> - <input type="text" name="<?php echo $this->settings_key; ?>[link_text]" id="link_text" value="<?php echo esc_attr( $options[ 'link_text' ] ); ?>" class="regular-text" /> + <input type="text" name="<?php echo $this->settings_key; ?>[link_text]" id="link_text" value="<?php echo esc_attr( $options['link_text'] ); ?>" class="regular-text" /> </td> </tr> <tr> <th scope="row"><label for="link_class"><?php _e( 'Link\'s CSS class(es):', 'view_all_posts_pages' ); ?></label></th> <td> - <input type="text" name="<?php echo $this->settings_key; ?>[link_class]" id="link_class" value="<?php echo esc_attr( $options[ 'link_class' ] ); ?>" class="regular-text" /> + <input type="text" name="<?php echo $this->settings_key; ?>[link_class]" id="link_class" value="<?php echo esc_attr( $options['link_class'] ); ?>" class="regular-text" /> <p class="description"><?php _e( 'Be aware that Internet Explorer will only interpret the first two CSS classes.', 'view_all_posts_pages' ); ?></p> </td> @@ -544,7 +566,7 @@ class view_all_posts_pages { <tr> <th scope="row"><label for="link_priority"><?php _e( 'Link\'s priority:', 'view_all_posts_pages' ); ?></label></th> <td> - <input type="text" name="<?php echo $this->settings_key; ?>[link_priority]" id="link_priority" class="small-text code" value="<?php echo esc_attr( $options[ 'link_priority' ] ); ?>" /> + <input type="text" name="<?php echo $this->settings_key; ?>[link_priority]" id="link_priority" class="small-text code" value="<?php echo esc_attr( $options['link_priority'] ); ?>" /> <p class="description"><?php _e( 'Priority determines when the link is added to a post\'s content. You can use the above setting to modulate the link\'s placement.', 'view_all_posts_pages' ); ?></p> <p class="description"><?php _e( 'The default value is <strong>10</strong>. Lower values mean the link will be added earlier, while higher values will add the link later.', 'view_all_posts_pages' ); ?></p> @@ -556,7 +578,7 @@ class view_all_posts_pages { </form> </div><!-- .wrap --> - <?php + <?php } /** @@ -574,21 +596,21 @@ class view_all_posts_pages { if ( is_array( $options ) ) { foreach ( $options as $key => $value ) { - switch( $key ) { + switch ( $key ) { case 'wlp': case 'link': - $new_options[ $key ] = (bool)$value; - break; + $new_options[ $key ] = (bool) $value; + break; case 'link_position': $placements = array( 'above', 'below', - 'both' + 'both', ); $new_options[ $key ] = in_array( $value, $placements ) ? $value : 'below'; - break; + break; case 'wlp_post_types': case 'link_post_types': @@ -598,11 +620,12 @@ class view_all_posts_pages { if ( is_array( $value ) && is_array( $post_types ) ) { foreach ( $post_types as $post_type ) { - if ( in_array( $post_type->name, $value ) ) + if ( in_array( $post_type->name, $value ) ) { $new_options[ $key ][] = $post_type->name; + } } } - break; + break; case 'wlp_text': case 'wlp_class': @@ -610,20 +633,21 @@ class view_all_posts_pages { case 'link_class': $value = sanitize_text_field( $value ); - if ( ( 'wlp_text' == $key || 'link_text' == $key ) && empty( $value ) ) + if ( ( 'wlp_text' == $key || 'link_text' == $key ) && empty( $value ) ) { $value = 'View all'; + } $new_options[ $key ] = $value; - break; + break; case 'link_priority': $value = absint( $value ); $new_options[ $key ] = $value; - break; + break; default: - break; + break; } } } @@ -641,14 +665,17 @@ class view_all_posts_pages { private function get_options() { $options = get_option( $this->settings_key, $this->settings_defaults ); - if ( ! is_array( $options ) ) + if ( ! is_array( $options ) ) { $options = array(); + } - if ( ! array_key_exists( 'wlp_post_types', $options ) ) - $options[ 'wlp_post_types' ] = array(); + if ( ! array_key_exists( 'wlp_post_types', $options ) ) { + $options['wlp_post_types'] = array(); + } - if ( ! array_key_exists( 'link_post_types', $options ) ) - $options[ 'link_post_types' ] = array(); + if ( ! array_key_exists( 'link_post_types', $options ) ) { + $options['link_post_types'] = array(); + } return wp_parse_args( $options, $this->settings_defaults ); } @@ -662,8 +689,9 @@ class view_all_posts_pages { private function post_types_array() { $post_types = array(); foreach ( get_post_types( array(), 'objects' ) as $post_type ) { - if ( false == $post_type->_builtin || 'post' == $post_type->name || 'page' == $post_type->name ) + if ( false == $post_type->_builtin || 'post' == $post_type->name || 'page' == $post_type->name ) { $post_types[] = $post_type; + } } return $post_types; @@ -683,7 +711,7 @@ class view_all_posts_pages { */ public function action_admin_notices_activation() { if ( ! get_option( $this->notice_key ) && apply_filters( 'vapp_display_rewrite_rules_notice', true ) ) : - ?> + ?> <div id="wpf-rewrite-flush-warning" class="error fade"> <p><strong><?php _e( 'View All Post\'s Pages', 'view_all_posts_pages' ); ?></strong></p> @@ -693,7 +721,7 @@ class view_all_posts_pages { <p><?php printf( __( 'When finished, click <a href="%s">here</a> to hide this message.', 'view_all_posts_pages' ), esc_url( admin_url( add_query_arg( $this->notice_key, 1, 'index.php' ) ) ) ); ?></p> </div> - <?php + <?php endif; } }