| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* DocumentGallery wraps core functionality involved in plugin setup. |
| 5 |
* |
| 6 |
* @author drossiter |
| 7 |
*/ |
| 8 |
class DocumentGallery { |
| 9 |
|
| 10 |
/*========================================================================== |
| 11 |
* THE SHORTCODE |
| 12 |
*=========================================================================*/ |
| 13 |
|
| 14 |
/** |
| 15 |
* Takes values passed from attributes and returns suitable HTML to represent |
| 16 |
* all valid attachments requested. |
| 17 |
* |
| 18 |
* @param array $atts Arguments from the user. |
| 19 |
* |
| 20 |
* @return string HTML for the Document Gallery. |
| 21 |
*/ |
| 22 |
public static function doShortcode( $atts ) { |
| 23 |
include_once DG_PATH . 'inc/class-gallery.php'; |
| 24 |
|
| 25 |
$start = microtime( true ); |
| 26 |
$gallery = (string) new DG_Gallery( $atts ); |
| 27 |
DG_Logger::writeLog( DG_LogLevel::Detail, 'Generation Time: ' . sprintf( '%.2f', ( microtime( true ) - $start ) ) . ' s' ); |
| 28 |
|
| 29 |
return $gallery; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Enqueue standard DG CSS. |
| 34 |
*/ |
| 35 |
public static function enqueueGalleryStyle() { |
| 36 |
DG_Util::enqueueAsset( 'document-gallery', 'assets/css/style.css' ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Enqueue script for Document Gallery frontend. |
| 41 |
*/ |
| 42 |
public static function enqueueGalleryScript() { |
| 43 |
DG_Util::enqueueAsset( 'document-gallery', 'assets/js/gallery.js', array( 'jquery' ) ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Prints user's custom CSS. |
| 48 |
*/ |
| 49 |
public static function printCustomStyle() { |
| 50 |
global $dg_options; |
| 51 |
|
| 52 |
if ( ! empty( $dg_options['css']['text'] ) ) { |
| 53 |
echo '<style type="text/css">' . $dg_options['css']['text'] . '</style>' . PHP_EOL; |
| 54 |
} |
| 55 |
|
| 56 |
// need AJAX URL variable in frontend |
| 57 |
?> |
| 58 |
<script type="text/javascript"> |
| 59 |
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>'; |
| 60 |
</script> |
| 61 |
<?php |
| 62 |
} |
| 63 |
|
| 64 |
/*========================================================================== |
| 65 |
* I18n |
| 66 |
*=========================================================================*/ |
| 67 |
|
| 68 |
/** |
| 69 |
* Loads language files into WP core. |
| 70 |
*/ |
| 71 |
public static function loadTextDomain() { |
| 72 |
load_plugin_textdomain( 'document-gallery', false, dirname( DG_BASENAME ) . '/languages/' ); |
| 73 |
} |
| 74 |
|
| 75 |
/*========================================================================== |
| 76 |
* HELPER FUNCTIONS |
| 77 |
*=========================================================================*/ |
| 78 |
|
| 79 |
/** |
| 80 |
* Accepts AJAX request containing list of IDs to be generated and returned. |
| 81 |
* Returns associative array mapping ID to thumbnail URL for all icons that were generated, |
| 82 |
* skipping any that could not be processed. |
| 83 |
*/ |
| 84 |
public static function ajaxGenerateIcons() { |
| 85 |
$ret = array(); |
| 86 |
|
| 87 |
if ( array_key_exists( 'ids', $_REQUEST ) ) { |
| 88 |
foreach ( $_REQUEST['ids'] as $id ) { |
| 89 |
// only return URL if different from default -- default image is already displayed on the client side |
| 90 |
$url = DG_Thumber::getThumbnail( $id ); |
| 91 |
if ( $url !== DG_Thumber::getDefaultThumbnail( $id ) ) { |
| 92 |
$ret[$id] = $url; |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
wp_send_json($ret); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* @param int $blog ID of the blog to be retrieved in multisite env. |
| 102 |
* |
| 103 |
* @return array Options for the blog. |
| 104 |
*/ |
| 105 |
public static function getOptions( $blog = null ) { |
| 106 |
global $dg_options; |
| 107 |
|
| 108 |
return is_null( $blog ) |
| 109 |
? $dg_options |
| 110 |
: get_blog_option( $blog, DG_OPTION_NAME, null ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* @param array $options |
| 115 |
* @param int $blog ID of the blog to be set in multisite env. |
| 116 |
*/ |
| 117 |
public static function setOptions( $options, $blog = null ) { |
| 118 |
if ( is_null( $blog ) ) { |
| 119 |
global $dg_options; |
| 120 |
update_option( DG_OPTION_NAME, $options ); |
| 121 |
$dg_options = $options; |
| 122 |
} else { |
| 123 |
update_blog_option( $blog, DG_OPTION_NAME, $options ); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* @param int $blog ID of the blog to be deleted in multisite env. |
| 129 |
*/ |
| 130 |
public static function deleteOptions( $blog = null ) { |
| 131 |
if ( is_null( $blog ) ) { |
| 132 |
delete_option( DG_OPTION_NAME ); |
| 133 |
} else { |
| 134 |
delete_blog_option( $blog, DG_OPTION_NAME ); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Adds hook to validate DG options every time save is attempted. |
| 140 |
*/ |
| 141 |
public static function addValidation() { |
| 142 |
add_filter( 'pre_update_option_' . DG_OPTION_NAME, array( |
| 143 |
__CLASS__, |
| 144 |
'validateOptionsStructure' |
| 145 |
), 10, 2 ); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Checks whether the given options match the option schema. |
| 150 |
* |
| 151 |
* @param array $new The new options to be validated. |
| 152 |
* @param array $old The old options. |
| 153 |
* |
| 154 |
* @return array The options to be saved. |
| 155 |
*/ |
| 156 |
public static function validateOptionsStructure( $new, $old ) { |
| 157 |
if ( self::isValidOptionsStructure( $new ) ) { |
| 158 |
$ret = $new; |
| 159 |
} else { |
| 160 |
$ret = $old; |
| 161 |
DG_Logger::writeLog( |
| 162 |
DG_LogLevel::Error, |
| 163 |
'Attempted to save invalid options.' . PHP_EOL . preg_replace( '/\s+/', ' ', print_r( $new, true ) ), |
| 164 |
true, |
| 165 |
true ); |
| 166 |
} |
| 167 |
|
| 168 |
return $ret; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* @param array|mixed $o The options structure to validate. |
| 173 |
* @param array $schema The schema to validate against (note that only keys matter -- non-array values are ignored). |
| 174 |
* |
| 175 |
* @return bool Whether the given options structure matches the schema. |
| 176 |
*/ |
| 177 |
public static function isValidOptionsStructure( $o, $schema = null ) { |
| 178 |
if ( is_null( $schema ) ) { |
| 179 |
$schema = DG_Setup::getDefaultOptions( true ); |
| 180 |
} |
| 181 |
|
| 182 |
// simple checks first |
| 183 |
$valid = is_array( $o ) && ( count( $schema ) === count( $o ) ); |
| 184 |
|
| 185 |
if ( $valid ) { |
| 186 |
foreach ( $schema as $sk => $sv ) { |
| 187 |
$valid = array_key_exists( $sk, $o ); |
| 188 |
if ( is_array( $sv ) && ! empty( $sv ) ) { |
| 189 |
$valid = $valid && self::isValidOptionsStructure( $o[ $sk ], $sv ); |
| 190 |
} |
| 191 |
|
| 192 |
if ( ! $valid ) { |
| 193 |
break; |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
return $valid; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Function takes a GMT timestamp and returns a date/time string in the |
| 203 |
* current timezone and WP format. |
| 204 |
* |
| 205 |
* @param int $timestamp The GMT timestamp to translate. |
| 206 |
* |
| 207 |
* @return string The local time in the WP date/time format. |
| 208 |
*/ |
| 209 |
public static function localDateTimeFromTimestamp( $timestamp ) { |
| 210 |
static $gmt_offet = null; |
| 211 |
static $wp_date_format = null; |
| 212 |
static $wp_time_format = null; |
| 213 |
if ( is_null( $gmt_offet ) ) { |
| 214 |
$gmt_offet = get_option( 'gmt_offset' ); |
| 215 |
$wp_date_format = get_option( 'date_format' ); |
| 216 |
$wp_time_format = get_option( 'time_format' ); |
| 217 |
} |
| 218 |
|
| 219 |
return '<span class="nowrap">' . date_i18n( $wp_date_format, $timestamp + $gmt_offet * 3600 ) . '</span> <span class="nowrap">' . date_i18n( $wp_time_format, $timestamp + $gmt_offet * 3600 ) . '</span>'; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Blocks instantiation. All functions are static. |
| 224 |
*/ |
| 225 |
private function __construct() { |
| 226 |
|
| 227 |
} |
| 228 |
} |