coding standards part 1

This commit is contained in:
Stef Kariotidis 2016-11-21 19:12:36 +02:00
parent 84700d5ff2
commit 74b1de1722
13 changed files with 465 additions and 250 deletions

View File

@ -0,0 +1,146 @@
<?php
/**
* Based on Roots Sage Gallery: https://github.com/roots/sage/blob/5b9786b8ceecfe717db55666efe5bcf0c9e1801c/lib/gallery.php
*
* @package understrap
*/
// Remove built in shortcode.
remove_shortcode( 'gallery', 'gallery_shortcode' );
/**
* Replaces gallery with custom shortcode.
*
* @param mixed $attr Shortcode parameters.
*
* @return mixed|string|void
*/
function shortcode_gallery( $attr ) {
$post = get_post();
static $instance = 0;
$instance ++;
if ( ! empty( $attr['ids'] ) ) {
if ( empty( $attr['orderby'] ) ) {
$attr['orderby'] = 'post__in';
}
$attr['include'] = $attr['ids'];
}
$output = apply_filters( 'post_gallery', '', $attr );
if ( $output != '' ) {
return $output;
}
if ( isset( $attr['orderby'] ) ) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
if ( ! $attr['orderby'] ) {
unset( $attr['orderby'] );
}
}
extract( shortcode_atts( [
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => '',
'icontag' => '',
'captiontag' => '',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => '',
'link' => '',
], $attr ) );
$id = intval( $id );
$columns = ( 12 % $columns == 0 ) ? $columns : 3;
$grid = sprintf( 'col-sm-%1$s col-lg-%1$s', 12 / $columns );
if ( $order === 'RAND' ) {
$orderby = 'none';
}
if ( ! empty( $include ) ) {
$_attachments = get_posts( [
'include' => $include,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $order,
'orderby' => $orderby
] );
$attachments = [];
foreach ( $_attachments as $key => $val ) {
$attachments[ $val->ID ] = $_attachments[ $key ];
}
} elseif ( ! empty( $exclude ) ) {
$attachments = get_children( [
'post_parent' => $id,
'exclude' => $exclude,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $order,
'orderby' => $orderby
] );
} else {
$attachments = get_children( [
'post_parent' => $id,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $order,
'orderby' => $orderby
] );
}
if ( empty( $attachments ) ) {
return '';
}
if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $att_id => $attachment ) {
$output .= wp_get_attachment_link( $att_id, $size, true ) . "\n";
}
return $output;
}
$unique = ( get_query_var( 'page' ) ) ? $instance . '-p' . get_query_var( 'page' ) : $instance;
$output = '<div class="gallery gallery-' . $id . '-' . $unique . '">';
$i = 0;
foreach ( $attachments as $id => $attachment ) {
switch ( $link ) {
case 'file':
$image = wp_get_attachment_link( $id, $size, false, false );
break;
case 'none':
$image = wp_get_attachment_image( $id, $size, false, [ 'class' => 'thumbnail img-thumbnail' ] );
break;
default:
$image = wp_get_attachment_link( $id, $size, true, false );
break;
}
$output .= ( $i % $columns == 0 ) ? '<div class="row gallery-row">' : '';
$output .= '<div class="' . $grid . '">' . $image;
if ( trim( $attachment->post_excerpt ) ) {
$output .= '<div class="caption hidden">' . wptexturize( $attachment->post_excerpt ) . '</div>';
}
$output .= '</div>';
$i ++;
$output .= ( $i % $columns == 0 ) ? '</div>' : '';
}
$output .= ( $i % $columns != 0 ) ? '</div>' : '';
$output .= '</div>';
return $output;
}
add_shortcode( 'gallery', 'shortcode_gallery' );
/**
* Add class="thumbnail img-thumbnail" to attachment items
*
* @param string $html Markup.
*
* @return mixed
*/
function attachment_link_class( $html ) {
$html = str_replace( '<a', '<a class="thumbnail img-thumbnail"', $html );
return $html;
}
add_filter( 'wp_get_attachment_link', 'attachment_link_class', 10, 1 );

View File

@ -165,14 +165,15 @@ if ( ! function_exists( 'understrap_theme_customize_register' ) ) {
// Columns setup for grid posts.
/**
* Function and callback to check when grid is enabled.
*
* @return bool
*/
function is_grid_enabled ()
{
function is_grid_enabled() {
return 'grid' == get_theme_mod( 'understrap_posts_index_style' );
}
if ( is_grid_enabled() ) {
// How many columns to use each grid post
// How many columns to use each grid post.
$wp_customize->add_setting( 'understrap_grid_post_columns', array(
'default' => '6',
'type' => 'theme_mod',
@ -185,7 +186,7 @@ if ( ! function_exists( 'understrap_theme_customize_register' ) ) {
$wp_customize,
'understrap_grid_post_columns', array(
'label' => __( 'Grid Post Columns', 'understrap' ),
'description' => __( "Choose how many columns to use in grid posts", 'understrap' ),
'description' => __( 'Choose how many columns to use in grid posts', 'understrap' ),
'section' => 'understrap_theme_layout_options',
'settings' => 'understrap_grid_post_columns',
'type' => 'select',
@ -203,10 +204,10 @@ if ( ! function_exists( 'understrap_theme_customize_register' ) ) {
) );
}
// hook to auto-hide/show depending the understrap_posts_index_style option
// hook to auto-hide/show depending the understrap_posts_index_style option.
$wp_customize->get_control( 'understrap_grid_post_columns' )->active_callback = 'is_grid_enabled';
}
}
} // endif function_exists( 'understrap_theme_customize_register' ).
add_action( 'customize_register', 'understrap_theme_customize_register' );
@ -214,6 +215,9 @@ add_action( 'customize_register', 'understrap_theme_customize_register' );
* Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
*/
if ( ! function_exists( 'understrap_customize_preview_js' ) ) {
/**
* Setup JS integration for live previewing.
*/
function understrap_customize_preview_js() {
wp_enqueue_script( 'understrap_customizer', get_template_directory_uri() . '/js/customizer.js',
array( 'customize-preview' ), '20130508', true );

View File

@ -1,15 +1,20 @@
<?php
/**
* understrap enqueue scripts
* Understrap enqueue scripts
*
* @package understrap
*/
if ( ! function_exists( 'understrap_scripts' ) ) {
/**
* Load theme's JavaScript sources.
*/
function understrap_scripts() {
wp_enqueue_style( 'understrap-styles', get_stylesheet_directory_uri() . '/css/theme.min.css', array(), '0.4.9' );
wp_enqueue_style( 'understrap-styles', get_stylesheet_directory_uri() . '/css/theme.min.css', array(),
'0.4.9' );
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'understrap-scripts', get_template_directory_uri() . '/js/theme.min.js', array(), '0.4.9', true );
wp_enqueue_script( 'understrap-scripts', get_template_directory_uri() . '/js/theme.min.js', array(), '0.4.9',
true );
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
@ -17,7 +22,7 @@ if ( ! function_exists ( 'understrap_scripts' ) ) {
// menu - vertical page association
// do not load on WooCommerce pages
// do not load if we are in WooCommerce pages
// do not load if we are in WooCommerce pages.
$loadit = true;
if ( class_exists( 'WooCommerce' ) ) {
if ( is_woocommerce() ) {
@ -25,40 +30,45 @@ if ( ! function_exists ( 'understrap_scripts' ) ) {
}
}
if ( is_page_template( ( 'page-templates/vertical-one-page.php' ) || is_home() || is_single() ) && $loadit ) {
wp_enqueue_script( 'vertical-one-page', get_template_directory_uri() . '/js/vertical-one-page.js', array( 'jquery' ), '0.4.9', true );
wp_enqueue_script( 'vertical-one-page', get_template_directory_uri() . '/js/vertical-one-page.js',
array( 'jquery' ), '0.4.9', true );
$page_for_posts = strtolower( get_the_title( get_option( 'page_for_posts' ) ) );
$home_url = home_url();
$is_single = is_single();
$vars = array(
'pageForPosts' => $page_for_posts,
'homeUrl' => $home_url,
'isSingle' => $is_single
'isSingle' => $is_single,
);
wp_localize_script( 'vertical-one-page', 'vars', $vars );
}
// menu - vertical page association end
}
// menu - vertical page association end.
}
} // endif function_exists( 'understrap_scripts' ).
add_action( 'wp_enqueue_scripts', 'understrap_scripts' );
/**
*Loading slider script conditionally
**/
*/
if ( is_active_sidebar( 'hero' ) ) :
add_action( "wp_enqueue_scripts", "understrap_slider" );
add_action( 'wp_enqueue_scripts', 'understrap_slider' );
if ( ! function_exists( 'understrap_slider' ) ) {
/**
* Setup slider.
*/
function understrap_slider() {
if ( is_front_page() ) {
$data = array(
"timeout" => intval( get_theme_mod( 'understrap_theme_slider_time_setting', 5000 ) ),
"items" => intval( get_theme_mod( 'understrap_theme_slider_count_setting', 1 ) )
'timeout' => intval( get_theme_mod( 'understrap_theme_slider_time_setting', 5000 ) ),
'items' => intval( get_theme_mod( 'understrap_theme_slider_count_setting', 1 ) ),
);
wp_enqueue_script( "understrap-slider-script", get_stylesheet_directory_uri() . '/js/slider_settings.js', array(), '0.4.9' );
wp_localize_script( "understrap-slider-script", "understrap_slider_variables", $data );
wp_enqueue_script( 'understrap-slider-script',
get_stylesheet_directory_uri() . '/js/slider_settings.js', array(), '0.4.9' );
wp_localize_script( 'understrap-slider-script', 'understrap_slider_variables', $data );
}
}
}

View File

@ -6,14 +6,15 @@
*
* @package understrap
*/
if ( ! function_exists( 'understrap_body_classes' ) ) {
/**
* Adds custom classes to the array of body classes.
*
* @param array $classes Classes for the body element.
*
* @return array
*/
if ( ! function_exists ( 'understrap_body_classes' ) ) {
function understrap_body_classes( $classes ) {
// Adds a class of group-blog to blogs with more than 1 published author.
if ( is_multi_author() ) {
@ -23,20 +24,29 @@ if ( ! function_exists ( 'understrap_body_classes' ) ) {
if ( ! is_singular() ) {
$classes[] = 'hfeed';
}
return $classes;
}
}
add_filter( 'body_class', 'understrap_body_classes' );
// Removes tag class from the body_class array to avoid Bootstrap markup styling issues.
add_filter( 'body_class', 'adjust_body_class' );
if ( ! function_exists( 'adjust_body_class' ) ) {
/**
* Setup body classes.
*
* @param string $classes CSS classes.
*
* @return mixed
*/
function adjust_body_class( $classes ) {
foreach ( $classes as $key => $value ) {
if ( $value == 'tag' ) unset( $classes[ $key ] );
if ( $value == 'tag' ) {
unset( $classes[ $key ] );
}
}
return $classes;
@ -44,14 +54,21 @@ if ( ! function_exists ( 'adjust_body_class' ) ) {
}
}
// Filter custom logo with correct classes
// Filter custom logo with correct classes.
add_filter( 'get_custom_logo', 'change_logo_class' );
if ( ! function_exists( 'change_logo_class' ) ) {
function change_logo_class($html)
{
/**
* Replaces logo CSS class.
*
* @param string $html Markup.
*
* @return mixed
*/
function change_logo_class( $html ) {
$html = str_replace( 'class="custom-logo"', 'class="img-responsive"', $html );
$html = str_replace( 'class="custom-logo-link"', 'class="navbar-brand custom-logo-link"', $html );
return $html;
}
}

View File

@ -6,13 +6,14 @@
*
* @package understrap
*/
if ( ! function_exists( 'understrap_jetpack_setup' ) ) {
/**
* Jetpack setup function.
*
* See: https://jetpack.com/support/infinite-scroll/
* See: https://jetpack.com/support/responsive-videos/
*/
if ( ! function_exists ( 'understrap_jetpack_setup' ) ) {
function understrap_jetpack_setup() {
// Add theme support for Infinite Scroll.
add_theme_support( 'infinite-scroll', array(
@ -25,10 +26,10 @@ if ( ! function_exists ( 'understrap_jetpack_setup' ) ) {
}
}
add_action( 'after_setup_theme', 'understrap_jetpack_setup' );
if ( ! function_exists( 'understrap_infinite_scroll_render' ) ) {
/**
* Custom render function for Infinite Scroll.
*/
if ( ! function_exists ( 'understrap_infinite_scroll_render' ) ) {
function understrap_infinite_scroll_render() {
while ( have_posts() ) {
the_post();

View File

@ -1,11 +1,19 @@
<?php
/* Inspired by Simon Bradburys cleanup.php fromb4st theme https://github.com/SimonPadbury/b4st */
/*
Removes the generator tag with WP version numbers. Hackers will use this to find weak and old WP installs
/**
* Inspired by Simon Bradburys cleanup.php fromb4st theme https://github.com/SimonPadbury/b4st
*
* @package understrap
*/
/**
* Removes the generator tag with WP version numbers. Hackers will use this to find weak and old WP installs
*
* @return string
*/
function no_generator() {
return '';
}
add_filter( 'the_generator', 'no_generator' );
/*
@ -19,11 +27,15 @@ remove_action('wp_head', 'feed_links', 2);
remove_action( 'wp_head', 'feed_links_extra', 3 );
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
/*
Show less info to users on failed login for security.
(Will not let a valid username be known.)
/**
* Show less info to users on failed login for security.
* (Will not let a valid username be known.)
*
* @return string
*/
function show_less_login_info() {
return "<strong>ERROR</strong>: Stop guessing!";
return '<strong>ERROR</strong>: Stop guessing!';
}
add_filter( 'login_errors', 'show_less_login_info' );

View File

@ -1,11 +1,13 @@
<?php
/**
* Set the content width based on the theme's design and stylesheet.
* Theme basic setup.
*
* @package understrap
*/
require get_template_directory() . '/inc/theme-settings.php';
// Set the content width based on the theme's design and stylesheet.
if ( ! isset( $content_width ) ) {
$content_width = 640; /* pixels */
}
@ -19,7 +21,6 @@ if ( ! function_exists( 'understrap_setup' ) ) :
* as indicating support for post thumbnails.
*/
function understrap_setup() {
/*
* Make theme available for translation.
* Translations can be filed in the /languages/ directory.
@ -56,20 +57,28 @@ function understrap_setup() {
* to output valid HTML5.
*/
add_theme_support( 'html5', array(
'search-form', 'comment-form', 'comment-list', 'gallery', 'caption',
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
) );
/*
* Adding Thumbnail basic support
*/
add_theme_support( "post-thumbnails" );
add_theme_support( 'post-thumbnails' );
/*
* Enable support for Post Formats.
* See http://codex.wordpress.org/Post_Formats
*/
add_theme_support( 'post-formats', array(
'aside', 'image', 'video', 'quote', 'link',
'aside',
'image',
'video',
'quote',
'link',
) );
// Set up the WordPress core custom background feature.
@ -84,29 +93,35 @@ function understrap_setup() {
// Check and setup theme default settings.
setup_theme_default_settings();
}
endif; // understrap_setup
endif; // understrap_setup.
add_action( 'after_setup_theme', 'understrap_setup' );
/**
* Adding the Read more link to excerpts
*/
/*function new_excerpt_more( $more ) {
return ' <p><a class="read-more btn btn-default" href="'. get_permalink( get_the_ID() ) . '">' . __('Read More', 'understrap') . '</a></p>';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );*/
/* Removes the ... from the excerpt read more link */
if ( ! function_exists( 'custom_excerpt_more' ) ) {
/**
* Removes the ... from the excerpt read more link
*
* @param string $more The excerpt.
*
* @return string
*/
function custom_excerpt_more( $more ) {
return '';
}
}
add_filter( 'excerpt_more', 'custom_excerpt_more' );
/* Adds a custom read more link to all excerpts, manually or automatically generated */
if ( ! function_exists( 'all_excerpts_get_more_link' ) ) {
/**
* Adds a custom read more link to all excerpts, manually or automatically generated
*
* @param string $post_excerpt Posts's excerpt.
*
* @return string
*/
function all_excerpts_get_more_link( $post_excerpt ) {
return $post_excerpt . ' [...]<p><a class="btn btn-secondary understrap-read-more-link" href="'. get_permalink( get_the_ID() ) . '">' . __('Read More...', 'understrap') . '</a></p>';
return $post_excerpt . ' [...]<p><a class="btn btn-secondary understrap-read-more-link" href="' . get_permalink( get_the_ID() ) . '">' . __( 'Read More...',
'understrap' ) . '</a></p>';
}
}
add_filter( 'wp_trim_excerpt', 'all_excerpts_get_more_link' );

View File

@ -7,7 +7,6 @@
* @package understrap
*/
if ( ! function_exists( 'understrap_posted_on' ) ) :
/**
* Prints HTML with meta information for the current post-date/time and author.
@ -15,7 +14,8 @@ if ( ! function_exists( 'understrap_posted_on' ) ) :
function understrap_posted_on() {
$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>, <time class="updated" datetime="%3$s">' . __( ' Edited %4$s', 'understrap' ) . '</time>';
$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>, <time class="updated" datetime="%3$s">' . __( ' Edited %4$s',
'understrap' ) . '</time>';
}
$time_string = sprintf( $time_string,
@ -35,7 +35,7 @@ function understrap_posted_on() {
'<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
);
echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>';
echo '<span class="posted-on">' . esc_html( $posted_on ) . '</span><span class="byline"> ' . esc_html( $byline ) . '</span>';
}
endif;
@ -50,7 +50,8 @@ function understrap_entry_footer() {
/* translators: used between list items, there is a space after the comma */
$categories_list = get_the_category_list( __( ', ', 'understrap' ) );
if ( $categories_list && understrap_categorized_blog() ) {
printf( '<span class="cat-links">' . __( 'Posted in %1$s', 'understrap' ) . '</span>', $categories_list );
printf( '<span class="cat-links">' . __( 'Posted in %1$s', 'understrap' ) . '</span>',
$categories_list );
}
/* translators: used between list items, there is a space after the comma */
@ -62,7 +63,8 @@ function understrap_entry_footer() {
if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
echo '<span class="comments-link">';
comments_popup_link( __( 'Leave a comment', 'understrap' ), __( '1 Comment', 'understrap' ), __( '% Comments', 'understrap' ) );
comments_popup_link( __( 'Leave a comment', 'understrap' ), __( '1 Comment', 'understrap' ),
__( '% Comments', 'understrap' ) );
echo '</span>';
}
@ -119,5 +121,6 @@ function understrap_category_transient_flusher() {
// Like, beat it. Dig?
delete_transient( 'understrap_categories' );
}
add_action( 'edit_category', 'understrap_category_transient_flusher' );
add_action( 'save_post', 'understrap_category_transient_flusher' );

View File

@ -6,7 +6,6 @@ function setup_theme_default_settings() {
// check if settings are set, if not set defaults.
// Caution: DO NOT check existence using === always check with == .
// Latest blog posts style.
$understrap_posts_index_style = get_theme_mod( 'understrap_posts_index_style' );
if ( '' == $understrap_posts_index_style ) {

View File

@ -1,13 +1,17 @@
<?php
/**
* Utility functions
*
* @package understrap
*/
/**
* Generate a custom length excerpt.
* If the content of the post is less than the provided length,
* the entire content is returned.
* @param $word_count
*
* @param int $post_id Post's ID.
* @param int $word_count How many words to keep.
*
* @return string
*/
@ -20,7 +24,7 @@ function understrap_excerpt_with_length( $post_id, $word_count ) {
$words = str_word_count( $content, 2 );
$keys = array_keys( $words );
$excerpt = substr( $content, 0, $keys[ $word_count ] );
$link_class = " class=\"btn btn-secondary understrap-read-more-link\"";
$link_class = ' class=\"btn btn-secondary understrap-read-more-link\"';
$excerpt = '<p>' . $excerpt . '[...]</p>';
$excerpt .= '<p><a href="' . $permalink . '"' . $link_class . '>Read More</a></p>';
} else {

View File

@ -2,10 +2,13 @@
/**
* Declaring widgets
*
*
* @package understrap
*/
if ( ! function_exists( 'understrap_widgets_init' ) ) {
/**
* Initializes themes widgets.
*/
function understrap_widgets_init() {
register_sidebar( array(
'name' => __( 'Right Sidebar', 'understrap' ),
@ -58,5 +61,5 @@ if ( ! function_exists( 'understrap_widgets_init' ) ) {
) );
}
}
} // endif function_exists( 'understrap_widgets_init' ).
add_action( 'widgets_init', 'understrap_widgets_init' );

View File

@ -2,15 +2,17 @@
/**
* Add WooCommerce support
*
*
* @package understrap
*/
add_action( 'after_setup_theme', 'woocommerce_support' );
if ( ! function_exists( 'woocommerce_support' ) ) {
/**
* Declares WooCommerce theme support.
*/
function woocommerce_support() {
add_theme_support( 'woocommerce' );
// hook in and customizer form fields
// hook in and customizer form fields.
add_filter( 'woocommerce_form_field_args', 'wc_form_field_args', 10, 3 );
}
}
@ -19,16 +21,15 @@ if ( ! function_exists( 'woocommerce_support' ) ) {
* Filter hook function monkey patching form classes
* Author: Adriano Monecchi http://stackoverflow.com/a/36724593/307826
*
* @param $args
* @param $key
* @param null $value
* @param string $args Form attributes.
* @param string $key Not in use.
* @param null $value Not in use.
*
* @return mixed
*/
function wc_form_field_args( $args, $key, $value = null ) {
// Start field type switch case
// Start field type switch case.
switch ( $args['type'] ) {
/* Targets all select input type elements, except the country and state select input types */
@ -99,7 +100,7 @@ function wc_form_field_args( $args, $key, $value = null ) {
$args['input_class'] = array( 'form-control', 'input-lg' );
$args['label_class'] = array( 'control-label' );
break;
}
} // end switch ($args).
return $args;
}

View File

@ -7,12 +7,12 @@
* @package understrap
*/
if ( ! function_exists( 'understrap_wpcom_setup' ) ) {
/**
* Adds support for wp.com-specific theme functions.
*
* @global array $themecolors
* @global array $themecolors Array with theme's colors.
*/
if ( ! function_exists ( 'understrap_wpcom_setup' ) ) {
function understrap_wpcom_setup() {
global $themecolors;