Skip to content

Commit

Permalink
Code updates to better confirm to coding standards
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarshall511 committed Jun 12, 2023
1 parent 9b3f881 commit 275fdd9
Show file tree
Hide file tree
Showing 18 changed files with 586 additions and 491 deletions.
53 changes: 27 additions & 26 deletions eight-day-week.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* Author URI: https://10up.com
* License: GPLv2+
* Text Domain: eight-day-week-print-workflow
*
* @package eight-day-week
*/

/**
Expand All @@ -27,26 +29,26 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

//load vip compat functions
// load vip compat functions
require_once __DIR__ . '/vip-compat.php';

//load plugin loader
// load plugin loader
require_once __DIR__ . '/plugins.php';

// Useful global constants
define( 'EDW_VERSION', '1.2.1' );
define( 'EDW_URL', Eight_Day_Week\plugins_url( __FILE__ ) );
define( 'EDW_PATH', dirname( __FILE__ ) . '/' );
define( 'EDW_INC', EDW_PATH . 'includes/' );
define( 'EDW_URL', Eight_Day_Week\plugins_url( __FILE__ ) );
define( 'EDW_PATH', __DIR__ . '/' );
define( 'EDW_INC', EDW_PATH . 'includes/' );

//allow override from wp-config (et al)
if( ! defined( 'EDW_PRODUCTION' ) ) {
// allow override from wp-config (et al)
if ( ! defined( 'EDW_PRODUCTION' ) ) {

//if on VIP, let VIP define production state
// if on VIP, let VIP define production state
if ( defined( 'WPCOM_IS_VIP_ENV' ) ) {
define( 'EDW_PRODUCTION', WPCOM_IS_VIP_ENV );
} else {
//otherwise, assume production
// otherwise, assume production
define( 'EDW_PRODUCTION', true );
}
}
Expand All @@ -72,7 +74,7 @@ function edw_bootstrap() {

$core_file = EDW_INC . 'functions' . DIRECTORY_SEPARATOR . 'core.php';

if( ! isset( $map[ $core_file ] ) ) {
if ( ! isset( $map[ $core_file ] ) ) {
return;
}

Expand All @@ -81,25 +83,24 @@ function edw_bootstrap() {

unset( $map[ $core_file ] );

foreach( $map as $file => $namespace ) {
foreach ( $map as $file => $namespace ) {

//play nice
try{
// play nice
try {
require_once $file;
$setup = $namespace . '\setup';

//allow files *not* to have a setup function
// allow files *not* to have a setup function
if ( function_exists( $setup ) ) {
$setup();
do_action( $namespace . '\setup' );
}
} catch( \Exception $e ) {
} catch ( \Exception $e ) {

}
}

}
//hook before init so the plugin has its own "init" action
// hook before init so the plugin has its own "init" action
add_action( 'after_setup_theme', 'edw_bootstrap' );

/**
Expand All @@ -118,39 +119,39 @@ function edw_build_namespace_map() {
RecursiveIteratorIterator::CATCH_GET_CHILD
);

$map = [];
$map = array();
foreach ( $Iterator as $file ) {

if ( 'php' !== $file->getExtension() ) {
continue;
}

//get just the file name, e.g. "core"
// get just the file name, e.g. "core"
$file_name = str_replace( '.php', '', $file->getFileName() );

//convert dashes to spaces
// convert dashes to spaces
$spaced = str_replace( '-', ' ', $file_name );

//so that ucwords will work
// so that ucwords will work
$capitalized = ucwords( $spaced );

//get the "end" namespace
// get the "end" namespace
$tip_of_the_iceberg = str_replace( ' ', '_', $capitalized );

$path = $file->getPathInfo()->getPathname();
if( $dir !== $path ) {
if ( $dir !== $path ) {
$sub_directory = str_replace( $dir . DIRECTORY_SEPARATOR, '', $path );

//convert slashes to spaces
// convert slashes to spaces
$capitalized = ucwords( str_replace( DIRECTORY_SEPARATOR, ' ', $sub_directory ) );

$tip_of_the_iceberg = str_replace( ' ', '\\', $capitalized ) . '\\' . $tip_of_the_iceberg;
}

//add the namespace prefix + convert spaces to underscores for the final namespace
// add the namespace prefix + convert spaces to underscores for the final namespace
$namespace = '\Eight_Day_Week\\' . $tip_of_the_iceberg;

//add it all the map
// add it all the map
$map[ $file->getPathname() ] = $namespace;
}

Expand Down
29 changes: 18 additions & 11 deletions includes/functions/admin-menu-page.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?php
/**
* Handles the admin menu functionality
*
* @package eight-day-week
*/

namespace Eight_Day_Week\Admin_Menu_Page;

/**
Expand All @@ -12,25 +18,27 @@
function setup() {
function ns( $function ) {
return __NAMESPACE__ . "\\$function";
};
}

function a( $function ) {
add_action( $function, ns( $function ) );
};
}

a( 'admin_menu' );

//dirty hack until https://core.trac.wordpress.org/ticket/22895 is solved
add_action( 'admin_head', function() {
?>
// dirty hack until https://core.trac.wordpress.org/ticket/22895 is solved
add_action(
'admin_head',
function() {
?>
<style type="text/css">
a[href="removeme"]{
display: none !important;
}
</style>
<?php
} );

<?php
}
);
}

/**
Expand All @@ -39,7 +47,7 @@ function a( $function ) {
*/
function admin_menu() {

$top_level = _x( 'Print', 'Top level menu label', 'eight-day-week-print-workflow' );
$top_level = _x( 'Print', 'Top level menu label', 'eight-day-week-print-workflow' );
$pi_submenu = _x( 'Print Issues', 'Submenu label for the Print Issue CPT', 'eight-day-week-print-workflow' );

// Top level "container", but still link to Print Issue CPT list table
Expand All @@ -48,9 +56,8 @@ function admin_menu() {
// Duplicate CPT list table link, with more specific text as requested.
add_submenu_page( EDW_ADMIN_MENU_SLUG, $pi_submenu, $pi_submenu, 'read_' . EDW_PRINT_ISSUE_CPT, EDW_ADMIN_MENU_SLUG );

//dirty hack until https://core.trac.wordpress.org/ticket/22895 is solved
// dirty hack until https://core.trac.wordpress.org/ticket/22895 is solved
add_submenu_page( EDW_ADMIN_MENU_SLUG, 'Dummy Submenu', 'Dummy Submenu', 'read', 'removeme' );

do_action( __NAMESPACE__ . '\admin_menu' );

}
5 changes: 5 additions & 0 deletions includes/functions/articles.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php
/**
* Handles the articles functionality
*
* @package eight-day-week
*/

namespace Eight_Day_Week\Articles;

Expand Down
54 changes: 31 additions & 23 deletions includes/functions/core.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?php
/**
* Core functionality
*
* @package eight-day-week
*/

namespace Eight_Day_Week\Core;

/**
Expand All @@ -17,12 +23,12 @@ function ns( $function ) {
add_action( 'init', ns( 'init' ) );
add_action( __NAMESPACE__ . '\init', ns( 'i18n' ) );

//activate current version of this plugin
// activate current version of this plugin
add_action( 'admin_init', ns( 'activate' ) );

do_action( __NAMESPACE__ . '\loaded' );

//add data to wp_send_json_*
// add data to wp_send_json_*
add_filter( 'pp-ajax-data', ns( 'tack_on_ajax_response' ) );
}

Expand All @@ -36,10 +42,10 @@ function ns( $function ) {
function init() {
do_action( __NAMESPACE__ . '\init' );
if ( is_admin() ) {
do_action ( __NAMESPACE__ . '\admin_init' );
do_action( __NAMESPACE__ . '\admin_init' );
}

//this should be used by plugins, so that they're guaranteed that all core functionality has been loaded
// this should be used by plugins, so that they're guaranteed that all core functionality has been loaded
do_action( __NAMESPACE__ . '\plugin_init' );
}

Expand All @@ -62,9 +68,9 @@ function i18n() {
*/
function activate() {

$stored_version = get_option( 'edw_activated');
$stored_version = get_option( 'edw_activated' );

if( EDW_VERSION === $stored_version ) {
if ( EDW_VERSION === $stored_version ) {
return;
}

Expand Down Expand Up @@ -93,15 +99,16 @@ function create_nonce() {
*
* @param array $data Data to send in the response
*/
function send_json_success( $data = [] ) {
function send_json_success( $data = array() ) {
wp_send_json_success( apply_filters( 'pp-ajax-data', $data ) );
}

/**
* Send a json error response + tacked on data
*
* @param array $data Data to send in the response
*/
function send_json_error( $data = [] ) {
function send_json_error( $data = array() ) {
wp_send_json_error( apply_filters( 'pp-ajax-data', $data ) );
}

Expand All @@ -113,13 +120,13 @@ function send_json_error( $data = [] ) {
*
* @return array Data + nonce
*/
function tack_on_ajax_response( $data = [] ) {
$data = ( is_array( $data ) || is_object( $data ) ? $data : [ 'message' => $data ] );
function tack_on_ajax_response( $data = array() ) {
$data = ( is_array( $data ) || is_object( $data ) ? $data : array( 'message' => $data ) );

$nonce = create_nonce();
if( is_array( $data ) ) {
if ( is_array( $data ) ) {
$data['_ajax_nonce'] = $nonce;
if( isset( $data['message'] ) ) {
if ( isset( $data['message'] ) ) {
$data['message'] = esc_html( $data['message'] );
}
} else {
Expand All @@ -146,7 +153,7 @@ function check_ajax_referer( $query_arg = false, $die = false ) {
function check_elevated_ajax_referer( $action = false, $query_arg = false, $die = false ) {
check_ajax_referer( $query_arg, $die );

//if the user didn't pass in an $action, check $_POST. One can pass an empty string to use no cap/action
// if the user didn't pass in an $action, check $_POST. One can pass an empty string to use no cap/action
$action = false === $action ? $_POST['action'] : sanitize_text_field( $action );

$elevated = \Eight_Day_Week\User_Roles\current_user_can_edit_print_issue( $action );
Expand All @@ -164,16 +171,16 @@ function check_elevated_ajax_referer( $action = false, $query_arg = false, $die
*
* @return string The final URL
*/
function get_asset_url ( $file_name, $js_or_css ) {
function get_asset_url( $file_name, $js_or_css ) {
$url = EDW_URL . 'assets/';

if ( $js_or_css === 'js' ) {
$url .= 'js/';
$url .= EDW_PRODUCTION ? '' : 'src/';
$ext = '.js';
$ext = '.js';
} else {
$url .= 'css/';
$ext = '.css';
$ext = '.css';
}

$url .= $file_name;
Expand All @@ -194,7 +201,7 @@ function get_asset_url ( $file_name, $js_or_css ) {
*/
function get_timezone() {
$timezone = get_option( 'timezone_string' );
if( ! $timezone ) {
if ( ! $timezone ) {
$timezone = new Helper_DateTimeZone( Helper_DateTimeZone::tzOffsetToName( get_offset() ) );
$timezone = $timezone->getName();
}
Expand All @@ -212,21 +219,22 @@ function get_offset() {

/**
* Class Helper_DateTimeZone
*
* @package Eight_Day_Week\Core
*
* http://php.net/manual/en/function.timezone-name-from-abbr.php#89155
*
*/
class Helper_DateTimeZone extends \DateTimeZone {
/**
* Converts a timezone hourly offset to its timezone's name.
*
* @example $offset = -5, $isDst = 0 <=> return value = 'America/New_York'
*
* @param float $offset The timezone's offset in hours.
* Lowest value: -12 (Pacific/Kwajalein)
* Highest value: 14 (Pacific/Kiritimati)
* @param bool $isDst Is the offset for the timezone when it's in daylight
* savings time?
* @param bool $isDst Is the offset for the timezone when it's in daylight
* savings time?
*
* @return string The name of the timezone: 'Asia/Tokyo', 'Europe/Paris', ...
*/
Expand All @@ -236,14 +244,14 @@ final public static function tzOffsetToName( $offset, $isDst = null ) {
}

$offset *= 3600;
$zone = timezone_name_from_abbr( '', $offset, $isDst );
$zone = timezone_name_from_abbr( '', $offset, $isDst );

if ( $zone === false ) {
foreach ( timezone_abbreviations_list() as $abbr ) {
foreach ( $abbr as $city ) {
if ( (bool) $city['dst'] === (bool) $isDst &&
strlen( $city['timezone_id'] ) > 0 &&
$city['offset'] == $offset
strlen( $city['timezone_id'] ) > 0 &&
$city['offset'] == $offset
) {
$zone = $city['timezone_id'];
break;
Expand Down
Loading

0 comments on commit 275fdd9

Please sign in to comment.