Created
September 11, 2018 23:00
-
-
Save earth3300/7793e4ba3893a9895043ce0b6c873bc4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| defined('NDA') || exit('No direct access.'); | |
| /** | |
| * Lists the jpg files in the media directory in which it is placed. | |
| * | |
| * Rename to index.php for it to work automatically, when viewing the directory. | |
| * | |
| * | |
| * | |
| * @see https://carlalexander.ca/designing-class-generate-wordpress-html-content/ | |
| */ | |
| /** | |
| * List the media of a certain type in a given directory. | |
| * | |
| * No WordPress hooks inside the class for portability purposes. | |
| */ | |
| class MediaList | |
| { | |
| /** | |
| * Use the PHP glob function to list files (of a certain type). | |
| * | |
| * @example glob( SITE_MEDIA_PATH . '/*.jpg') | |
| */ | |
| public function generate( $args ) | |
| { | |
| $max = isset( $args['max'] ) ? $args['max'] : 5; | |
| $root = SITE_CDN_PATH; | |
| $pattern = "/*.jpg"; | |
| $match = $root . $args['dir'] . $pattern; | |
| $str = '<article>' . PHP_EOL; | |
| $cnt = 0; | |
| foreach ( glob( $match ) as $file ) | |
| { | |
| $cnt++; | |
| if ( $cnt > $max ) | |
| { | |
| break; | |
| } | |
| /** search, replace, subject */ | |
| $src = str_replace( SITE_PATH, '', $file ); | |
| $str .= sprintf('<a href="%s"><img src="%s" width="500" height="auto" alt="Image" class="generic" /></a>%s', SITE_URL . $src, $src, PHP_EOL ); | |
| } | |
| $str .= '</article>' . PHP_EOL; | |
| return $str; | |
| } | |
| } | |
| /** | |
| * The shortcode calls a function, which then has attributes passed to it. | |
| * That function then instantiates a class, which processes the data and | |
| * then returns it. | |
| * | |
| * @see https://carlalexander.ca/designing-class-generate-wordpress-html-content/ | |
| */ | |
| function media_list( $args ) | |
| { | |
| if ( ! is_array( $args ) || ! isset( $args['dir'] ) ) { | |
| return '<!-- Missing directory. We need this. -->'; | |
| } | |
| $media_list = new MediaList(); | |
| return $media_list -> generate( $args ); | |
| } | |
| /** | |
| * If the SITE constant is defined, we assume all other related constants are defined. | |
| * | |
| * @see `/c/config/site/cfg-structure.php` for a list of these contants and their values. | |
| */ | |
| if( defined('SITE') ) | |
| { | |
| { | |
| media_list(); | |
| } | |
| } | |
| else | |
| { | |
| exit('SITE_ configuration paramaters are required for this class to work.'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment