| 1 |
<?php |
| 2 |
/** |
| 3 |
* Declare any actions and filters here. |
| 4 |
* In most cases you should use a service provider, but in cases where you |
| 5 |
* just need to add an action/filter and forget about it you can add it here. |
| 6 |
* |
| 7 |
* @package Depicter |
| 8 |
*/ |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
// phpcs:ignore |
| 15 |
// add_action( 'some_action', 'some_function' ); |
| 16 |
function depicter_add_thumbnail_size() { |
| 17 |
add_image_size( 'depicter-thumbnail', 200, 9999, false ); |
| 18 |
} |
| 19 |
add_action( 'after_setup_theme', 'depicter_add_thumbnail_size' ); |
| 20 |
|
| 21 |
|
| 22 |
/** |
| 23 |
* Make assets and cache for slider after publishing the changes |
| 24 |
* |
| 25 |
* @param $documentID |
| 26 |
* @param $properties |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
function depicter_make_document_cache( $documentID, $properties ) { |
| 31 |
if ( $properties['status'] === 'publish' ) { |
| 32 |
\Depicter::front()->render()->flushDocumentCache( $documentID ); |
| 33 |
\Depicter::front()->render()->document( $documentID, [ 'echo' => false ] ); |
| 34 |
} |
| 35 |
} |
| 36 |
add_action( 'depicter/editor/after/store', 'depicter_make_document_cache', 10, 2 ); |
| 37 |
|
| 38 |
|
| 39 |
/** |
| 40 |
* Remove document cache after document was deleted |
| 41 |
* |
| 42 |
* @param $documentID |
| 43 |
* |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
function depicter_purge_document_cache( $documentID ) { |
| 47 |
\Depicter::front()->render()->flushDocumentCache( $documentID ); |
| 48 |
} |
| 49 |
add_action( 'depicter/editor/after/delete', 'depicter_purge_document_cache', 10 ); |
| 50 |
|
| 51 |
|
| 52 |
/** |
| 53 |
* Depicter sanitize html tags for depicter slider output |
| 54 |
* |
| 55 |
* @param array $allowed_tags |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
function depicter_sanitize_html_tags_for_output( $allowed_tags ) { |
| 59 |
return array_merge_recursive( $allowed_tags, wp_kses_allowed_html( 'post' ) ); |
| 60 |
} |
| 61 |
add_filter( 'averta/wordpress/sanitize/html/tags/depicter/output', 'depicter_sanitize_html_tags_for_output' ); |
| 62 |
|
| 63 |
|
| 64 |
function depicter_disable_nocache_headers( $headers ) { |
| 65 |
unset( $headers['Expires'] ); |
| 66 |
unset( $headers['Cache-Control'] ); |
| 67 |
return $headers; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
/** |
| 72 |
* Set Svg Meta Data |
| 73 |
* |
| 74 |
* Adds dimensions metadata to uploaded SVG files, since WordPress doesn't do it. |
| 75 |
* |
| 76 |
* @param array $data |
| 77 |
* @param int $id |
| 78 |
* @return array $data |
| 79 |
*/ |
| 80 |
function depicter_set_svg_meta_data( $data, $id ) { |
| 81 |
// If the attachment is an svg |
| 82 |
if ( 'image/svg+xml' === get_post_mime_type( $id ) ) { |
| 83 |
// If the svg metadata are empty or the width is empty or the height is empty. |
| 84 |
// then get the attributes from xml. |
| 85 |
if ( empty( $data ) || empty( $data['width'] ) || empty( $data['height'] ) ) { |
| 86 |
$attachment = get_the_guid( $id ); |
| 87 |
$xml = simplexml_load_file( $attachment ); |
| 88 |
|
| 89 |
if ( ! empty( $xml ) ) { |
| 90 |
$attr = $xml->attributes(); |
| 91 |
$view_box = explode( ' ', $attr->viewBox );// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 92 |
$data['width'] = isset( $attr->width ) && preg_match( '/\d+/', $attr->width, $value ) ? (int) $value[0] : ( 4 === count( $view_box ) ? (int) $view_box[2] : null ); |
| 93 |
$data['height'] = isset( $attr->height ) && preg_match( '/\d+/', $attr->height, $value ) ? (int) $value[0] : ( 4 === count( $view_box ) ? (int) $view_box[3] : null ); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
return $data; |
| 99 |
} |
| 100 |
add_filter( 'wp_update_attachment_metadata', 'depicter_set_svg_meta_data', 10, 2 ); |
| 101 |
|
| 102 |
function depicter_clear_cache() { |
| 103 |
\Depicter::cache('document')->clear(); |
| 104 |
} |
| 105 |
|
| 106 |
function depicter_clear_cache_by_cache_enabler() { |
| 107 |
$cacheEnabled = !empty( $_GET['_cache'] ) && $_GET['_cache'] == 'cache-enabler' ? true : false; |
| 108 |
$isClearAction = !empty( $_GET['_action'] ) && ( $_GET['_action'] == 'clear' || $_GET['_action'] == 'clearurl' ); |
| 109 |
if ( $cacheEnabled && $isClearAction && !empty( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'cache_enabler_clear_cache_nonce' ) ) { |
| 110 |
depicter_clear_cache(); |
| 111 |
} |
| 112 |
} |
| 113 |
add_action( 'init', 'depicter_clear_cache_by_cache_enabler' ); |
| 114 |
add_action( 'post_updated', 'depicter_clear_cache' ); |
| 115 |
|
| 116 |
add_filter( 'style_loader_tag', 'depicter_add_preload_to_styles', 10, 2 ); |
| 117 |
function depicter_add_preload_to_styles( $html, $handle ){ |
| 118 |
if (strpos( $handle, 'depicter--') === 0) { |
| 119 |
$html = str_replace("rel='stylesheet'", 'rel="preload" as="style" onload="this.rel=\'stylesheet\';this.onload=null"', $html); |
| 120 |
} |
| 121 |
return $html; |
| 122 |
} |
| 123 |
|
| 124 |
function depicter_add_defer_to_scripts( $tag, $handle ) { |
| 125 |
if ( strpos( $handle, 'depicter--') === 0 ) { |
| 126 |
$tag = str_replace(' src=', ' defer src=', $tag ); |
| 127 |
} |
| 128 |
return $tag; |
| 129 |
} |
| 130 |
add_filter( 'script_loader_tag', 'depicter_add_defer_to_scripts', 15, 2 ); |
| 131 |
|
| 132 |
/** |
| 133 |
* Check if imported media deleted then remove its ID from imported assets dictionary |
| 134 |
* |
| 135 |
* @param int $attachment_id |
| 136 |
* @return void |
| 137 |
*/ |
| 138 |
function depicter_check_deleted_imported_media( $attachment_id ) { |
| 139 |
\Depicter::media()->maybeRemoveAttachmentFromDictionary( $attachment_id ); |
| 140 |
} |
| 141 |
add_action( 'delete_attachment', 'depicter_check_deleted_imported_media'); |
| 142 |
|
| 143 |
function depicter_check_activation() { |
| 144 |
if ( isset( $_GET['depicter_upgraded'] ) || ( isset( $_GET['page'] ) && $_GET['page'] == 'depicter-dashboard' ) || ( isset( $_GET['action'] ) && $_GET['action'] == 'depicter' ) ) { |
| 145 |
\Depicter::client()->validateActivation(); |
| 146 |
} |
| 147 |
} |
| 148 |
add_action( 'admin_init', 'depicter_check_activation' ); |
| 149 |
|
| 150 |
|