From 9b3f88103c465fbf23d4cbe2ede1f8b79aa0e276 Mon Sep 17 00:00:00 2001 From: Ben Marshall Date: Mon, 12 Jun 2023 10:18:34 -0500 Subject: [PATCH 1/7] Integrated code from xlesy for adding images to import, see #22 --- includes/functions/articles.php | 158 ++--- includes/functions/plugins/article-export.php | 600 +++++++++++------- includes/functions/plugins/article-status.php | 131 ++-- 3 files changed, 544 insertions(+), 345 deletions(-) diff --git a/includes/functions/articles.php b/includes/functions/articles.php index 37f117d..73e4afa 100644 --- a/includes/functions/articles.php +++ b/includes/functions/articles.php @@ -28,11 +28,9 @@ function a( $function ) { add_action( 'wp_ajax_pp-get-article-row', ns( 'get_article_row_ajax' ) ); add_action( 'save_print_issue', ns( 'save_section_articles' ), 10, 3 ); - //load publish date - //use -1 priority to ensure its loaded before 3rd party plugins + // load publish date + // use -1 priority to ensure its loaded before 3rd party plugins add_filter( __NAMESPACE__ . '\article_columns', ns( 'filter_article_columns_date' ), 1 ); - - } /** @@ -47,9 +45,11 @@ function articles_metabox_output( $section_id ) { - + } + ?> - $export_status] ); + Core\send_json_success( array( 'export_status' => $export_status ) ); } catch ( \Exception $e ) { Core\send_json_error(); } @@ -153,7 +153,7 @@ function article_status_handler() { $article_ids = $_POST['article_ids']; - //sanitize - only allow comma delimited integers + // sanitize - only allow comma delimited integers if ( ! ctype_digit( str_replace( ',', '', $article_ids ) ) ) { throw new \Exception( __( 'Invalid article IDs specified in the request.', 'eight-day-week-print-workflow' ) ); } @@ -206,8 +206,7 @@ function get_export_status( $article_id ) { } $user_id = get_post_meta( $article_id, 'export_status_user_id', true ); - $user = get_user_by( 'id', absint( $user_id ) ); - + $user = get_user_by( 'id', absint( $user_id ) ); $zone = new \DateTimeZone( Core\get_timezone() ); @@ -216,12 +215,12 @@ function get_export_status( $article_id ) { $now = new \DateTime( 'now', $zone ); - $not_today = $export_datetime->diff( $now )->format( "%R%a" ); + $not_today = $export_datetime->diff( $now )->format( '%R%a' ); - if( ! $not_today ) { + if ( ! $not_today ) { $export_status = sprintf( __( 'Exported on %1$s by %2$s', 'eight-day-week-print-workflow' ), $export_datetime->format( get_option( 'date_format' ) ), $user->display_name ); } else { - $export_status = sprintf( __( 'Exported at %1$s by %2$s', 'eight-day-week-print-workflow' ), $export_datetime->format( _x( 'g:ia', 'Format for article export timestamp', 'eight-day-week-print-workflow' ) ), $user->display_name ); + $export_status = sprintf( __( 'Exported at %1$s by %2$s', 'eight-day-week-print-workflow' ), $export_datetime->format( _x( 'g:ia', 'Format for article export timestamp', 'eight-day-week' ) ), $user->display_name ); } return $export_status; @@ -229,13 +228,13 @@ function get_export_status( $article_id ) { /** * Class Article_Zip_Factory + * * @package Eight_Day_Week\Plugins\Article_Export * * Factory for building an export zip for an article * * @todo Pull out XML-related functions so this class is for a generic ZIP; * @todo Perhaps introduce a fallback filter vs explicitly requesting an XML file fallback - * */ class Article_Zip_Factory { @@ -254,11 +253,16 @@ class Article_Zip_Factory { */ var $files; + /** + * @var array Images for all articles + */ + var $images; + /** * Sets up object properties * - * @param array $ids Array of post IDs - * @param int $print_issue_id ID of parent print issue + * @param array $ids Array of post IDs + * @param int $print_issue_id ID of parent print issue * @param string $print_issue_title Title of parent print issue (to avoid lookup) * * @throws \Exception Various points of failure in constructing the factory @@ -270,10 +274,9 @@ function __construct( $ids, $print_issue_id, $print_issue_title ) { throw new \Exception( __( 'Invalid article IDs specified in the request.', 'eight-day-week-print-workflow' ) ); } - $this->ids = $ids; - $this->print_issue_id = absint( $print_issue_id ); + $this->ids = $ids; + $this->print_issue_id = absint( $print_issue_id ); $this->print_issue_title = sanitize_text_field( $print_issue_title ); - } /** @@ -282,7 +285,7 @@ function __construct( $ids, $print_issue_id, $print_issue_title ) { * @return \WP_Post[] */ function import_articles() { - $articles = [ ]; + $articles = array(); foreach ( $this->ids as $id ) { $article = get_post( $id ); if ( $article ) { @@ -320,13 +323,13 @@ function build_file_sets() { $articles = $this->get_articles(); - $file_sets = [ ]; + $file_sets = array(); foreach ( $articles as $article ) { - //allow articles to export an alternative file + // allow articles to export an alternative file $files = apply_filters( __NAMESPACE__ . '\short_circuit_article_export_files', false, $article, $this->print_issue_id, $this->print_issue_title ); - //but fall back to XML + // but fall back to XML if ( ! $files ) { $files = $this->get_xml_file( $article ); } @@ -336,7 +339,6 @@ function build_file_sets() { } return $file_sets; - } /** @@ -349,12 +351,15 @@ function build_file_sets() { function get_xml_file( $article ) { $xml = $this->get_xml( $article ); - $file_name = apply_filters( __NAMESPACE__ . '\xml_filename', $xml->root_element->getAttribute( 'title' ), $article, $xml ); + $file_name = apply_filters( __NAMESPACE__ . '\xml_filename', $xml->root_element->getAttribute( 'title' ), $article, $xml ); $file_name .= '.xml'; $file_contents = $xml->xml_document->saveXML(); - return new File( $file_contents, apply_filters( __NAMESPACE__ . '\xml_full_filename', $file_name, $article ) ); + $fileset = array(); + $fileset[] = new File( $file_contents, apply_filters( __NAMESPACE__ . '\xml_full_filename', $file_name, $article ) ); + + return $fileset; } /** @@ -366,8 +371,10 @@ function get_xml_file( $article ) { * @throws \Exception */ function get_xml( $article ) { - $xml = new Article_XML( $article ); - return $xml->build_xml(); + $xml = new Article_XML( $article ); + $xmlContent = $xml->build_xml(); + $this->images[ $article->ID ] = $xml->images; + return $xmlContent; } /** @@ -384,27 +391,110 @@ function get_file_sets() { } function output_zip() { + // prepare folder + $tmp_folder = 'export_xml_' . get_current_user_id(); + + if ( ! is_dir( get_temp_dir() . $tmp_folder ) ) { + mkdir( get_temp_dir() . $tmp_folder ); + } + + // create zip + $tmp_zip_file = get_temp_dir() . $tmp_folder . '/output.zip'; + if ( file_exists( $tmp_zip_file ) ) { + unlink( $tmp_zip_file ); + } + $zip = new \ZipArchive( $this->print_issue_title ); + $code = $zip->open( $tmp_zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE ); + $file_sets = $this->get_file_sets(); - $zip = new Articles_Zip( $this->print_issue_title ); + $sub_folders = array(); + foreach ( $file_sets as $article_id => $files ) { + foreach ( $files as $file ) { + if ( stripos( $file->filename, '.xml' ) !== false ) { + $filename = explode( '.', $file->filename ); + $sub_folders[ $article_id ] = $filename[0]; + break; + } + } + + if ( ! $sub_folders[ $article_id ] ) { + $sub_folders[ $article_id ] = $article_id; + } + } + + // add xml files foreach ( $file_sets as $article_id => $files ) { - //force an array - $files = is_array( $files ) ? $files : [ $files ]; - $zip->addFiles( $files ); + $zip->addEmptyDir( $sub_folders[ $article_id ] ); + + // force an array + $files = is_array( $files ) ? $files : array( $files ); + foreach ( $files as $file ) { + $zip->addFromString( $sub_folders[ $article_id ] . '/' . $file->filename, $file->contents ); + } + } + + // add images (by path) + if ( is_array( $this->images ) ) { + foreach ( $this->images as $article_id => $images ) { + foreach ( $images as $image_full_path => $image_name ) { + $zip->addFile( $image_full_path, $sub_folders[ $article_id ] . '/' . remove_accents( $image_name ) ); + } + } } - $zip->output_zip(); + $zip->close(); + + $this->out_zip_file( $tmp_zip_file ); + } + + /** + * Builds the file name for the zip + * Uses the print issue title & day/time + * + * @uses get_timezone + * + * @return string The zip file name + */ + function out_zip_file( $filename ) { + header( 'Content-type: application/octet-stream' ); + header( 'Content-Disposition: attachment; filename="' . $this->get_zip_file_name() . '.zip"' ); + $handle = fopen( $filename, 'rb' ); + if ( $handle ) { + while ( ! feof( $handle ) ) { + echo fread( $handle, 4096 ); + ob_flush(); + flush(); + } + } + + fclose( $handle ); + exit; + } + + + /** + * Builds the file name for the zip + * Uses the print issue title & day/time + * + * @uses get_timezone + * + * @return string The zip file name + */ + function get_zip_file_name() { + date_default_timezone_set( Core\get_timezone() ); + return sprintf( __( 'Issue %1$s exported on %2$s at %3$s', 'eight-day-week' ), $this->print_issue_title, date( 'm-d-y' ), date( 'h:ia' ) ); } } /** * Class Article_XML + * * @package Eight_Day_Week\Plugins\Article_Export * * Builds an XML DOMDocument based on a WP_Post - * */ class Article_XML { @@ -433,24 +523,20 @@ function build_xml() { global $post; - //store global post backup + // store global post backup $old = $post; - //and set global post to this post + // and set global post to this post $post = $this->article; - $elements = apply_filters( __NAMESPACE__ . '\xml_outer_elements', [ - 'headline' => get_the_title( $this->article ) - ], $this->article ); - - $dom = new \DOMDocument; - - $content = strip_shortcodes( $this->article->post_content ); - + $content = $this->get_article_content( $this->article ); if ( ! $content ) { throw new \Exception( 'Post ' . $this->id . ' was empty.' ); } + $content = str_replace( array( "\r\n", "\r" ), "\n", $content ); + + $dom = new \DOMDocument(); @$dom->loadHTML( mb_convert_encoding( $content, @@ -459,30 +545,170 @@ function build_xml() { ) ); - //perform dom manipulations + // perform dom manipulations $dom = $this->manipulate_dom( $dom ); - //do html_to_xml before adding elements so that the html wrap stuff is removed first + // do html_to_xml before adding elements so that the html wrap stuff is removed first $xml_elements = $this->html_to_xml( $dom ); + $elements = apply_filters( + __NAMESPACE__ . '\xml_outer_elements', + $this->get_outer_elements( $this->article ), + $this->article + ); + if ( $elements ) { $this->add_outer_elements( $xml_elements, $elements ); } $this->add_article_attributes( $xml_elements->root_element, $elements ); - //reset global post + // reset global post $post = $old; + $GLOBALS['recentComment'] = $elements['comment'] ? $elements['comment'] : false; + return $xml_elements; + } + + /** + * Get prepared article content + * + * @param object $article WP_Post article + * + * @return string article content + */ + function get_article_content( $article ) { + + $content = $this->article->post_content; + + if ( $post = get_post( get_post_thumbnail_id( $this->id ) ) ) { + if ( $image = $this->get_image_name( $post->ID ) ) { + $content = $this->get_image_tag( $image[1], $post->post_excerpt ) . $content; + } + } + + $content = preg_replace_callback( + '/\[caption.*\[\/caption\]/Usi', + function ( $matches ) { + if ( preg_match( '/attachment_(\d+)\D/i', $matches[0], $matches2 ) ) { + $image = $this->get_image_name( (int) $matches2[1] ); + } + if ( preg_match( '/\[caption[^\]]*](.*)\[\/caption\]/Usi', strip_tags( $matches[0] ), $matches2 ) ) { + $caption = trim( $matches2[1] ); + } + return $image ? $this->get_image_tag( $image[1], $caption ) : ''; + }, + $content + ); + + $content = preg_replace_callback( + '/\[gallery[^]]*ids="([\d,]+)"[^]]*\]/Usi', + function ( $matches ) { + + if ( $matches[1] ) { + $gallery_images = explode( ',', $matches[1] ); + foreach ( $gallery_images as $img_id ) { + $image = $this->get_image_name( (int) $img_id ); + wp_mail( 'aaaa@asefetbhtregfsefwef.com', 'TEST', print_r( $image, 1 ) ); + } + } + // return $image ? $this->get_image_tag($image[1], $caption) : ''; + return ''; + }, + $content + ); + + $content = strip_shortcodes( $content ); + + $content = preg_replace_callback( + '/((]*>)([^<]*<\/img>|))/Usi', + function ( $matches ) { + $image = false; + if ( preg_match( '/wp\-image\-(\d+)\D/i', $matches[0], $matches2 ) ) { + $image = $this->get_image_name( (int) $matches2[1] ); + // } else if( preg_match('/src="([^"]*)"|src=\'([^\']*)\'/Usi', $matches[0], $matches2 ) ) { + // $components = parse_url($matches2[1]); + // $full_url = empty($components['host']) ? $_SERVER['HTTP_HOST'] . '/' . $matches2[1] : $matches2[1]; + // $image = $this->get_image_name(false, $full_url); + } + return $image ? $this->get_image_tag( $image[1] ) : ''; + }, + $content + ); + + return $content; + } + + /** + * Get array with various elements + * + * @param object $article WP_Post article + * + * @return array with elements + */ + function get_outer_elements( $article ) { + + $res = array( 'headline' => get_the_title( $article ) ); + + if ( $featured_id = get_post_thumbnail_id( $this->id ) ) { + if ( $image = $this->get_image_name( $featured_id ) ) { + $res['featured'] = $image; + } + } + + if ( $this->images ) { + $res['image'] = $this->images; + } + if ( function_exists( 'get_field' ) && $this->id && $comment = get_field( 'opombe_za_dtp', $this->id ) ) { + $res['comment'] = $comment; + } + return $res; + } + + /** + * Retrieves path and full filename for atatchment id + * + * @param int $attachment_id Attachment id to process + * + * @return Array with path and name + */ + function get_image_name( $attachment_id = false, $attachment_path = false ) { + $imageSrc = $attachment_path ? $attachment_path : get_attached_file( $attachment_id ); + + if ( preg_match( '/^(.*[\\/])([^\\/]+)\.(.*)$/', $imageSrc, $matches ) ) { + // [-_]\d+x\d+ + $imagePath = $matches[1]; + $imageFilename = ( ! $attachment_id && preg_match( '/^(.+)[-_]\d+x\d+$/i', $matches[2], $matches2 ) ? $matches2[1] : $matches[2] ) . '.' . $matches[3]; + } + + if ( ! $imageFilename ) { + return array(); + } + + $this->images[ $imagePath . $imageFilename ] = $imageFilename; + + return array( $imagePath, $imageFilename ); + } + + /** + * Formats tag for atatchment name and caption + * + * @param string $attachment_name Attachment name + * @param string $attachment_caption Attachment caption (optional) + * + * @return String with formatted image tag + */ + function get_image_tag( $attachment_name, $attachment_caption = false ) { + return ' ## ' . remove_accents( $attachment_name ) . ' ## ' . ( $attachment_caption ? trim( $attachment_caption ) : '' ) . ' '; } /** * Appends various elements * * @param object $xml_elements XML Document + children - * @param array $outer_elements Elements to add to the root element + * @param array $outer_elements Elements to add to the root element * * @return \DOMDocument XML Doc with elements appended to the root */ @@ -491,11 +717,43 @@ function add_outer_elements( $xml_elements, $outer_elements ) { if ( ! $value ) { continue; } - $element = $xml_elements->xml_document->createElement( $tag_name ); - $element->nodeValue = $value; - $xml_elements->root_element->appendChild( $element ); - } + if ( $tag_name == 'headline' ) { + + $element = $xml_elements->xml_document->createElement( $tag_name ); + $element->nodeValue = $value; + $afterSibling = $xml_elements->root_element->firstChild; + $xml_elements->root_element->insertBefore( $element, $afterSibling ); + + } elseif ( $tag_name == 'featured' || $tag_name == 'image' ) { + + if ( $tag_name == 'featured' ) { + $element = $xml_elements->xml_document->createElement( 'image' ); + $value = array_values( $value ); + $element->setAttribute( 'href', 'file:///' . html_entity_decode( remove_accents( array_pop( $value ) ) ) ); + $element->nodeValue = ''; + $afterSibling = $xml_elements->xml_document->getElementsByTagName( 'content' ); + $xml_elements->root_element->insertBefore( $element, $afterSibling[0] ); + $xml_elements->root_element->insertBefore( $xml_elements->xml_document->createTextNode( "\n" ), $afterSibling[0] ); + } else { + foreach ( $value as $image_full_path => $image_name ) { + if ( $outer_elements['featured'] && $outer_elements['featured'][1] == $image_name ) { + continue; + } + $element = $xml_elements->xml_document->createElement( 'image' ); + $element->setAttribute( 'href', 'file:///' . html_entity_decode( remove_accents( $image_name ) ) ); + $element->nodeValue = ''; + $xml_elements->root_element->appendChild( $element ); + } + } + } else { + + $element = $xml_elements->xml_document->createElement( $tag_name ); + $element->nodeValue = $value; + $xml_elements->root_element->appendChild( $element ); + + } + } } /** @@ -507,7 +765,7 @@ function get_first_author_name() { if ( function_exists( 'get_coauthors' ) ) { $authors = get_coauthors( $this->id ); } else { - $authors = [ get_userdata( $this->id ) ]; + $authors = array( get_userdata( $this->id ) ); } if ( ! $authors ) { @@ -525,7 +783,6 @@ function get_first_author_name() { } return $author->display_name; - } /** @@ -541,22 +798,32 @@ function add_author_name( $article_element ) { * Adds the post title to the article element * * @param \DOMElement $article_element The root article element - * @param string $title The post title to add + * @param string $title The post title to add */ function add_post_title( $article_element, $title ) { $article_element->setAttribute( 'title', html_entity_decode( $title ) ); } + /** + * Adds the post comment to the article element + * + * @param \DOMElement $article_element The root article element + * @param string $title The post comment to add + */ + function add_post_comment( $article_element, $comment ) { + $article_element->setAttribute( 'comment', html_entity_decode( $comment ) ); + } + /** * Adds various attributes to the article element * * @param \DOMElement $article_element - * @param array $elements Predefined attribute values + * @param array $elements Predefined attribute values */ function add_article_attributes( $article_element, $elements ) { $this->add_author_name( $article_element ); - if ( isset( $elements['headline' ] ) ) { + if ( isset( $elements['headline'] ) ) { $this->add_post_title( $article_element, $elements['headline'] ); } } @@ -572,7 +839,7 @@ function manipulate_dom( $dom ) { $this->extract_elements_by_xpath( $dom ); $this->remove_elements( $dom ); - //allow third party modification of the entire dom + // allow third party modification of the entire dom $dom = apply_filters( __NAMESPACE__ . '\dom', $dom ); return $dom; @@ -585,9 +852,9 @@ function manipulate_dom( $dom ) { * @param $dom \DOMDocument */ function remove_elements( $dom ) { - $elements_to_remove = apply_filters( __NAMESPACE__ . '\remove_elements', [ 'img' ] ); + $elements_to_remove = apply_filters( __NAMESPACE__ . '\remove_elements', array( 'img' ) ); - $remove = [ ]; + $remove = array(); foreach ( $elements_to_remove as $tag_name ) { $found = $dom->getElementsByTagName( $tag_name ); foreach ( $found as $el ) { @@ -625,12 +892,12 @@ function remove_elements( $dom ) { * Doesn't need to return anything because the DOM is aliiiiiive */ function extract_elements_by_xpath( $dom ) { - $xpath_extract = apply_filters( __NAMESPACE__ . '\xpath_extract', [] ); + $xpath_extract = apply_filters( __NAMESPACE__ . '\xpath_extract', array() ); if ( $xpath_extract ) { - $domxpath = new \DOMXPath( $dom ); + $domxpath = new \DOMXPath( $dom ); foreach ( $xpath_extract as $set ) { - $remove = [ ]; + $remove = array(); $elements = $domxpath->query( $set['query'] ); if ( $elements->length ) { $wrap = $dom->createElement( $set['container'] ); @@ -662,12 +929,12 @@ function extract_elements_by_xpath( $dom ) { function html_to_xml( $dom ) { $content = $dom->getElementsByTagName( 'body' ); if ( ! $content ) { - throw new \Exception ( 'Empty content' ); + throw new \Exception( 'Empty content' ); } $content = $content->item( 0 ); - $xml_document = new \DOMDocument; + $xml_document = new \DOMDocument(); $article_element = $xml_document->createElement( apply_filters( __NAMESPACE__ . '\xml_root_element', 'article' ) ); $xml_document->appendChild( $article_element ); @@ -679,145 +946,22 @@ function html_to_xml( $dom ) { $content_element->appendChild( $xml_document->importNode( $el, true ) ); } - $article_xml = new \stdClass; - $article_xml->xml_document = $xml_document; - $article_xml->root_element = $article_element; + $article_xml = new \stdClass(); + $article_xml->xml_document = $xml_document; + $article_xml->root_element = $article_element; $article_xml->content_element = $content_element; return $article_xml; - - } - -} - -/** - * Class Articles_Zip - * @package Eight_Day_Week\Plugins\Article_Export - * - * Represents an in-memory Zip, including implementation of the vendor zip lib - * - */ -class Articles_Zip { - - /** - * @var string The print issue title (used for building zip file name) - */ - protected $issue_title; - - /** - * @var File[] Files for the current articles - */ - protected $files; - - /** - * @var \ZipFile - */ - private $zip; - - /** - * Sets object properties - * - * @param $issue_title string The current print issue's title - * @param $files File[] Set of Files - */ - function __construct( $issue_title, $files = [] ) { - $this->issue_title = $issue_title; - $this->files = $files; - } - - /** - * Builds the file name for the zip - * Uses the print issue title & day/time - * - * @uses get_timezone - * - * @return string The zip file name - */ - function get_zip_file_name( ) { - $timezone_string = get_option( 'timezone_string' ); - if ( empty( $timezone_string ) ) { - $offset = get_option( 'gmt_offset' ); - $hours = (int) $offset; - $minutes = abs( ( $offset - (int) $offset ) * 60 ); - $timezone_string = sprintf( '%+03d:%02d', $hours, $minutes ); - } - $date = new \DateTime( 'now', new \DateTimeZone( $timezone_string ) ); - return 'Issue ' . $this->issue_title . ' exported on ' . $date->format( 'm-d-y' ) . ' at ' . $date->format( 'h:ia' ); - } - - /** - * Adds a File to the object, for later addition to the zip - * - * @param $file File The file to add to the zip - */ - function addFile( $file ) { - $this->files[] = $file; - } - - /** - * Adds a set of files to the current set - * - * @param $files File[] FIles to add to the zip - */ - function addFiles( $files ) { - $this->files = array_merge( $this->files, $files ); - } - - /** - * Creates a new \ZipFile, adds current file set to it, and yields it - * - * @return \ZipFile - */ - function build_zip(){ - include_once EDW_INC . 'lib/zip.lib.php'; - $zip = new \ZipFile(); - - foreach ( $this->files as $file ) { - $zip->addFile( $file->contents, $file->filename ); - } - - return $zip; - } - - /** - * Gets/builds a \ZipFile via the current file set - * - * @uses build_zip - * - * @return \ZipFile - */ - function get_zip() { - if ( $this->zip ) { - return $this->zip; - } - - return $this->zip = $this->build_zip(); - } - - /** - * Builds a zip and outputs it to the browser - * - * @uses get_zip - * - */ - function output_zip() { - - $zip = $this->get_zip(); - - header( "Content-type: application/octet-stream" ); - header( 'Content-Disposition: attachment; filename="' . $this->get_zip_file_name() . '.zip"' ); - echo $zip->file(); - exit; } } /** * Class File + * * @package Eight_Day_Week\Plugins\Article_Export * * Builds a "File" based on either a string or a readable, actual file - * */ class File { @@ -838,10 +982,10 @@ class File { * Otherwise assumes provision of explicit file contents + name * * @param $contents_or_file_path - * @param string $filename + * @param string $filename */ function __construct( $contents_or_file_path, $filename = '' ) { - if( is_readable( $contents_or_file_path ) ) { + if ( is_readable( $contents_or_file_path ) ) { $this->contents = file_get_contents( $contents_or_file_path ); $this->filename = basename( $contents_or_file_path ); } else { @@ -865,14 +1009,16 @@ function maybe_export_article_attachments( $incoming, $article, $issue_id, $issu add_filter( 'posts_where', __NAMESPACE__ . '\filter_article_export_attachments_where' ); - $attachments = new \WP_Query( [ - 'post_type' => 'attachment', - 'post_parent' => absint( $article->ID ), - 'posts_per_page' => 20, - 'post_status' => 'inherit', - //meta will be used, so only disable term cache - 'update_post_term_cache' => false, - ] ); + $attachments = new \WP_Query( + array( + 'post_type' => 'attachment', + 'post_parent' => absint( $article->ID ), + 'posts_per_page' => 20, + 'post_status' => 'inherit', + // meta will be used, so only disable term cache + 'update_post_term_cache' => false, + ) + ); remove_filter( 'posts_where', __NAMESPACE__ . '\filter_article_export_attachments_where' ); @@ -881,7 +1027,6 @@ function maybe_export_article_attachments( $incoming, $article, $issue_id, $issu } return $incoming; - } function filter_article_export_attachments_where( $where ) { @@ -896,9 +1041,12 @@ function filter_article_export_attachments_where( $where ) { * @return File[] Attachment files */ function get_document_files( $attachments ) { - return array_map( function( $attachment ){ - return get_attachment_file( $attachment ); - }, $attachments ); + return array_map( + function( $attachment ) { + return get_attachment_file( $attachment ); + }, + $attachments + ); } /** @@ -921,7 +1069,7 @@ function get_attachment_file( $attachment ) { * @return array Modified elements */ function filter_xml_elements_headline( $elements, $article ) { - $elements['headline'] = get_the_title( $article ); + $elements['headline'] = get_the_title( $article ); return $elements; } @@ -936,7 +1084,7 @@ function filter_xml_elements_headline( $elements, $article ) { */ function filter_xml_filename_author( $file_name, $article, $xml ) { $author = $xml->root_element->getAttribute( 'author' ); - if( $author ) { + if ( $author ) { $file_name = $author . '.' . $file_name; } diff --git a/includes/functions/plugins/article-status.php b/includes/functions/plugins/article-status.php index b6772b8..e3ef2ac 100644 --- a/includes/functions/plugins/article-status.php +++ b/includes/functions/plugins/article-status.php @@ -6,34 +6,35 @@ use Eight_Day_Week\Taxonomies as Tax; use Eight_Day_Week\User_Roles as User; -function setup (){ +function setup() { - add_action( 'Eight_Day_Week\Core\plugin_init', function () { + add_action( + 'Eight_Day_Week\Core\plugin_init', + function () { - function ns( $function ) { - return __NAMESPACE__ . "\\$function"; - } - - register_taxonomy(); - - add_filter( 'Eight_Day_Week\Articles\article_columns', ns( 'filter_article_columns_article_status' ), 0 ); - add_filter( 'Eight_Day_Week\Articles\article_meta_article_status', ns( 'filter_article_meta_article_status' ), 10, 2 ); - - add_action( 'Eight_Day_Week\Admin_Menu_Page\admin_menu', ns( 'admin_menu' ) ); + function ns( $function ) { + return __NAMESPACE__ . "\\$function"; + } - add_action( 'edw_sections_top', ns( 'output_bulk_article_status_editor' ) ); - add_action( 'wp_ajax_pp-bulk-edit-article-statuses', ns( 'bulk_edit_article_statuses_ajax' ) ); + register_taxonomy(); + add_filter( 'Eight_Day_Week\Articles\article_columns', ns( 'filter_article_columns_article_status' ), 0 ); + add_filter( 'Eight_Day_Week\Articles\article_columns', ns( 'filter_article_columns_article_images' ), 0 ); + add_filter( 'Eight_Day_Week\Articles\article_meta_article_status', ns( 'filter_article_meta_article_status' ), 10, 2 ); - } ); + add_action( 'Eight_Day_Week\Admin_Menu_Page\admin_menu', ns( 'admin_menu' ) ); + add_action( 'edw_sections_top', ns( 'output_bulk_article_status_editor' ) ); + add_action( 'wp_ajax_pp-bulk-edit-article-statuses', ns( 'bulk_edit_article_statuses_ajax' ) ); + } + ); } /** * Register the Article Status taxonomy */ function register_taxonomy() { - $labels = [ + $labels = array( 'name' => __( 'Article Status', 'eight-day-week-print-workflow' ), 'singular_name' => __( 'Article Statuses', 'eight-day-week-print-workflow' ), 'all_items' => __( 'Article Statuses', 'eight-day-week-print-workflow' ), @@ -44,44 +45,44 @@ function register_taxonomy() { 'new_item_name' => __( 'New Article Status', 'eight-day-week-print-workflow' ), 'search_items' => __( 'Search Article Status', 'eight-day-week-print-workflow' ), 'not_found' => __( 'No Article Statuses found', 'eight-day-week-print-workflow' ), - ]; + ); - $args = [ + $args = array( 'labels' => $labels, - //only for the backend + // only for the backend 'public' => false, - //don't show under posts menu - //as of WP 4.4, setting show_ui to false yields "You are not allowed to manage these items." + // don't show under posts menu + // as of WP 4.4, setting show_ui to false yields "You are not allowed to manage these items." 'show_ui' => true, 'show_in_menu' => false, 'show_in_nav_menus' => false, 'meta_box_cb' => false, - //don't show on posts + // don't show on posts 'show_in_quick_edit' => false, - //don't show on posts + // don't show on posts 'meta_box_cb' => false, - //don't show on posts + // don't show on posts 'show_admin_column' => false, - //don't allow front end querying + // don't allow front end querying 'query_var' => false, - //don't allow front end rewriting + // don't allow front end rewriting 'rewrite' => false, - 'capabilities' => [ + 'capabilities' => array( 'manage_terms' => 'manage_' . EDW_PRINT_ISSUE_CPT, 'edit_terms' => 'manage_' . EDW_PRINT_ISSUE_CPT, 'delete_terms' => 'manage_' . EDW_PRINT_ISSUE_CPT, - 'assign_terms' => 'create_' . EDW_PRINT_ISSUE_CPT - ] - ]; + 'assign_terms' => 'create_' . EDW_PRINT_ISSUE_CPT, + ), + ); \register_taxonomy( EDW_ARTICLE_STATUS_TAX, 'post', $args ); } function admin_menu() { - //Note that the & is important. - //It is required to stay in line with how edit-tags.php - //builds the global $submenu_file, which is checked against - //to determine whether or not the active submenu gets a "current" CSS class. + // Note that the & is important. + // It is required to stay in line with how edit-tags.php + // builds the global $submenu_file, which is checked against + // to determine whether or not the active submenu gets a "current" CSS class. add_submenu_page( EDW_ADMIN_MENU_SLUG, __( 'Article Statuses', 'eight-day-week-print-workflow' ), @@ -99,7 +100,42 @@ function admin_menu() { * @return array Modified columns */ function filter_article_columns_article_status( $columns ) { - $columns['article_status'] = __( 'Article Status', 'eight-day-week-print-workflow' ); + $status = array( + 'post_status' => __( 'Article Status', 'eight-day-week' ), + ); + + $title_offset = array_search( 'title', array_keys( $columns ) ); + if ( $title_offset ) { + $end = $status + array_slice( $columns, $title_offset + 1, null ); + $columns = array_slice( $columns, 0, $title_offset + 1 ) + $end; + } else { + $columns += $status; + } + + return $columns; +} + +/** + * Add Images to print issue table columns + * + * @param $columns array Incoming print issue table columns + * + * @return array Modified columns + */ +function filter_article_columns_article_images( $columns ) { + $status = array( + 'post_img_num' => __( 'Images', 'eight-day-week' ), + ); + + /* put after char_count when available */ + $title_offset = array_search( 'char_count', array_keys( $columns ) ); + if ( $title_offset ) { + $end = $status + array_slice( $columns, $title_offset + 1, null ); + $columns = array_slice( $columns, 0, $title_offset + 1 ) + $end; + } else { + $columns += $status; + } + return $columns; } @@ -127,15 +163,18 @@ function filter_article_meta_article_status( $incoming, $item ) { * @return array Indexed article status terms */ function get_indexed_article_statuses() { - $article_statuses = get_terms( EDW_ARTICLE_STATUS_TAX, [ - 'hide_empty' => false, + $article_statuses = get_terms( + EDW_ARTICLE_STATUS_TAX, + array( + 'hide_empty' => false, - ] ); + ) + ); if ( ! $article_statuses || is_wp_error( $article_statuses ) ) { - return [ ]; + return array(); } - $actions = [ ]; + $actions = array(); foreach ( $article_statuses as $article_status ) { $actions[ $article_status->term_id ] = $article_status->name; } @@ -148,7 +187,7 @@ function get_indexed_article_statuses() { * Only does so for an print-issue-edit-capable user */ function output_bulk_article_status_editor() { - if( ! User\current_user_can_edit_print_issue() ) { + if ( ! User\current_user_can_edit_print_issue() ) { return; } $statuses = get_indexed_article_statuses(); @@ -170,6 +209,7 @@ function output_bulk_article_status_editor() { /** * Handles a request to edit articles' status + * * @todo refactor into pattern so that json functions are higher up, and the handler throws exceptions insteads */ function bulk_edit_article_statuses_ajax() { @@ -179,9 +219,9 @@ function bulk_edit_article_statuses_ajax() { $term_id = absint( $_POST['status'] ); $article_ids = $_POST['checked_articles']; - //sanitize - only allow comma delimited integers + // sanitize - only allow comma delimited integers if ( ! ctype_digit( str_replace( ',', '', $article_ids ) ) ) { - \Eight_Day_Week\Core\send_json_error( [ 'message' => __( 'Invalid article IDs specified in the request.', 'eight-day-week-print-workflow' ) ] ); + \Eight_Day_Week\Core\send_json_error( array( 'message' => __( 'Invalid article IDs specified in the request.', 'eight-day-week-print-workflow' ) ) ); } $article_ids = explode( ',', $article_ids ); @@ -189,11 +229,10 @@ function bulk_edit_article_statuses_ajax() { try { bulk_edit_article_statuses( $term_id, $article_ids ); } catch ( \Exception $e ) { - \Eight_Day_Week\Core\send_json_error( [ 'message' => $e->getMessage() ] ); + \Eight_Day_Week\Core\send_json_error( array( 'message' => $e->getMessage() ) ); } \Eight_Day_Week\Core\send_json_success(); - } /** @@ -201,7 +240,7 @@ function bulk_edit_article_statuses_ajax() { * Note it does *not* append terms, it replaces them * * @param string $status_term_id The term ID to add to the articles - * @param array $article_ids Article IDs to which to add the term + * @param array $article_ids Article IDs to which to add the term * * @throws \Exception WP Error message if invalid tax specified */ From 275fdd9086cb7038e7efe35f7a12ba2c3cc2450d Mon Sep 17 00:00:00 2001 From: Ben Marshall Date: Mon, 12 Jun 2023 10:40:35 -0500 Subject: [PATCH 2/7] Code updates to better confirm to coding standards --- eight-day-week.php | 53 ++-- includes/functions/admin-menu-page.php | 29 ++- includes/functions/articles.php | 5 + includes/functions/core.php | 54 +++-- includes/functions/plugins/article-byline.php | 36 +-- includes/functions/plugins/article-count.php | 32 +-- includes/functions/plugins/article-export.php | 5 + includes/functions/plugins/article-status.php | 5 + .../functions/plugins/issue-publication.php | 45 ++-- includes/functions/plugins/issue-status.php | 76 +++--- includes/functions/print-issue-columns.php | 21 +- includes/functions/print-issue.php | 229 +++++++++--------- includes/functions/sections.php | 115 ++++----- includes/functions/taxonomies.php | 71 +++--- includes/functions/user-roles.php | 125 +++++----- includes/lib/zip.lib.php | 137 +++++------ plugins.php | 26 +- vip-compat.php | 13 +- 18 files changed, 586 insertions(+), 491 deletions(-) diff --git a/eight-day-week.php b/eight-day-week.php index 02f61f4..7b0b07d 100644 --- a/eight-day-week.php +++ b/eight-day-week.php @@ -7,6 +7,8 @@ * Author URI: https://10up.com * License: GPLv2+ * Text Domain: eight-day-week-print-workflow + * + * @package eight-day-week */ /** @@ -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 ); } } @@ -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; } @@ -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' ); /** @@ -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; } diff --git a/includes/functions/admin-menu-page.php b/includes/functions/admin-menu-page.php index 09e9661..e465b50 100644 --- a/includes/functions/admin-menu-page.php +++ b/includes/functions/admin-menu-page.php @@ -1,4 +1,10 @@ + // dirty hack until https://core.trac.wordpress.org/ticket/22895 is solved + add_action( + 'admin_head', + function() { + ?> - $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 { @@ -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 ); @@ -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; @@ -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(); } @@ -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', ... */ @@ -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; diff --git a/includes/functions/plugins/article-byline.php b/includes/functions/plugins/article-byline.php index ad3af63..7cf28fb 100644 --- a/includes/functions/plugins/article-byline.php +++ b/includes/functions/plugins/article-byline.php @@ -1,5 +1,4 @@ ID ); if ( $byline && ! is_wp_error( $byline ) ) { - $byline = implode( ', ', wp_list_pluck( $byline, 'display_name' ) ); + $byline = implode( ', ', wp_list_pluck( $byline, 'display_name' ) ); } return $byline; @@ -90,7 +92,7 @@ function get_article_byline( $article ) { * @return array of \WP_Users */ function get_authors( $post_id ) { - $authors = [ ]; + $authors = array(); if ( ! $post_id ) { $post_id = get_the_ID(); @@ -104,7 +106,7 @@ function get_authors( $post_id ) { } else { $author = get_user_by( 'id', get_post_field( 'post_author', $post_id ) ); if ( $author ) { - $authors = [ $author ]; + $authors = array( $author ); } } diff --git a/includes/functions/plugins/article-count.php b/includes/functions/plugins/article-count.php index f3befbe..b009b53 100644 --- a/includes/functions/plugins/article-count.php +++ b/includes/functions/plugins/article-count.php @@ -1,5 +1,4 @@ 0 ) { foreach ( $sections as $section_id ) { diff --git a/includes/functions/plugins/article-export.php b/includes/functions/plugins/article-export.php index 3ffe0b0..7f0f996 100644 --- a/includes/functions/plugins/article-export.php +++ b/includes/functions/plugins/article-export.php @@ -1,4 +1,9 @@ __( 'Publications', 'eight-day-week-print-workflow' ), 'singular_name' => __( 'Publication', 'eight-day-week-print-workflow' ), 'search_items' => __( 'Search Publications', 'eight-day-week-print-workflow' ), @@ -50,10 +59,10 @@ function register_taxonomy() { 'menu_name' => __( 'Publication', 'eight-day-week-print-workflow' ), 'separate_items_with_commas' => '', 'choose_from_most_used' => __( 'Choose a Publication', 'eight-day-week-print-workflow' ), - 'not_found' => __( 'No Publications found.', 'eight-day-week-print-workflow' ) - ]; + 'not_found' => __( 'No Publications found.', 'eight-day-week-print-workflow' ), + ); - $args = [ + $args = array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, @@ -63,8 +72,8 @@ function register_taxonomy() { 'rewrite' => false, 'public' => true, 'publicly_queryable' => false, - 'capabilities' => [ 'manage_' . EDW_PRINT_ISSUE_CPT ] - ]; + 'capabilities' => array( 'manage_' . EDW_PRINT_ISSUE_CPT ), + ); \register_taxonomy( 'print_issue_publication', EDW_PRINT_ISSUE_CPT, $args ); } diff --git a/includes/functions/plugins/issue-status.php b/includes/functions/plugins/issue-status.php index e118ecf..ec7deb5 100644 --- a/includes/functions/plugins/issue-status.php +++ b/includes/functions/plugins/issue-status.php @@ -1,4 +1,9 @@ __( 'Issue Statuses', 'eight-day-week-print-workflow' ), 'singular_name' => __( 'Issue Status', 'eight-day-week-print-workflow' ), 'search_items' => __( 'Search Issue Statuses', 'eight-day-week-print-workflow' ), @@ -66,9 +76,9 @@ function register_taxonomy() { 'parent_item_colon' => __( 'Parent Issue Status Colon', 'eight-day-week-print-workflow' ), 'no_terms' => __( 'No Issue Status', 'eight-day-week-print-workflow' ), 'back_to_items' => __( 'Back to Issue Statuses', 'eight-day-week-print-workflow' ), - ]; + ); - $args = [ + $args = array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, @@ -78,8 +88,8 @@ function register_taxonomy() { 'rewrite' => false, 'public' => true, 'publicly_queryable' => false, - 'capabilities' => [ 'manage_' . EDW_PRINT_ISSUE_CPT ] - ]; + 'capabilities' => array( 'manage_' . EDW_PRINT_ISSUE_CPT ), + ); \register_taxonomy( 'print_issue_status', EDW_PRINT_ISSUE_CPT, $args ); } @@ -90,19 +100,19 @@ function register_taxonomy() { function add_issue_status_filters() { global $typenow; - //Display on our custom post type only + // Display on our custom post type only if ( $typenow == EDW_PRINT_ISSUE_CPT ) { $tax_slug = 'print_issue_status'; $tax_obj = get_taxonomy( $tax_slug ); $tax_name = $tax_obj->labels->name; - //print issues are private, so hide_empty must be false - //as private posts don't count towards term count - $terms = get_terms( $tax_slug, [ 'hide_empty' => false ] ); + // print issues are private, so hide_empty must be false + // as private posts don't count towards term count + $terms = get_terms( $tax_slug, array( 'hide_empty' => false ) ); echo '

@@ -161,7 +166,7 @@ function sections_meta_box( $post, $args ) { */ function get_sections( $post_id ) { $section_ids = get_post_meta( $post_id, 'sections', true ); - //sanitize - only allow comma delimited integers + // sanitize - only allow comma delimited integers if ( ! ctype_digit( str_replace( ',', '', $section_ids ) ) ) { return ''; } @@ -180,8 +185,8 @@ function get_sections( $post_id ) { * @param $post \WP_Post The current post */ function add_section_output( $post ) { - if( EDW_PRINT_ISSUE_CPT !== $post->post_type || - ! User\current_user_can_edit_print_issue() + if ( EDW_PRINT_ISSUE_CPT !== $post->post_type || + ! User\current_user_can_edit_print_issue() ) { return; } @@ -226,7 +231,7 @@ class="button button-secondary dashicons dashicons-yes"> */ function update_print_issue_sections( $post_id, $post, $update ) { - if( ! isset( $_POST['pi-section-ids'] ) ) { + if ( ! isset( $_POST['pi-section-ids'] ) ) { return; } @@ -241,7 +246,6 @@ function update_print_issue_sections( $post_id, $post, $update ) { } set_print_issue_sections( $section_ids, $post_id ); - } /** * Saves section IDs to the DB @@ -252,21 +256,21 @@ function update_print_issue_sections( $post_id, $post, $update ) { */ function set_print_issue_sections( $section_ids, $print_issue_id ) { - //sanitize - only allow comma delimited integers + // sanitize - only allow comma delimited integers if ( ! ctype_digit( str_replace( ',', '', $section_ids ) ) ) { return; } update_post_meta( $print_issue_id, 'sections', $section_ids ); - //allow other parts to hook + // allow other parts to hook do_action( 'save_print_issue_sections', $print_issue_id, $section_ids ); - } /** * Class Section_Factory + * * @package Eight_Day_Week\Sections * * Factory that creates + updates sections @@ -284,10 +288,10 @@ class Section_Factory { */ public static function create( $name ) { - $info = [ + $info = array( 'post_title' => $name, - 'post_type' => EDW_SECTION_CPT, - ]; + 'post_type' => EDW_SECTION_CPT, + ); $section_id = wp_insert_post( $info ); if ( $section_id ) { return new Section( $section_id ); @@ -298,7 +302,7 @@ public static function create( $name ) { public static function assign_to_print_issue( $section, $print_issue ) { $current_sections = get_sections( $print_issue->ID ); - $new_sections = $current_sections ? $current_sections . ',' . $section->ID : $section->ID; + $new_sections = $current_sections ? $current_sections . ',' . $section->ID : $section->ID; set_print_issue_sections( $new_sections, $print_issue->ID ); return $new_sections; } @@ -314,7 +318,7 @@ public static function create_ajax() { $name = isset( $_POST['name'] ) ? sanitize_text_field( $_POST['name'] ) : false; if ( ! $name ) { - Core\send_json_error( [ 'message' => __( 'Please enter a section name.', 'eight-day-week-print-workflow' ) ] ); + Core\send_json_error( array( 'message' => __( 'Please enter a section name.', 'eight-day-week-print-workflow' ) ) ); } $print_issue_id = absint( $_POST['print_issue_id'] ); @@ -327,16 +331,16 @@ public static function create_ajax() { try { $section = self::create( $name ); } catch ( \Exception $e ) { - //let the whoops message run its course + // let the whoops message run its course $section = null; } if ( $section instanceof Section ) { self::assign_to_print_issue( $section, $print_issue ); - Core\send_json_success( [ 'section_id' => $section->ID ] ); + Core\send_json_success( array( 'section_id' => $section->ID ) ); } - Core\send_json_error( [ 'message' => __( 'Whoops! Something went awry.', 'eight-day-week-print-workflow' ) ] ); + Core\send_json_error( array( 'message' => __( 'Whoops! Something went awry.', 'eight-day-week-print-workflow' ) ) ); } /** @@ -350,17 +354,17 @@ public static function update_title_ajax() { $title = isset( $_POST['title'] ) ? sanitize_text_field( $_POST['title'] ) : false; if ( ! $title ) { - Core\send_json_error( [ 'message' => __( 'Please enter a section name.', 'eight-day-week-print-workflow' ) ] ); + Core\send_json_error( array( 'message' => __( 'Please enter a section name.', 'eight-day-week-print-workflow' ) ) ); } $post_id = isset( $_POST['post_id'] ) ? sanitize_text_field( $_POST['post_id'] ) : false; if ( ! $post_id ) { - Core\send_json_error( [ 'message' => __( 'Whoops! This section appears to be invalid.', 'eight-day-week-print-workflow' ) ] ); + Core\send_json_error( array( 'message' => __( 'Whoops! This section appears to be invalid.', 'eight-day-week-print-workflow' ) ) ); } try { self::update_title( $title, $post_id ); } catch ( \Exception $e ) { - Core\send_json_error( [ 'message' => $e->getMessage() ] ); + Core\send_json_error( array( 'message' => $e->getMessage() ) ); } Core\send_json_success(); } @@ -381,6 +385,7 @@ static function update_title( $title, $id ) { /** * Class Section + * * @package Eight_Day_Week\Sections * * Class that represents a section object + offers utility functions for it @@ -442,7 +447,7 @@ private function import_post_info() { foreach ( $info as $key => $value ) { if ( ! empty( $key ) ) { $this->$key = $value; - } else if ( ! empty( $key ) && ! method_exists( $this, $key ) ) { + } elseif ( ! empty( $key ) && ! method_exists( $this, $key ) ) { $this->$key = $value; } } @@ -481,10 +486,10 @@ function update_title( $title ) { throw new \Exception( __( 'Please supply a valid, non-empty title', 'eight-day-week-print-workflow' ) ); } $title = sanitize_text_field( $title ); - $args = [ + $args = array( 'ID' => $this->ID, 'post_title' => $title, - ]; + ); $result = $this->update( $args ); } } @@ -500,48 +505,50 @@ function save_metabox_order() { check_ajax_referer( 'meta-box-order' ); $order = isset( $_POST['order'] ) ? (array) $_POST['order'] : false; - if( ! $order ) { + if ( ! $order ) { return; } $page = isset( $_POST['page'] ) ? $_POST['page'] : ''; - if ( $page != sanitize_key( $page ) ) + if ( $page != sanitize_key( $page ) ) { wp_die( 0 ); + } - //only intercept PI CPT - if( EDW_PRINT_ISSUE_CPT !== $page ) { + // only intercept PI CPT + if ( EDW_PRINT_ISSUE_CPT !== $page ) { return; } - if ( ! $user = wp_get_current_user() ) + if ( ! $user = wp_get_current_user() ) { wp_die( -1 ); + } - //don't allow print prod users to re-order - if( ! User\current_user_can_edit_print_issue() ) { + // don't allow print prod users to re-order + if ( ! User\current_user_can_edit_print_issue() ) { wp_die( -1 ); } - //grab the post ID from the section template + // grab the post ID from the section template $metaboxes = explode( ',', $order['normal'] ); - $template = false; - foreach( $metaboxes as $metabox ) { - if( strpos( $metabox, 'template' ) !== FALSE ) { + $template = false; + foreach ( $metaboxes as $metabox ) { + if ( strpos( $metabox, 'template' ) !== false ) { $template = $metabox; } } - //couldnt find PI template, which contains PI post ID - if( ! $template ) { + // couldnt find PI template, which contains PI post ID + if ( ! $template ) { return; } - $parts = explode( '-', $template ); + $parts = explode( '-', $template ); $post_id = end( $parts ); $post = get_post( $post_id ); - if( ! $post || ( $post ) && EDW_PRINT_ISSUE_CPT !== $post->post_type ) { + if ( ! $post || ( $post ) && EDW_PRINT_ISSUE_CPT !== $post->post_type ) { return; } @@ -560,7 +567,7 @@ function save_metabox_order() { function get_section_order( $result ) { global $post; - if( $post && $order = get_post_meta( $post->ID, 'section-order', true ) ) { + if ( $post && $order = get_post_meta( $post->ID, 'section-order', true ) ) { return $order; } @@ -570,7 +577,7 @@ function get_section_order( $result ) { /** * Outputs a Save button */ -function section_save_button(){ +function section_save_button() { if ( Print_Issue\is_read_only_view() || ! User\current_user_can_edit_print_issue() ) { return; } diff --git a/includes/functions/taxonomies.php b/includes/functions/taxonomies.php index a5189c8..2cd1661 100644 --- a/includes/functions/taxonomies.php +++ b/includes/functions/taxonomies.php @@ -1,4 +1,9 @@ labels->singular_name, - __NAMESPACE__ . '\create_taxonomy_dropdown_metabox', + __NAMESPACE__ . '\create_taxonomy_dropdown_metabox', EDW_PRINT_ISSUE_CPT, 'side', 'default', - [ 'taxonomy' => $taxonomy ] + array( 'taxonomy' => $taxonomy ) ); } @@ -34,19 +39,19 @@ function add_taxonomy_dropdown_meta_box( $tax_slug ) { * Displays a taxonomy dropdown box metabox * * @param \WP_Post $post The currently edited post - * @param array $metabox Contains args from the add_meta_box func. Important one: pass in a Taxonomy object. + * @param array $metabox Contains args from the add_meta_box func. Important one: pass in a Taxonomy object. */ function create_taxonomy_dropdown_metabox( $post, $metabox ) { $taxonomy = $metabox['args']['taxonomy']; - //bail if invalid taxonomy or not an object - if( !is_object( $taxonomy) || ( is_object( $taxonomy ) && ! get_taxonomy( $taxonomy->name ) ) ) { + // bail if invalid taxonomy or not an object + if ( ! is_object( $taxonomy ) || ( is_object( $taxonomy ) && ! get_taxonomy( $taxonomy->name ) ) ) { return; } // uses same noncename as default box so no save_post hook needed - wp_nonce_field( 'taxonomy_'. $taxonomy->name, 'taxonomy_noncename' ); + wp_nonce_field( 'taxonomy_' . $taxonomy->name, 'taxonomy_noncename' ); // get terms associated with this post $names = get_the_terms( get_the_ID(), $taxonomy->name ); @@ -54,15 +59,15 @@ function create_taxonomy_dropdown_metabox( $post, $metabox ) { // get all terms in this taxonomy $terms = (array) get_terms( $taxonomy->name, 'hide_empty=0' ); - if( ! $terms ) { + if ( ! $terms ) { echo '

' . sprintf( esc_html_x( 'No %s created.', 'When no terms are present, i.e. "No publications created."', 'eight-day-week-print-workflow' ), esc_html( $taxonomy->labels->name ) ) . '

'; return; } // filter the ids out of the terms - $existing = ( !is_wp_error( $names ) && !empty( $names ) ) + $existing = ( ! is_wp_error( $names ) && ! empty( $names ) ) ? (array) wp_list_pluck( $names, 'term_id' ) - : []; + : array(); // Check if taxonomy is hierarchical // Terms are saved differently between types @@ -72,38 +77,38 @@ function create_taxonomy_dropdown_metabox( $post, $metabox ) { $default_val = $h ? 0 : ''; // input name - $name = $h ? 'tax_input['. $taxonomy->name .'][]' : 'tax_input['. $taxonomy->name .']'; + $name = $h ? 'tax_input[' . $taxonomy->name . '][]' : 'tax_input[' . $taxonomy->name . ']'; $default_text = sprintf( _x( 'No %s', 'Provides a select option for choosing "no" term.', 'eight-day-week-print-workflow' ), esc_html( $taxonomy->labels->singular_name ) ); - $select = ''; + $select = ''; $selected_term = false; $select .= ''; - if( User\current_user_can_edit_print_issue() ) { - echo wp_kses( $select, [ - 'option' => [ - 'value' => [], - 'selected' => [], - ], - 'select' => [ - 'name' => [], - 'id' => [], - ] - ] ); + if ( User\current_user_can_edit_print_issue() ) { + echo wp_kses( + $select, + array( + 'option' => array( + 'value' => array(), + 'selected' => array(), + ), + 'select' => array( + 'name' => array(), + 'id' => array(), + ), + ) + ); } else { echo '

' . esc_html( $selected_term ? $selected_term : $default_text ) . '

'; } - } diff --git a/includes/functions/user-roles.php b/includes/functions/user-roles.php index 372348e..451c3df 100644 --- a/includes/functions/user-roles.php +++ b/includes/functions/user-roles.php @@ -1,4 +1,10 @@ true, - //grant all caps related to the CPT - 'edit_post' => true, - "edit_{$pt}" => true, - "read_{$pt}" => true, - "delete_{$pt}" => true, - "edit_{$pt}s" => true, - "edit_others_{$pt}s" => true, - "publish_{$pt}s" => true, - "read_private_{$pt}s" => true, - "delete_{$pt}s" => true, - "delete_private_{$pt}s" => true, + return array( + // grant dashboard access + 'read' => true, + // grant all caps related to the CPT + 'edit_post' => true, + "edit_{$pt}" => true, + "read_{$pt}" => true, + "delete_{$pt}" => true, + "edit_{$pt}s" => true, + "edit_others_{$pt}s" => true, + "publish_{$pt}s" => true, + "read_private_{$pt}s" => true, + "delete_{$pt}s" => true, + "delete_private_{$pt}s" => true, "delete_published_{$pt}s" => true, - "delete_others_{$pt}s" => true, - "edit_private_{$pt}s" => true, - "edit_published_{$pt}s" => true, - "edit_{$pt}s" => true, - //custom cap for submenus - "manage_{$pt}" => true, - ]; + "delete_others_{$pt}s" => true, + "edit_private_{$pt}s" => true, + "edit_published_{$pt}s" => true, + "edit_{$pt}s" => true, + // custom cap for submenus + "manage_{$pt}" => true, + ); } /** @@ -119,26 +125,26 @@ function add_print_production_role() { */ function get_production_caps() { $pt = EDW_PRINT_ISSUE_CPT; - return [ - //grants dashboard access + return array( + // grants dashboard access 'read' => true, - //read CPT + // read CPT "read_{$pt}" => true, - //read private CPT + // read private CPT "read_private_{$pt}s" => true, - //access post list table + // access post list table "edit_{$pt}s" => true, - //access post editor + // access post editor "edit_{$pt}" => true, - ]; + ); } /** * Assigns print capabilities to WP built in roles */ function add_caps_to_built_in_roles() { - //use array keys so things like edit_post => false - //don't come over + // use array keys so things like edit_post => false + // don't come over PP\add_role_caps( 'administrator', array_keys( get_editor_caps() ) ); } @@ -151,7 +157,7 @@ function add_caps_to_built_in_roles() { */ function editable_roles( $roles ) { global $edw_roles; - //remove print roles by key + // remove print roles by key $roles = array_diff_key( $roles, array_flip( $edw_roles ) ); return $roles; } @@ -168,12 +174,12 @@ function output_print_role_on_user_list_table( $which ) { $select_id = 'top' === $which ? 'pp-print-role' : 'pp-print-role2'; - echo ''; echo ''; echo ''; - foreach( (array) $edw_roles as $role ) { + foreach ( (array) $edw_roles as $role ) { $wp_role = get_role( $role ); - if( ! $wp_role ) { + if ( ! $wp_role ) { continue; } $name = $role_names[ $role ]; @@ -212,13 +218,13 @@ function get_role_names() { function get_user_print_roles( $user ) { global $edw_roles; $current_roles = $user->roles; - $roles = get_editable_roles(); + $roles = get_editable_roles(); - //flip to check values as if they were keys (to match WP_Roles->roles format) - //flip back to get back indexed array for later + // flip to check values as if they were keys (to match WP_Roles->roles format) + // flip back to get back indexed array for later $non_built_in = array_flip( array_diff_key( array_flip( $current_roles ), $roles ) ); - if( ! $non_built_in || ! is_array( $non_built_in ) ) { - return []; + if ( ! $non_built_in || ! is_array( $non_built_in ) ) { + return array(); } return array_intersect( $edw_roles, $non_built_in ); } @@ -236,7 +242,7 @@ function update_users_print_role() { return; } - //don't proccess the default option + // don't proccess the default option if ( -1 == $_GET['pp-print-role'] && -1 == $_GET['pp-print-role2'] ) { return; } @@ -253,7 +259,7 @@ function update_users_print_role() { $new_role = -1 != $_GET['pp-print-role'] ? $_GET['pp-print-role'] : $_GET['pp-print-role2']; - //validate requested print role + // validate requested print role if ( 'remove' !== $new_role && ! in_array( $new_role, $edw_roles ) ) { wp_die( __( 'You can’t give users that print role.', 'eight-day-week-print-workflow' ) ); } @@ -275,22 +281,21 @@ function update_users_print_role() { $user = get_userdata( $id ); - //remove all previous roles + // remove all previous roles foreach ( (array) $edw_roles as $role_to_remove ) { $user->remove_role( $role_to_remove ); } - //add new role + // add new role if ( 'remove' !== $new_role ) { $user->add_role( sanitize_text_field( $new_role ) ); } } - $redirect = remove_query_arg( [ 'pp-print-role' ] ); - $redirect = add_query_arg( [ 'update' => 'promote' ], $redirect ); + $redirect = remove_query_arg( array( 'pp-print-role' ) ); + $redirect = add_query_arg( array( 'update' => 'promote' ), $redirect ); wp_redirect( $redirect ); exit; - } /** @@ -307,25 +312,24 @@ function update_users_print_role() { function re_save_print_roles( $user_id, $new_role, $old_roles ) { global $edw_roles; - //if adding a print role, we're done here - if( in_array( $new_role, $edw_roles ) ) { + // if adding a print role, we're done here + if ( in_array( $new_role, $edw_roles ) ) { return; } $user = get_userdata( $user_id ); - //if a user has a print role already, bail - if( array_intersect( $edw_roles, $user->roles ) ) { + // if a user has a print role already, bail + if ( array_intersect( $edw_roles, $user->roles ) ) { return; } $old_print_roles = array_intersect( $edw_roles, $old_roles ); - //get the first print role + // get the first print role $old_print_role = reset( $old_print_roles ); $user->add_role( $old_print_role ); - } /** @@ -362,14 +366,13 @@ function add_print_role_column( $columns ) { * @param $user_id int The current user's ID * * @return string Modified value - * */ function add_print_role_column_text( $value, $column_name, $user_id ) { $user = get_userdata( $user_id ); if ( 'print_role' == $column_name ) { - $role = get_user_print_roles( $user ); + $role = get_user_print_roles( $user ); $role_names = get_role_names(); return $role ? $role_names[ reset( $role ) ] : ''; } diff --git a/includes/lib/zip.lib.php b/includes/lib/zip.lib.php index b045078..10861c6 100644 --- a/includes/lib/zip.lib.php +++ b/includes/lib/zip.lib.php @@ -1,4 +1,9 @@ file * * @var boolean $doWrite */ - var $doWrite = false; + var $doWrite = false; /** * Array to store compressed data * * @var array $datasec */ - var $datasec = array(); + var $datasec = array(); /** * Central directory * * @var array $ctrl_dir */ - var $ctrl_dir = array(); + var $ctrl_dir = array(); /** * End of central directory record @@ -42,7 +47,7 @@ class ZipFile * * @var integer $old_offset */ - var $old_offset = 0; + var $old_offset = 0; /** @@ -58,9 +63,8 @@ class ZipFile * * @return void */ - function setDoWrite() - { - $this -> doWrite = true; + function setDoWrite() { + $this->doWrite = true; } // end of the 'setDoWrite()' method /** @@ -73,11 +77,10 @@ function setDoWrite() * * @access private */ - function unix2DosTime($unixtime = 0) - { - $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime); + function unix2DosTime( $unixtime = 0 ) { + $timearray = ( $unixtime == 0 ) ? getdate() : getdate( $unixtime ); - if ($timearray['year'] < 1980) { + if ( $timearray['year'] < 1980 ) { $timearray['year'] = 1980; $timearray['mon'] = 1; $timearray['mday'] = 1; @@ -86,12 +89,12 @@ function unix2DosTime($unixtime = 0) $timearray['seconds'] = 0; } // end if - return (($timearray['year'] - 1980) << 25) - | ($timearray['mon'] << 21) - | ($timearray['mday'] << 16) - | ($timearray['hours'] << 11) - | ($timearray['minutes'] << 5) - | ($timearray['seconds'] >> 1); + return ( ( $timearray['year'] - 1980 ) << 25 ) + | ( $timearray['mon'] << 21 ) + | ( $timearray['mday'] << 16 ) + | ( $timearray['hours'] << 11 ) + | ( $timearray['minutes'] << 5 ) + | ( $timearray['seconds'] >> 1 ); } // end of the 'unix2DosTime()' method @@ -106,67 +109,66 @@ function unix2DosTime($unixtime = 0) * * @return void */ - function addFile($data, $name, $time = 0) - { - $name = str_replace('\\', '/', $name); + function addFile( $data, $name, $time = 0 ) { + $name = str_replace( '\\', '/', $name ); - $hexdtime = pack('V', $this->unix2DosTime($time)); + $hexdtime = pack( 'V', $this->unix2DosTime( $time ) ); - $fr = "\x50\x4b\x03\x04"; - $fr .= "\x14\x00"; // ver needed to extract - $fr .= "\x00\x00"; // gen purpose bit flag - $fr .= "\x08\x00"; // compression method - $fr .= $hexdtime; // last mod time and date + $fr = "\x50\x4b\x03\x04"; + $fr .= "\x14\x00"; // ver needed to extract + $fr .= "\x00\x00"; // gen purpose bit flag + $fr .= "\x08\x00"; // compression method + $fr .= $hexdtime; // last mod time and date // "local file header" segment - $unc_len = strlen($data); - $crc = crc32($data); - $zdata = gzcompress($data, 0); - $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug - $c_len = strlen($zdata); - $fr .= pack('V', $crc); // crc32 - $fr .= pack('V', $c_len); // compressed filesize - $fr .= pack('V', $unc_len); // uncompressed filesize - $fr .= pack('v', strlen($name)); // length of filename - $fr .= pack('v', 0); // extra field length - $fr .= $name; + $unc_len = strlen( $data ); + $crc = crc32( $data ); + $zdata = gzcompress( $data, 0 ); + $zdata = substr( substr( $zdata, 0, strlen( $zdata ) - 4 ), 2 ); // fix crc bug + $c_len = strlen( $zdata ); + $fr .= pack( 'V', $crc ); // crc32 + $fr .= pack( 'V', $c_len ); // compressed filesize + $fr .= pack( 'V', $unc_len ); // uncompressed filesize + $fr .= pack( 'v', strlen( $name ) ); // length of filename + $fr .= pack( 'v', 0 ); // extra field length + $fr .= $name; // "file data" segment $fr .= $zdata; // echo this entry on the fly, ... - if ($this -> doWrite) { + if ( $this->doWrite ) { echo $fr; } else { // ... OR add this entry to array - $this -> datasec[] = $fr; + $this->datasec[] = $fr; } // now add to central directory record - $cdrec = "\x50\x4b\x01\x02"; + $cdrec = "\x50\x4b\x01\x02"; $cdrec .= "\x00\x00"; // version made by $cdrec .= "\x14\x00"; // version needed to extract $cdrec .= "\x00\x00"; // gen purpose bit flag $cdrec .= "\x08\x00"; // compression method $cdrec .= $hexdtime; // last mod time & date - $cdrec .= pack('V', $crc); // crc32 - $cdrec .= pack('V', $c_len); // compressed filesize - $cdrec .= pack('V', $unc_len); // uncompressed filesize - $cdrec .= pack('v', strlen($name)); // length of filename - $cdrec .= pack('v', 0); // extra field length - $cdrec .= pack('v', 0); // file comment length - $cdrec .= pack('v', 0); // disk number start - $cdrec .= pack('v', 0); // internal file attributes - $cdrec .= pack('V', 32); // external file attributes + $cdrec .= pack( 'V', $crc ); // crc32 + $cdrec .= pack( 'V', $c_len ); // compressed filesize + $cdrec .= pack( 'V', $unc_len ); // uncompressed filesize + $cdrec .= pack( 'v', strlen( $name ) ); // length of filename + $cdrec .= pack( 'v', 0 ); // extra field length + $cdrec .= pack( 'v', 0 ); // file comment length + $cdrec .= pack( 'v', 0 ); // disk number start + $cdrec .= pack( 'v', 0 ); // internal file attributes + $cdrec .= pack( 'V', 32 ); // external file attributes // - 'archive' bit set - $cdrec .= pack('V', $this -> old_offset); // relative offset of local header - $this -> old_offset += strlen($fr); + $cdrec .= pack( 'V', $this->old_offset ); // relative offset of local header + $this->old_offset += strlen( $fr ); $cdrec .= $name; // optional extra field, file comment goes here // save to central directory - $this -> ctrl_dir[] = $cdrec; + $this->ctrl_dir[] = $cdrec; } // end of the 'addFile()' method @@ -177,22 +179,21 @@ function addFile($data, $name, $time = 0) * * @access public */ - function file() - { - $ctrldir = implode('', $this -> ctrl_dir); - $header = $ctrldir . - $this -> eof_ctrl_dir . - pack('v', sizeof($this -> ctrl_dir)) . //total #of entries "on this disk" - pack('v', sizeof($this -> ctrl_dir)) . //total #of entries overall - pack('V', strlen($ctrldir)) . //size of central dir - pack('V', $this -> old_offset) . //offset to start of central dir - "\x00\x00"; //.zip file comment length - - if ($this -> doWrite) { // Send central directory & end ctrl dir to STDOUT + function file() { + $ctrldir = implode( '', $this->ctrl_dir ); + $header = $ctrldir . + $this->eof_ctrl_dir . + pack( 'v', sizeof( $this->ctrl_dir ) ) . // total #of entries "on this disk" + pack( 'v', sizeof( $this->ctrl_dir ) ) . // total #of entries overall + pack( 'V', strlen( $ctrldir ) ) . // size of central dir + pack( 'V', $this->old_offset ) . // offset to start of central dir + "\x00\x00"; // .zip file comment length + + if ( $this->doWrite ) { // Send central directory & end ctrl dir to STDOUT echo $header; - return ""; // Return empty string + return ''; // Return empty string } else { // Return entire ZIP archive as string - $data = implode('', $this -> datasec); + $data = implode( '', $this->datasec ); return $data . $header; } } // end of the 'file()' method diff --git a/plugins.php b/plugins.php index f6e70f3..c07342a 100644 --- a/plugins.php +++ b/plugins.php @@ -1,4 +1,9 @@ $namespace ) { - if( FALSE !== strpos( $file_path, EDW_INC . 'functions/plugins/' ) ) { - if ( ! should_load_plugin( str_replace( '.php', '', basename( $file_path ) ) ) ) { - unset( $files[ $file_path ] ); + foreach ( $files as $file_path => $namespace ) { + if ( false !== strpos( $file_path, EDW_INC . 'functions/plugins/' ) ) { + if ( ! should_load_plugin( str_replace( '.php', '', basename( $file_path ) ) ) ) { + unset( $files[ $file_path ] ); + } } } - } - return $files; -} ); \ No newline at end of file + return $files; + } +); diff --git a/vip-compat.php b/vip-compat.php index bfe1376..40a30a6 100644 --- a/vip-compat.php +++ b/vip-compat.php @@ -1,4 +1,9 @@ false ) ); * * @param string $role Role name - * @param array $caps Key/value array of capabilities for this role + * @param array $caps Key/value array of capabilities for this role */ function merge_role_caps( $role, $caps ) { if ( function_exists( 'wpcom_vip_merge_role_caps' ) ) { @@ -128,4 +133,4 @@ function merge_role_caps( $role, $caps ) { $wp_user_roles[ $role ]['capabilities'] = array_merge( $current_caps, (array) $caps ); } } -} \ No newline at end of file +} From 1fdce6fa8e454eef54465193cb994ce201ec9551 Mon Sep 17 00:00:00 2001 From: Ben Marshall Date: Mon, 26 Jun 2023 07:30:05 -0500 Subject: [PATCH 3/7] fix(qa): minor code updates from qa --- includes/functions/articles.php | 2 +- includes/functions/plugins/article-export.php | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/includes/functions/articles.php b/includes/functions/articles.php index 73e4afa..d42b60f 100644 --- a/includes/functions/articles.php +++ b/includes/functions/articles.php @@ -183,7 +183,7 @@ function prepare_items() { $post->post_content .= $single; } } - $post_img_num = (int) preg_match_all( '/]*>/', $post->post_content, $array ); + $post_img_num = (int) preg_match_all( '/]*>/', $post->post_content, $matches ); if ( has_post_thumbnail( $post->ID ) ) { ++$post_img_num; } diff --git a/includes/functions/plugins/article-export.php b/includes/functions/plugins/article-export.php index 3ffe0b0..2819e5a 100644 --- a/includes/functions/plugins/article-export.php +++ b/includes/functions/plugins/article-export.php @@ -610,7 +610,6 @@ function ( $matches ) { $gallery_images = explode( ',', $matches[1] ); foreach ( $gallery_images as $img_id ) { $image = $this->get_image_name( (int) $img_id ); - wp_mail( 'aaaa@asefetbhtregfsefwef.com', 'TEST', print_r( $image, 1 ) ); } } // return $image ? $this->get_image_tag($image[1], $caption) : ''; @@ -627,10 +626,6 @@ function ( $matches ) { $image = false; if ( preg_match( '/wp\-image\-(\d+)\D/i', $matches[0], $matches2 ) ) { $image = $this->get_image_name( (int) $matches2[1] ); - // } else if( preg_match('/src="([^"]*)"|src=\'([^\']*)\'/Usi', $matches[0], $matches2 ) ) { - // $components = parse_url($matches2[1]); - // $full_url = empty($components['host']) ? $_SERVER['HTTP_HOST'] . '/' . $matches2[1] : $matches2[1]; - // $image = $this->get_image_name(false, $full_url); } return $image ? $this->get_image_tag( $image[1] ) : ''; }, From 282b637b67c2522acebfe6ab60acc7142a95c9ef Mon Sep 17 00:00:00 2001 From: Ben Marshall Date: Fri, 14 Jul 2023 13:01:47 -0500 Subject: [PATCH 4/7] revert(code standards): reverted changes that were specific to coding standards so they can be addressed in another pull request --- eight-day-week.php | 53 ++-- includes/functions/admin-menu-page.php | 29 +-- includes/functions/core.php | 54 ++--- includes/functions/plugins/article-byline.php | 36 ++- includes/functions/plugins/article-count.php | 32 ++- .../functions/plugins/issue-publication.php | 45 ++-- includes/functions/plugins/issue-status.php | 76 +++--- includes/functions/print-issue-columns.php | 21 +- includes/functions/print-issue.php | 229 +++++++++--------- includes/functions/sections.php | 115 +++++---- includes/functions/taxonomies.php | 71 +++--- includes/functions/user-roles.php | 125 +++++----- includes/lib/zip.lib.php | 137 ++++++----- plugins.php | 26 +- vip-compat.php | 11 +- 15 files changed, 490 insertions(+), 570 deletions(-) diff --git a/eight-day-week.php b/eight-day-week.php index 7b0b07d..02f61f4 100644 --- a/eight-day-week.php +++ b/eight-day-week.php @@ -7,8 +7,6 @@ * Author URI: https://10up.com * License: GPLv2+ * Text Domain: eight-day-week-print-workflow - * - * @package eight-day-week */ /** @@ -29,26 +27,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', __DIR__ . '/' ); -define( 'EDW_INC', EDW_PATH . 'includes/' ); +define( 'EDW_URL', Eight_Day_Week\plugins_url( __FILE__ ) ); +define( 'EDW_PATH', dirname( __FILE__ ) . '/' ); +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 ); } } @@ -74,7 +72,7 @@ function edw_bootstrap() { $core_file = EDW_INC . 'functions' . DIRECTORY_SEPARATOR . 'core.php'; - if ( ! isset( $map[ $core_file ] ) ) { + if( ! isset( $map[ $core_file ] ) ) { return; } @@ -83,24 +81,25 @@ 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' ); /** @@ -119,39 +118,39 @@ function edw_build_namespace_map() { RecursiveIteratorIterator::CATCH_GET_CHILD ); - $map = array(); + $map = []; 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; } diff --git a/includes/functions/admin-menu-page.php b/includes/functions/admin-menu-page.php index e465b50..09e9661 100644 --- a/includes/functions/admin-menu-page.php +++ b/includes/functions/admin-menu-page.php @@ -1,10 +1,4 @@ + //dirty hack until https://core.trac.wordpress.org/ticket/22895 is solved + add_action( 'admin_head', function() { + ?> - $data ) ); +function tack_on_ajax_response( $data = [] ) { + $data = ( is_array( $data ) || is_object( $data ) ? $data : [ '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 { @@ -153,7 +146,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 ); @@ -171,16 +164,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; @@ -201,7 +194,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(); } @@ -219,22 +212,21 @@ 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', ... */ @@ -244,14 +236,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; diff --git a/includes/functions/plugins/article-byline.php b/includes/functions/plugins/article-byline.php index 7cf28fb..ad3af63 100644 --- a/includes/functions/plugins/article-byline.php +++ b/includes/functions/plugins/article-byline.php @@ -1,4 +1,5 @@ ID ); if ( $byline && ! is_wp_error( $byline ) ) { - $byline = implode( ', ', wp_list_pluck( $byline, 'display_name' ) ); + $byline = implode( ', ', wp_list_pluck( $byline, 'display_name' ) ); } return $byline; @@ -92,7 +90,7 @@ function get_article_byline( $article ) { * @return array of \WP_Users */ function get_authors( $post_id ) { - $authors = array(); + $authors = [ ]; if ( ! $post_id ) { $post_id = get_the_ID(); @@ -106,7 +104,7 @@ function get_authors( $post_id ) { } else { $author = get_user_by( 'id', get_post_field( 'post_author', $post_id ) ); if ( $author ) { - $authors = array( $author ); + $authors = [ $author ]; } } diff --git a/includes/functions/plugins/article-count.php b/includes/functions/plugins/article-count.php index b009b53..f3befbe 100644 --- a/includes/functions/plugins/article-count.php +++ b/includes/functions/plugins/article-count.php @@ -1,4 +1,5 @@ 0 ) { foreach ( $sections as $section_id ) { diff --git a/includes/functions/plugins/issue-publication.php b/includes/functions/plugins/issue-publication.php index 6aeb76c..54924e8 100644 --- a/includes/functions/plugins/issue-publication.php +++ b/includes/functions/plugins/issue-publication.php @@ -1,9 +1,4 @@ __( 'Publications', 'eight-day-week-print-workflow' ), 'singular_name' => __( 'Publication', 'eight-day-week-print-workflow' ), 'search_items' => __( 'Search Publications', 'eight-day-week-print-workflow' ), @@ -59,10 +50,10 @@ function register_taxonomy() { 'menu_name' => __( 'Publication', 'eight-day-week-print-workflow' ), 'separate_items_with_commas' => '', 'choose_from_most_used' => __( 'Choose a Publication', 'eight-day-week-print-workflow' ), - 'not_found' => __( 'No Publications found.', 'eight-day-week-print-workflow' ), - ); + 'not_found' => __( 'No Publications found.', 'eight-day-week-print-workflow' ) + ]; - $args = array( + $args = [ 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, @@ -72,8 +63,8 @@ function register_taxonomy() { 'rewrite' => false, 'public' => true, 'publicly_queryable' => false, - 'capabilities' => array( 'manage_' . EDW_PRINT_ISSUE_CPT ), - ); + 'capabilities' => [ 'manage_' . EDW_PRINT_ISSUE_CPT ] + ]; \register_taxonomy( 'print_issue_publication', EDW_PRINT_ISSUE_CPT, $args ); } diff --git a/includes/functions/plugins/issue-status.php b/includes/functions/plugins/issue-status.php index ec7deb5..e118ecf 100644 --- a/includes/functions/plugins/issue-status.php +++ b/includes/functions/plugins/issue-status.php @@ -1,9 +1,4 @@ __( 'Issue Statuses', 'eight-day-week-print-workflow' ), 'singular_name' => __( 'Issue Status', 'eight-day-week-print-workflow' ), 'search_items' => __( 'Search Issue Statuses', 'eight-day-week-print-workflow' ), @@ -76,9 +66,9 @@ function register_taxonomy() { 'parent_item_colon' => __( 'Parent Issue Status Colon', 'eight-day-week-print-workflow' ), 'no_terms' => __( 'No Issue Status', 'eight-day-week-print-workflow' ), 'back_to_items' => __( 'Back to Issue Statuses', 'eight-day-week-print-workflow' ), - ); + ]; - $args = array( + $args = [ 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, @@ -88,8 +78,8 @@ function register_taxonomy() { 'rewrite' => false, 'public' => true, 'publicly_queryable' => false, - 'capabilities' => array( 'manage_' . EDW_PRINT_ISSUE_CPT ), - ); + 'capabilities' => [ 'manage_' . EDW_PRINT_ISSUE_CPT ] + ]; \register_taxonomy( 'print_issue_status', EDW_PRINT_ISSUE_CPT, $args ); } @@ -100,19 +90,19 @@ function register_taxonomy() { function add_issue_status_filters() { global $typenow; - // Display on our custom post type only + //Display on our custom post type only if ( $typenow == EDW_PRINT_ISSUE_CPT ) { $tax_slug = 'print_issue_status'; $tax_obj = get_taxonomy( $tax_slug ); $tax_name = $tax_obj->labels->name; - // print issues are private, so hide_empty must be false - // as private posts don't count towards term count - $terms = get_terms( $tax_slug, array( 'hide_empty' => false ) ); + //print issues are private, so hide_empty must be false + //as private posts don't count towards term count + $terms = get_terms( $tax_slug, [ 'hide_empty' => false ] ); echo '

@@ -166,7 +161,7 @@ function sections_meta_box( $post, $args ) { */ function get_sections( $post_id ) { $section_ids = get_post_meta( $post_id, 'sections', true ); - // sanitize - only allow comma delimited integers + //sanitize - only allow comma delimited integers if ( ! ctype_digit( str_replace( ',', '', $section_ids ) ) ) { return ''; } @@ -185,8 +180,8 @@ function get_sections( $post_id ) { * @param $post \WP_Post The current post */ function add_section_output( $post ) { - if ( EDW_PRINT_ISSUE_CPT !== $post->post_type || - ! User\current_user_can_edit_print_issue() + if( EDW_PRINT_ISSUE_CPT !== $post->post_type || + ! User\current_user_can_edit_print_issue() ) { return; } @@ -231,7 +226,7 @@ class="button button-secondary dashicons dashicons-yes"> */ function update_print_issue_sections( $post_id, $post, $update ) { - if ( ! isset( $_POST['pi-section-ids'] ) ) { + if( ! isset( $_POST['pi-section-ids'] ) ) { return; } @@ -246,6 +241,7 @@ function update_print_issue_sections( $post_id, $post, $update ) { } set_print_issue_sections( $section_ids, $post_id ); + } /** * Saves section IDs to the DB @@ -256,21 +252,21 @@ function update_print_issue_sections( $post_id, $post, $update ) { */ function set_print_issue_sections( $section_ids, $print_issue_id ) { - // sanitize - only allow comma delimited integers + //sanitize - only allow comma delimited integers if ( ! ctype_digit( str_replace( ',', '', $section_ids ) ) ) { return; } update_post_meta( $print_issue_id, 'sections', $section_ids ); - // allow other parts to hook + //allow other parts to hook do_action( 'save_print_issue_sections', $print_issue_id, $section_ids ); + } /** * Class Section_Factory - * * @package Eight_Day_Week\Sections * * Factory that creates + updates sections @@ -288,10 +284,10 @@ class Section_Factory { */ public static function create( $name ) { - $info = array( + $info = [ 'post_title' => $name, - 'post_type' => EDW_SECTION_CPT, - ); + 'post_type' => EDW_SECTION_CPT, + ]; $section_id = wp_insert_post( $info ); if ( $section_id ) { return new Section( $section_id ); @@ -302,7 +298,7 @@ public static function create( $name ) { public static function assign_to_print_issue( $section, $print_issue ) { $current_sections = get_sections( $print_issue->ID ); - $new_sections = $current_sections ? $current_sections . ',' . $section->ID : $section->ID; + $new_sections = $current_sections ? $current_sections . ',' . $section->ID : $section->ID; set_print_issue_sections( $new_sections, $print_issue->ID ); return $new_sections; } @@ -318,7 +314,7 @@ public static function create_ajax() { $name = isset( $_POST['name'] ) ? sanitize_text_field( $_POST['name'] ) : false; if ( ! $name ) { - Core\send_json_error( array( 'message' => __( 'Please enter a section name.', 'eight-day-week-print-workflow' ) ) ); + Core\send_json_error( [ 'message' => __( 'Please enter a section name.', 'eight-day-week-print-workflow' ) ] ); } $print_issue_id = absint( $_POST['print_issue_id'] ); @@ -331,16 +327,16 @@ public static function create_ajax() { try { $section = self::create( $name ); } catch ( \Exception $e ) { - // let the whoops message run its course + //let the whoops message run its course $section = null; } if ( $section instanceof Section ) { self::assign_to_print_issue( $section, $print_issue ); - Core\send_json_success( array( 'section_id' => $section->ID ) ); + Core\send_json_success( [ 'section_id' => $section->ID ] ); } - Core\send_json_error( array( 'message' => __( 'Whoops! Something went awry.', 'eight-day-week-print-workflow' ) ) ); + Core\send_json_error( [ 'message' => __( 'Whoops! Something went awry.', 'eight-day-week-print-workflow' ) ] ); } /** @@ -354,17 +350,17 @@ public static function update_title_ajax() { $title = isset( $_POST['title'] ) ? sanitize_text_field( $_POST['title'] ) : false; if ( ! $title ) { - Core\send_json_error( array( 'message' => __( 'Please enter a section name.', 'eight-day-week-print-workflow' ) ) ); + Core\send_json_error( [ 'message' => __( 'Please enter a section name.', 'eight-day-week-print-workflow' ) ] ); } $post_id = isset( $_POST['post_id'] ) ? sanitize_text_field( $_POST['post_id'] ) : false; if ( ! $post_id ) { - Core\send_json_error( array( 'message' => __( 'Whoops! This section appears to be invalid.', 'eight-day-week-print-workflow' ) ) ); + Core\send_json_error( [ 'message' => __( 'Whoops! This section appears to be invalid.', 'eight-day-week-print-workflow' ) ] ); } try { self::update_title( $title, $post_id ); } catch ( \Exception $e ) { - Core\send_json_error( array( 'message' => $e->getMessage() ) ); + Core\send_json_error( [ 'message' => $e->getMessage() ] ); } Core\send_json_success(); } @@ -385,7 +381,6 @@ static function update_title( $title, $id ) { /** * Class Section - * * @package Eight_Day_Week\Sections * * Class that represents a section object + offers utility functions for it @@ -447,7 +442,7 @@ private function import_post_info() { foreach ( $info as $key => $value ) { if ( ! empty( $key ) ) { $this->$key = $value; - } elseif ( ! empty( $key ) && ! method_exists( $this, $key ) ) { + } else if ( ! empty( $key ) && ! method_exists( $this, $key ) ) { $this->$key = $value; } } @@ -486,10 +481,10 @@ function update_title( $title ) { throw new \Exception( __( 'Please supply a valid, non-empty title', 'eight-day-week-print-workflow' ) ); } $title = sanitize_text_field( $title ); - $args = array( + $args = [ 'ID' => $this->ID, 'post_title' => $title, - ); + ]; $result = $this->update( $args ); } } @@ -505,50 +500,48 @@ function save_metabox_order() { check_ajax_referer( 'meta-box-order' ); $order = isset( $_POST['order'] ) ? (array) $_POST['order'] : false; - if ( ! $order ) { + if( ! $order ) { return; } $page = isset( $_POST['page'] ) ? $_POST['page'] : ''; - if ( $page != sanitize_key( $page ) ) { + if ( $page != sanitize_key( $page ) ) wp_die( 0 ); - } - // only intercept PI CPT - if ( EDW_PRINT_ISSUE_CPT !== $page ) { + //only intercept PI CPT + if( EDW_PRINT_ISSUE_CPT !== $page ) { return; } - if ( ! $user = wp_get_current_user() ) { + if ( ! $user = wp_get_current_user() ) wp_die( -1 ); - } - // don't allow print prod users to re-order - if ( ! User\current_user_can_edit_print_issue() ) { + //don't allow print prod users to re-order + if( ! User\current_user_can_edit_print_issue() ) { wp_die( -1 ); } - // grab the post ID from the section template + //grab the post ID from the section template $metaboxes = explode( ',', $order['normal'] ); - $template = false; - foreach ( $metaboxes as $metabox ) { - if ( strpos( $metabox, 'template' ) !== false ) { + $template = false; + foreach( $metaboxes as $metabox ) { + if( strpos( $metabox, 'template' ) !== FALSE ) { $template = $metabox; } } - // couldnt find PI template, which contains PI post ID - if ( ! $template ) { + //couldnt find PI template, which contains PI post ID + if( ! $template ) { return; } - $parts = explode( '-', $template ); + $parts = explode( '-', $template ); $post_id = end( $parts ); $post = get_post( $post_id ); - if ( ! $post || ( $post ) && EDW_PRINT_ISSUE_CPT !== $post->post_type ) { + if( ! $post || ( $post ) && EDW_PRINT_ISSUE_CPT !== $post->post_type ) { return; } @@ -567,7 +560,7 @@ function save_metabox_order() { function get_section_order( $result ) { global $post; - if ( $post && $order = get_post_meta( $post->ID, 'section-order', true ) ) { + if( $post && $order = get_post_meta( $post->ID, 'section-order', true ) ) { return $order; } @@ -577,7 +570,7 @@ function get_section_order( $result ) { /** * Outputs a Save button */ -function section_save_button() { +function section_save_button(){ if ( Print_Issue\is_read_only_view() || ! User\current_user_can_edit_print_issue() ) { return; } diff --git a/includes/functions/taxonomies.php b/includes/functions/taxonomies.php index 2cd1661..a5189c8 100644 --- a/includes/functions/taxonomies.php +++ b/includes/functions/taxonomies.php @@ -1,9 +1,4 @@ labels->singular_name, - __NAMESPACE__ . '\create_taxonomy_dropdown_metabox', + __NAMESPACE__ . '\create_taxonomy_dropdown_metabox', EDW_PRINT_ISSUE_CPT, 'side', 'default', - array( 'taxonomy' => $taxonomy ) + [ 'taxonomy' => $taxonomy ] ); } @@ -39,19 +34,19 @@ function add_taxonomy_dropdown_meta_box( $tax_slug ) { * Displays a taxonomy dropdown box metabox * * @param \WP_Post $post The currently edited post - * @param array $metabox Contains args from the add_meta_box func. Important one: pass in a Taxonomy object. + * @param array $metabox Contains args from the add_meta_box func. Important one: pass in a Taxonomy object. */ function create_taxonomy_dropdown_metabox( $post, $metabox ) { $taxonomy = $metabox['args']['taxonomy']; - // bail if invalid taxonomy or not an object - if ( ! is_object( $taxonomy ) || ( is_object( $taxonomy ) && ! get_taxonomy( $taxonomy->name ) ) ) { + //bail if invalid taxonomy or not an object + if( !is_object( $taxonomy) || ( is_object( $taxonomy ) && ! get_taxonomy( $taxonomy->name ) ) ) { return; } // uses same noncename as default box so no save_post hook needed - wp_nonce_field( 'taxonomy_' . $taxonomy->name, 'taxonomy_noncename' ); + wp_nonce_field( 'taxonomy_'. $taxonomy->name, 'taxonomy_noncename' ); // get terms associated with this post $names = get_the_terms( get_the_ID(), $taxonomy->name ); @@ -59,15 +54,15 @@ function create_taxonomy_dropdown_metabox( $post, $metabox ) { // get all terms in this taxonomy $terms = (array) get_terms( $taxonomy->name, 'hide_empty=0' ); - if ( ! $terms ) { + if( ! $terms ) { echo '

' . sprintf( esc_html_x( 'No %s created.', 'When no terms are present, i.e. "No publications created."', 'eight-day-week-print-workflow' ), esc_html( $taxonomy->labels->name ) ) . '

'; return; } // filter the ids out of the terms - $existing = ( ! is_wp_error( $names ) && ! empty( $names ) ) + $existing = ( !is_wp_error( $names ) && !empty( $names ) ) ? (array) wp_list_pluck( $names, 'term_id' ) - : array(); + : []; // Check if taxonomy is hierarchical // Terms are saved differently between types @@ -77,38 +72,38 @@ function create_taxonomy_dropdown_metabox( $post, $metabox ) { $default_val = $h ? 0 : ''; // input name - $name = $h ? 'tax_input[' . $taxonomy->name . '][]' : 'tax_input[' . $taxonomy->name . ']'; + $name = $h ? 'tax_input['. $taxonomy->name .'][]' : 'tax_input['. $taxonomy->name .']'; $default_text = sprintf( _x( 'No %s', 'Provides a select option for choosing "no" term.', 'eight-day-week-print-workflow' ), esc_html( $taxonomy->labels->singular_name ) ); - $select = ''; + $select = ''; $selected_term = false; $select .= ''; - if ( User\current_user_can_edit_print_issue() ) { - echo wp_kses( - $select, - array( - 'option' => array( - 'value' => array(), - 'selected' => array(), - ), - 'select' => array( - 'name' => array(), - 'id' => array(), - ), - ) - ); + if( User\current_user_can_edit_print_issue() ) { + echo wp_kses( $select, [ + 'option' => [ + 'value' => [], + 'selected' => [], + ], + 'select' => [ + 'name' => [], + 'id' => [], + ] + ] ); } else { echo '

' . esc_html( $selected_term ? $selected_term : $default_text ) . '

'; } + } diff --git a/includes/functions/user-roles.php b/includes/functions/user-roles.php index 451c3df..372348e 100644 --- a/includes/functions/user-roles.php +++ b/includes/functions/user-roles.php @@ -1,10 +1,4 @@ true, - // grant all caps related to the CPT - 'edit_post' => true, - "edit_{$pt}" => true, - "read_{$pt}" => true, - "delete_{$pt}" => true, - "edit_{$pt}s" => true, - "edit_others_{$pt}s" => true, - "publish_{$pt}s" => true, - "read_private_{$pt}s" => true, - "delete_{$pt}s" => true, - "delete_private_{$pt}s" => true, + return [ + //grant dashboard access + 'read' => true, + //grant all caps related to the CPT + 'edit_post' => true, + "edit_{$pt}" => true, + "read_{$pt}" => true, + "delete_{$pt}" => true, + "edit_{$pt}s" => true, + "edit_others_{$pt}s" => true, + "publish_{$pt}s" => true, + "read_private_{$pt}s" => true, + "delete_{$pt}s" => true, + "delete_private_{$pt}s" => true, "delete_published_{$pt}s" => true, - "delete_others_{$pt}s" => true, - "edit_private_{$pt}s" => true, - "edit_published_{$pt}s" => true, - "edit_{$pt}s" => true, - // custom cap for submenus - "manage_{$pt}" => true, - ); + "delete_others_{$pt}s" => true, + "edit_private_{$pt}s" => true, + "edit_published_{$pt}s" => true, + "edit_{$pt}s" => true, + //custom cap for submenus + "manage_{$pt}" => true, + ]; } /** @@ -125,26 +119,26 @@ function add_print_production_role() { */ function get_production_caps() { $pt = EDW_PRINT_ISSUE_CPT; - return array( - // grants dashboard access + return [ + //grants dashboard access 'read' => true, - // read CPT + //read CPT "read_{$pt}" => true, - // read private CPT + //read private CPT "read_private_{$pt}s" => true, - // access post list table + //access post list table "edit_{$pt}s" => true, - // access post editor + //access post editor "edit_{$pt}" => true, - ); + ]; } /** * Assigns print capabilities to WP built in roles */ function add_caps_to_built_in_roles() { - // use array keys so things like edit_post => false - // don't come over + //use array keys so things like edit_post => false + //don't come over PP\add_role_caps( 'administrator', array_keys( get_editor_caps() ) ); } @@ -157,7 +151,7 @@ function add_caps_to_built_in_roles() { */ function editable_roles( $roles ) { global $edw_roles; - // remove print roles by key + //remove print roles by key $roles = array_diff_key( $roles, array_flip( $edw_roles ) ); return $roles; } @@ -174,12 +168,12 @@ function output_print_role_on_user_list_table( $which ) { $select_id = 'top' === $which ? 'pp-print-role' : 'pp-print-role2'; - echo ''; echo ''; echo ''; - foreach ( (array) $edw_roles as $role ) { + foreach( (array) $edw_roles as $role ) { $wp_role = get_role( $role ); - if ( ! $wp_role ) { + if( ! $wp_role ) { continue; } $name = $role_names[ $role ]; @@ -218,13 +212,13 @@ function get_role_names() { function get_user_print_roles( $user ) { global $edw_roles; $current_roles = $user->roles; - $roles = get_editable_roles(); + $roles = get_editable_roles(); - // flip to check values as if they were keys (to match WP_Roles->roles format) - // flip back to get back indexed array for later + //flip to check values as if they were keys (to match WP_Roles->roles format) + //flip back to get back indexed array for later $non_built_in = array_flip( array_diff_key( array_flip( $current_roles ), $roles ) ); - if ( ! $non_built_in || ! is_array( $non_built_in ) ) { - return array(); + if( ! $non_built_in || ! is_array( $non_built_in ) ) { + return []; } return array_intersect( $edw_roles, $non_built_in ); } @@ -242,7 +236,7 @@ function update_users_print_role() { return; } - // don't proccess the default option + //don't proccess the default option if ( -1 == $_GET['pp-print-role'] && -1 == $_GET['pp-print-role2'] ) { return; } @@ -259,7 +253,7 @@ function update_users_print_role() { $new_role = -1 != $_GET['pp-print-role'] ? $_GET['pp-print-role'] : $_GET['pp-print-role2']; - // validate requested print role + //validate requested print role if ( 'remove' !== $new_role && ! in_array( $new_role, $edw_roles ) ) { wp_die( __( 'You can’t give users that print role.', 'eight-day-week-print-workflow' ) ); } @@ -281,21 +275,22 @@ function update_users_print_role() { $user = get_userdata( $id ); - // remove all previous roles + //remove all previous roles foreach ( (array) $edw_roles as $role_to_remove ) { $user->remove_role( $role_to_remove ); } - // add new role + //add new role if ( 'remove' !== $new_role ) { $user->add_role( sanitize_text_field( $new_role ) ); } } - $redirect = remove_query_arg( array( 'pp-print-role' ) ); - $redirect = add_query_arg( array( 'update' => 'promote' ), $redirect ); + $redirect = remove_query_arg( [ 'pp-print-role' ] ); + $redirect = add_query_arg( [ 'update' => 'promote' ], $redirect ); wp_redirect( $redirect ); exit; + } /** @@ -312,24 +307,25 @@ function update_users_print_role() { function re_save_print_roles( $user_id, $new_role, $old_roles ) { global $edw_roles; - // if adding a print role, we're done here - if ( in_array( $new_role, $edw_roles ) ) { + //if adding a print role, we're done here + if( in_array( $new_role, $edw_roles ) ) { return; } $user = get_userdata( $user_id ); - // if a user has a print role already, bail - if ( array_intersect( $edw_roles, $user->roles ) ) { + //if a user has a print role already, bail + if( array_intersect( $edw_roles, $user->roles ) ) { return; } $old_print_roles = array_intersect( $edw_roles, $old_roles ); - // get the first print role + //get the first print role $old_print_role = reset( $old_print_roles ); $user->add_role( $old_print_role ); + } /** @@ -366,13 +362,14 @@ function add_print_role_column( $columns ) { * @param $user_id int The current user's ID * * @return string Modified value + * */ function add_print_role_column_text( $value, $column_name, $user_id ) { $user = get_userdata( $user_id ); if ( 'print_role' == $column_name ) { - $role = get_user_print_roles( $user ); + $role = get_user_print_roles( $user ); $role_names = get_role_names(); return $role ? $role_names[ reset( $role ) ] : ''; } diff --git a/includes/lib/zip.lib.php b/includes/lib/zip.lib.php index 10861c6..b045078 100644 --- a/includes/lib/zip.lib.php +++ b/includes/lib/zip.lib.php @@ -1,9 +1,4 @@ file * * @var boolean $doWrite */ - var $doWrite = false; + var $doWrite = false; /** * Array to store compressed data * * @var array $datasec */ - var $datasec = array(); + var $datasec = array(); /** * Central directory * * @var array $ctrl_dir */ - var $ctrl_dir = array(); + var $ctrl_dir = array(); /** * End of central directory record @@ -47,7 +42,7 @@ class ZipFile { * * @var integer $old_offset */ - var $old_offset = 0; + var $old_offset = 0; /** @@ -63,8 +58,9 @@ class ZipFile { * * @return void */ - function setDoWrite() { - $this->doWrite = true; + function setDoWrite() + { + $this -> doWrite = true; } // end of the 'setDoWrite()' method /** @@ -77,10 +73,11 @@ function setDoWrite() { * * @access private */ - function unix2DosTime( $unixtime = 0 ) { - $timearray = ( $unixtime == 0 ) ? getdate() : getdate( $unixtime ); + function unix2DosTime($unixtime = 0) + { + $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime); - if ( $timearray['year'] < 1980 ) { + if ($timearray['year'] < 1980) { $timearray['year'] = 1980; $timearray['mon'] = 1; $timearray['mday'] = 1; @@ -89,12 +86,12 @@ function unix2DosTime( $unixtime = 0 ) { $timearray['seconds'] = 0; } // end if - return ( ( $timearray['year'] - 1980 ) << 25 ) - | ( $timearray['mon'] << 21 ) - | ( $timearray['mday'] << 16 ) - | ( $timearray['hours'] << 11 ) - | ( $timearray['minutes'] << 5 ) - | ( $timearray['seconds'] >> 1 ); + return (($timearray['year'] - 1980) << 25) + | ($timearray['mon'] << 21) + | ($timearray['mday'] << 16) + | ($timearray['hours'] << 11) + | ($timearray['minutes'] << 5) + | ($timearray['seconds'] >> 1); } // end of the 'unix2DosTime()' method @@ -109,66 +106,67 @@ function unix2DosTime( $unixtime = 0 ) { * * @return void */ - function addFile( $data, $name, $time = 0 ) { - $name = str_replace( '\\', '/', $name ); + function addFile($data, $name, $time = 0) + { + $name = str_replace('\\', '/', $name); - $hexdtime = pack( 'V', $this->unix2DosTime( $time ) ); + $hexdtime = pack('V', $this->unix2DosTime($time)); - $fr = "\x50\x4b\x03\x04"; - $fr .= "\x14\x00"; // ver needed to extract - $fr .= "\x00\x00"; // gen purpose bit flag - $fr .= "\x08\x00"; // compression method - $fr .= $hexdtime; // last mod time and date + $fr = "\x50\x4b\x03\x04"; + $fr .= "\x14\x00"; // ver needed to extract + $fr .= "\x00\x00"; // gen purpose bit flag + $fr .= "\x08\x00"; // compression method + $fr .= $hexdtime; // last mod time and date // "local file header" segment - $unc_len = strlen( $data ); - $crc = crc32( $data ); - $zdata = gzcompress( $data, 0 ); - $zdata = substr( substr( $zdata, 0, strlen( $zdata ) - 4 ), 2 ); // fix crc bug - $c_len = strlen( $zdata ); - $fr .= pack( 'V', $crc ); // crc32 - $fr .= pack( 'V', $c_len ); // compressed filesize - $fr .= pack( 'V', $unc_len ); // uncompressed filesize - $fr .= pack( 'v', strlen( $name ) ); // length of filename - $fr .= pack( 'v', 0 ); // extra field length - $fr .= $name; + $unc_len = strlen($data); + $crc = crc32($data); + $zdata = gzcompress($data, 0); + $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug + $c_len = strlen($zdata); + $fr .= pack('V', $crc); // crc32 + $fr .= pack('V', $c_len); // compressed filesize + $fr .= pack('V', $unc_len); // uncompressed filesize + $fr .= pack('v', strlen($name)); // length of filename + $fr .= pack('v', 0); // extra field length + $fr .= $name; // "file data" segment $fr .= $zdata; // echo this entry on the fly, ... - if ( $this->doWrite ) { + if ($this -> doWrite) { echo $fr; } else { // ... OR add this entry to array - $this->datasec[] = $fr; + $this -> datasec[] = $fr; } // now add to central directory record - $cdrec = "\x50\x4b\x01\x02"; + $cdrec = "\x50\x4b\x01\x02"; $cdrec .= "\x00\x00"; // version made by $cdrec .= "\x14\x00"; // version needed to extract $cdrec .= "\x00\x00"; // gen purpose bit flag $cdrec .= "\x08\x00"; // compression method $cdrec .= $hexdtime; // last mod time & date - $cdrec .= pack( 'V', $crc ); // crc32 - $cdrec .= pack( 'V', $c_len ); // compressed filesize - $cdrec .= pack( 'V', $unc_len ); // uncompressed filesize - $cdrec .= pack( 'v', strlen( $name ) ); // length of filename - $cdrec .= pack( 'v', 0 ); // extra field length - $cdrec .= pack( 'v', 0 ); // file comment length - $cdrec .= pack( 'v', 0 ); // disk number start - $cdrec .= pack( 'v', 0 ); // internal file attributes - $cdrec .= pack( 'V', 32 ); // external file attributes + $cdrec .= pack('V', $crc); // crc32 + $cdrec .= pack('V', $c_len); // compressed filesize + $cdrec .= pack('V', $unc_len); // uncompressed filesize + $cdrec .= pack('v', strlen($name)); // length of filename + $cdrec .= pack('v', 0); // extra field length + $cdrec .= pack('v', 0); // file comment length + $cdrec .= pack('v', 0); // disk number start + $cdrec .= pack('v', 0); // internal file attributes + $cdrec .= pack('V', 32); // external file attributes // - 'archive' bit set - $cdrec .= pack( 'V', $this->old_offset ); // relative offset of local header - $this->old_offset += strlen( $fr ); + $cdrec .= pack('V', $this -> old_offset); // relative offset of local header + $this -> old_offset += strlen($fr); $cdrec .= $name; // optional extra field, file comment goes here // save to central directory - $this->ctrl_dir[] = $cdrec; + $this -> ctrl_dir[] = $cdrec; } // end of the 'addFile()' method @@ -179,21 +177,22 @@ function addFile( $data, $name, $time = 0 ) { * * @access public */ - function file() { - $ctrldir = implode( '', $this->ctrl_dir ); - $header = $ctrldir . - $this->eof_ctrl_dir . - pack( 'v', sizeof( $this->ctrl_dir ) ) . // total #of entries "on this disk" - pack( 'v', sizeof( $this->ctrl_dir ) ) . // total #of entries overall - pack( 'V', strlen( $ctrldir ) ) . // size of central dir - pack( 'V', $this->old_offset ) . // offset to start of central dir - "\x00\x00"; // .zip file comment length - - if ( $this->doWrite ) { // Send central directory & end ctrl dir to STDOUT + function file() + { + $ctrldir = implode('', $this -> ctrl_dir); + $header = $ctrldir . + $this -> eof_ctrl_dir . + pack('v', sizeof($this -> ctrl_dir)) . //total #of entries "on this disk" + pack('v', sizeof($this -> ctrl_dir)) . //total #of entries overall + pack('V', strlen($ctrldir)) . //size of central dir + pack('V', $this -> old_offset) . //offset to start of central dir + "\x00\x00"; //.zip file comment length + + if ($this -> doWrite) { // Send central directory & end ctrl dir to STDOUT echo $header; - return ''; // Return empty string + return ""; // Return empty string } else { // Return entire ZIP archive as string - $data = implode( '', $this->datasec ); + $data = implode('', $this -> datasec); return $data . $header; } } // end of the 'file()' method diff --git a/plugins.php b/plugins.php index c07342a..58596a9 100644 --- a/plugins.php +++ b/plugins.php @@ -1,9 +1,4 @@ $namespace ) { - if ( false !== strpos( $file_path, EDW_INC . 'functions/plugins/' ) ) { - if ( ! should_load_plugin( str_replace( '.php', '', basename( $file_path ) ) ) ) { - unset( $files[ $file_path ] ); - } + foreach ( $files as $file_path => $namespace ) { + if( FALSE !== strpos( $file_path, EDW_INC . 'functions/plugins/' ) ) { + if ( ! should_load_plugin( str_replace( '.php', '', basename( $file_path ) ) ) ) { + unset( $files[ $file_path ] ); } } - - return $files; } -); + + return $files; +} ); diff --git a/vip-compat.php b/vip-compat.php index 40a30a6..2c8b834 100644 --- a/vip-compat.php +++ b/vip-compat.php @@ -1,9 +1,4 @@ false ) ); * * @param string $role Role name - * @param array $caps Key/value array of capabilities for this role + * @param array $caps Key/value array of capabilities for this role */ function merge_role_caps( $role, $caps ) { if ( function_exists( 'wpcom_vip_merge_role_caps' ) ) { From 9fb5ccd6740c109ff554dac084ce9e705b8531a2 Mon Sep 17 00:00:00 2001 From: Ben Marshall Date: Fri, 14 Jul 2023 13:11:25 -0500 Subject: [PATCH 5/7] fix(code review): made code updates based on code review --- includes/functions/articles.php | 7 ++--- includes/functions/plugins/article-export.php | 30 ++++++++++--------- includes/functions/plugins/article-status.php | 2 +- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/includes/functions/articles.php b/includes/functions/articles.php index c8b78d8..2874847 100644 --- a/includes/functions/articles.php +++ b/includes/functions/articles.php @@ -201,8 +201,8 @@ function prepare_items() { /** * Generates content for a single row of the table * - * @param object $item The current item - * @param $level int The current item's level (parent relationship level) + * @param object $item The current item. + * @param int $level The current item's level (parent relationship level). */ public function single_row( $item, $level = 0 ) { if ( property_exists( $item, 'ID' ) ) { @@ -279,8 +279,7 @@ function _column_title( $item, $classes = '', $data = '', $primary = false ) { * @return string HTML for checkbox */ function column_cb( $item ) { - return ''; + return ''; } /** diff --git a/includes/functions/plugins/article-export.php b/includes/functions/plugins/article-export.php index 5736259..6c42073 100644 --- a/includes/functions/plugins/article-export.php +++ b/includes/functions/plugins/article-export.php @@ -346,12 +346,12 @@ function build_file_sets() { return $file_sets; } + /** - * Gets an xml export file for an article - * - * @param $article \WP_Post the current post + * Retrieve an XML file for the given article. * - * @return File The XML file for the provided post + * @param mixed $article The article to retrieve the XML file for. + * @return array An array containing the XML file. */ function get_xml_file( $article ) { $xml = $this->get_xml( $article ); @@ -455,12 +455,11 @@ function output_zip() { } /** - * Builds the file name for the zip - * Uses the print issue title & day/time + * Outputs the contents of a zip file as a download. * - * @uses get_timezone - * - * @return string The zip file name + * @param string $filename The path to the zip file. + * @throws Exception If the file cannot be opened. + * @return void */ function out_zip_file( $filename ) { header( 'Content-type: application/octet-stream' ); @@ -587,8 +586,10 @@ function get_article_content( $article ) { $content = $this->article->post_content; - if ( $post = get_post( get_post_thumbnail_id( $this->id ) ) ) { - if ( $image = $this->get_image_name( $post->ID ) ) { + $post = get_post( get_post_thumbnail_id( $this->id ) ); + if ( $post ) { + $image = $this->get_image_name( $post->ID ); + if ( $image ) { $content = $this->get_image_tag( $image[1], $post->post_excerpt ) . $content; } } @@ -617,7 +618,7 @@ function ( $matches ) { $image = $this->get_image_name( (int) $img_id ); } } - // return $image ? $this->get_image_tag($image[1], $caption) : ''; + return ''; }, $content @@ -651,7 +652,8 @@ function get_outer_elements( $article ) { $res = array( 'headline' => get_the_title( $article ) ); - if ( $featured_id = get_post_thumbnail_id( $this->id ) ) { + $featured_id = get_post_thumbnail_id( $this->id ); + if ( $featured_id ) { if ( $image = $this->get_image_name( $featured_id ) ) { $res['featured'] = $image; } @@ -693,7 +695,7 @@ function get_image_name( $attachment_id = false, $attachment_path = false ) { } /** - * Formats tag for atatchment name and caption + * Formats tag for attachment name and caption * * @param string $attachment_name Attachment name * @param string $attachment_caption Attachment caption (optional) diff --git a/includes/functions/plugins/article-status.php b/includes/functions/plugins/article-status.php index a412797..4c6b7be 100644 --- a/includes/functions/plugins/article-status.php +++ b/includes/functions/plugins/article-status.php @@ -109,7 +109,7 @@ function filter_article_columns_article_status( $columns ) { 'post_status' => __( 'Article Status', 'eight-day-week' ), ); - $title_offset = array_search( 'title', array_keys( $columns ) ); + $title_offset = array_search( 'title', array_keys( $columns ) ); if ( $title_offset ) { $end = $status + array_slice( $columns, $title_offset + 1, null ); $columns = array_slice( $columns, 0, $title_offset + 1 ) + $end; From e229035dbba808763a4ed7eb239a2157f9cef1c3 Mon Sep 17 00:00:00 2001 From: Ben Marshall Date: Fri, 14 Jul 2023 13:22:28 -0500 Subject: [PATCH 6/7] fix(code review): made code updates based on code review --- includes/functions/plugins/article-export.php | 44 +++++++++++++------ 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/includes/functions/plugins/article-export.php b/includes/functions/plugins/article-export.php index 6c42073..66ceb18 100644 --- a/includes/functions/plugins/article-export.php +++ b/includes/functions/plugins/article-export.php @@ -594,19 +594,37 @@ function get_article_content( $article ) { } } - $content = preg_replace_callback( - '/\[caption.*\[\/caption\]/Usi', - function ( $matches ) { - if ( preg_match( '/attachment_(\d+)\D/i', $matches[0], $matches2 ) ) { - $image = $this->get_image_name( (int) $matches2[1] ); - } - if ( preg_match( '/\[caption[^\]]*](.*)\[\/caption\]/Usi', strip_tags( $matches[0] ), $matches2 ) ) { - $caption = trim( $matches2[1] ); - } - return $image ? $this->get_image_tag( $image[1], $caption ) : ''; - }, - $content - ); + $dom = new \DOMDocument(); + $dom->loadHTML( $content ); + + $captions = $dom->getElementsByTagName('caption'); + + foreach ( $captions as $caption ) { + $image = null; + $caption_text = ''; + + // Assuming the attachment id is an attribute in the caption tag. + $attachment_id = $caption->getAttribute( 'attachment' ); + if ( $attachment_id ) { + $image = $this->get_image_name( (int) $attachment_id ); + } + + // Assuming the caption text is inside the caption tag. + if ( $caption->nodeValue ) { // phpcs:ignore + $caption_text = trim( $caption->nodeValue ); // phpcs:ignore + } + + if ( $image ) { + $image_tag = $this->get_image_tag( $image[1], $caption_text ); + $new_node = $dom->createDocumentFragment(); + $new_node->appendXML( $image_tag ); + $caption->parentNode->replaceChild( $new_node, $caption ); // phpcs:ignore + } else { + $caption->parentNode->removeChild( $caption ); // phpcs:ignore + } + } + + $content = $dom->saveHTML(); $content = preg_replace_callback( '/\[gallery[^]]*ids="([\d,]+)"[^]]*\]/Usi', From 4d1a76d96caeed2406339b6d1eab134ddf8888f8 Mon Sep 17 00:00:00 2001 From: Ben Marshall Date: Fri, 14 Jul 2023 13:40:21 -0500 Subject: [PATCH 7/7] fix(code review): removed unneeded code --- includes/functions/plugins/article-export.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/includes/functions/plugins/article-export.php b/includes/functions/plugins/article-export.php index 66ceb18..0a2c268 100644 --- a/includes/functions/plugins/article-export.php +++ b/includes/functions/plugins/article-export.php @@ -626,22 +626,6 @@ function get_article_content( $article ) { $content = $dom->saveHTML(); - $content = preg_replace_callback( - '/\[gallery[^]]*ids="([\d,]+)"[^]]*\]/Usi', - function ( $matches ) { - - if ( $matches[1] ) { - $gallery_images = explode( ',', $matches[1] ); - foreach ( $gallery_images as $img_id ) { - $image = $this->get_image_name( (int) $img_id ); - } - } - - return ''; - }, - $content - ); - $content = strip_shortcodes( $content ); $content = preg_replace_callback(