| 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 |
function depicter_add_thumbnail_size() { |
| 15 |
add_image_size( 'depicter-thumbnail', 200, 9999, false ); |
| 16 |
} |
| 17 |
add_action( 'after_setup_theme', 'depicter_add_thumbnail_size' ); |
| 18 |
|
| 19 |
|
| 20 |
/** |
| 21 |
* Make assets and cache for slider after publishing the changes |
| 22 |
* |
| 23 |
* @param $documentID |
| 24 |
* @param $properties |
| 25 |
* |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
function depicter_make_document_cache( $documentID, $properties ) { |
| 29 |
if ( $properties['status'] === 'publish' ) { |
| 30 |
\Depicter::front()->render()->flushDocumentCache( $documentID ); |
| 31 |
\Depicter::front()->render()->document( $documentID, [ 'echo' => false ] ); |
| 32 |
} |
| 33 |
} |
| 34 |
add_action( 'depicter/editor/after/store', 'depicter_make_document_cache', 10, 2 ); |
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* Remove document cache after document was deleted |
| 39 |
* |
| 40 |
* @param $documentID |
| 41 |
* |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
function depicter_purge_document_cache( $documentID ) { |
| 45 |
\Depicter::front()->render()->flushDocumentCache( $documentID ); |
| 46 |
} |
| 47 |
add_action( 'depicter/editor/after/delete', 'depicter_purge_document_cache', 10 ); |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* Flushes conditional documents cache |
| 53 |
* |
| 54 |
* @param int $documentID |
| 55 |
* |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
function depicter_purge_conditional_documents_cache( $documentID = 0 ) { |
| 59 |
\Depicter::document()->getConditionalDocumentIDs(true); |
| 60 |
} |
| 61 |
add_action( 'depicter/dashboard/after/delete', 'depicter_purge_conditional_documents_cache', 10 ); |
| 62 |
add_action( 'depicter/editor/after/store' , 'depicter_purge_conditional_documents_cache', 10 ); |
| 63 |
add_action( 'depicter/rules/after/store' , 'depicter_purge_conditional_documents_cache', 10 ); |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* Depicter sanitize html tags for depicter slider output |
| 68 |
* |
| 69 |
* @param array $allowed_tags |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
function depicter_sanitize_html_tags_for_output( $allowed_tags ) { |
| 73 |
return array_merge_recursive( $allowed_tags, wp_kses_allowed_html( 'post' ) ); |
| 74 |
} |
| 75 |
add_filter( 'averta/wordpress/sanitize/html/tags/depicter/output', 'depicter_sanitize_html_tags_for_output' ); |
| 76 |
|
| 77 |
|
| 78 |
function depicter_disable_nocache_headers( $headers ) { |
| 79 |
unset( $headers['Expires'] ); |
| 80 |
unset( $headers['Cache-Control'] ); |
| 81 |
return $headers; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* Set Svg Meta Data |
| 87 |
* |
| 88 |
* Adds dimensions metadata to uploaded SVG files, since WordPress doesn't do it. |
| 89 |
* |
| 90 |
* @param array $data |
| 91 |
* @param int $id |
| 92 |
* @return array $data |
| 93 |
*/ |
| 94 |
function depicter_set_svg_meta_data( $data, $id ) { |
| 95 |
// If the attachment is an svg |
| 96 |
if ( 'image/svg+xml' === get_post_mime_type( $id ) ) { |
| 97 |
// If the svg metadata are empty or the width is empty or the height is empty. |
| 98 |
// then get the attributes from xml. |
| 99 |
if ( empty( $data ) || empty( $data['width'] ) || empty( $data['height'] ) ) { |
| 100 |
$attachment = get_the_guid( $id ); |
| 101 |
$xml = simplexml_load_file( $attachment ); |
| 102 |
|
| 103 |
if ( ! empty( $xml ) ) { |
| 104 |
$attr = $xml->attributes(); |
| 105 |
$view_box = explode( ' ', $attr->viewBox );// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 106 |
$data['width'] = isset( $attr->width ) && preg_match( '/\d+/', $attr->width, $value ) ? (int) $value[0] : ( 4 === count( $view_box ) ? (int) $view_box[2] : null ); |
| 107 |
$data['height'] = isset( $attr->height ) && preg_match( '/\d+/', $attr->height, $value ) ? (int) $value[0] : ( 4 === count( $view_box ) ? (int) $view_box[3] : null ); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return $data; |
| 113 |
} |
| 114 |
add_filter( 'wp_update_attachment_metadata', 'depicter_set_svg_meta_data', 10, 2 ); |
| 115 |
|
| 116 |
function depicter_clear_cache() { |
| 117 |
\Depicter::cache('document')->clear(); |
| 118 |
} |
| 119 |
|
| 120 |
function depicter_clear_cache_by_cache_enabler() { |
| 121 |
$cacheEnabled = !empty( $_GET['_cache'] ) && $_GET['_cache'] == 'cache-enabler' ? true : false; |
| 122 |
$isClearAction = !empty( $_GET['_action'] ) && ( $_GET['_action'] == 'clear' || $_GET['_action'] == 'clearurl' ); |
| 123 |
if ( $cacheEnabled && $isClearAction && !empty( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'cache_enabler_clear_cache_nonce' ) ) { |
| 124 |
depicter_clear_cache(); |
| 125 |
} |
| 126 |
} |
| 127 |
add_action( 'init', 'depicter_clear_cache_by_cache_enabler' ); |
| 128 |
add_action( 'post_updated', 'depicter_clear_cache' ); |
| 129 |
|
| 130 |
add_filter( 'style_loader_tag', 'depicter_add_preload_to_styles', 10, 2 ); |
| 131 |
function depicter_add_preload_to_styles( $html, $handle ){ |
| 132 |
// skip if resource preloading option was disabled |
| 133 |
if( \Depicter::options()->get('resource_preloading' ) === 'off' ){ |
| 134 |
return $html; |
| 135 |
} |
| 136 |
if( strpos( $handle, 'depicter--') === 0 ){ |
| 137 |
$html = str_replace("rel='stylesheet'", 'rel="preload" as="style" onload="this.rel=\'stylesheet\';this.onload=null"', $html); |
| 138 |
} |
| 139 |
return $html; |
| 140 |
} |
| 141 |
|
| 142 |
function depicter_add_defer_to_scripts( $tag, $handle ) { |
| 143 |
if( strpos( $handle, 'depicter--') === 0 ){ |
| 144 |
$tag = str_replace(' src=', ' defer src=', $tag ); |
| 145 |
} |
| 146 |
return $tag; |
| 147 |
} |
| 148 |
add_filter( 'script_loader_tag', 'depicter_add_defer_to_scripts', 15, 2 ); |
| 149 |
|
| 150 |
/** |
| 151 |
* Check if imported media deleted then remove its ID from imported assets dictionary |
| 152 |
* |
| 153 |
* @param int $attachment_id |
| 154 |
* @return void |
| 155 |
*/ |
| 156 |
function depicter_check_deleted_imported_media( $attachment_id ) { |
| 157 |
\Depicter::media()->maybeRemoveAttachmentFromDictionary( $attachment_id ); |
| 158 |
} |
| 159 |
add_action( 'delete_attachment', 'depicter_check_deleted_imported_media'); |
| 160 |
|
| 161 |
function depicter_check_activation() { |
| 162 |
|
| 163 |
if ( isset( $_GET['depicter_upgraded'] ) ) { |
| 164 |
if ( \Depicter::client()->validateActivation() ) { |
| 165 |
\Depicter::client()->getAccessToken( true ); |
| 166 |
} |
| 167 |
} elseif ( ( isset( $_GET['page'] ) && $_GET['page'] == 'depicter-dashboard' ) || ( isset( $_GET['action'] ) && $_GET['action'] == 'depicter' ) ) { |
| 168 |
if ( \Depicter::client()->validateActivation() ) { |
| 169 |
\Depicter::client()->getRefreshToken( true ); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
if ( isset( $_GET['depicter_flush_tokens'] ) && user_can( wp_get_current_user(), 'manage_options') ) { |
| 174 |
\Depicter::client()->getRefreshToken( true ); |
| 175 |
\Depicter::client()->getAccessToken( true ); |
| 176 |
} |
| 177 |
} |
| 178 |
add_action( 'admin_init', 'depicter_check_activation' ); |
| 179 |
|
| 180 |
/** |
| 181 |
* Renew expired tokens |
| 182 |
* |
| 183 |
* @return void |
| 184 |
*/ |
| 185 |
function depicter_renew_tokens() { |
| 186 |
if ( false === \Depicter::cache('base')->get( 'access_token' ) ) { |
| 187 |
\Depicter\Services\UserAPIService::renewTokens(); |
| 188 |
} |
| 189 |
} |
| 190 |
add_action( 'admin_init', 'depicter_renew_tokens' ); |
| 191 |
|
| 192 |
/** |
| 193 |
* disable admin bar |
| 194 |
* |
| 195 |
* @return void |
| 196 |
*/ |
| 197 |
function depicter_disable_admin_bar() { |
| 198 |
if ( isset( $_GET['dp-wp-admin-bar'] ) && $_GET['dp-wp-admin-bar'] == 0 ) { |
| 199 |
add_filter( 'show_admin_bar', '__return_false' ); |
| 200 |
} |
| 201 |
} |
| 202 |
add_action( 'init', 'depicter_disable_admin_bar'); |
| 203 |
|
| 204 |
|
| 205 |
add_action( 'admin_notices', 'depicter_renew_subscription_notice'); |
| 206 |
function depicter_renew_subscription_notice() { |
| 207 |
if ( ! \Depicter::auth()->isSubscriptionExpired() ) { |
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 211 |
$subscription_id = \Depicter::options()->get('subscription_id' , ''); |
| 212 |
echo Depicter::view('admin/notices/renew-subscription-notice')->with( 'view_args', [ |
| 213 |
'subscription_id' => $subscription_id |
| 214 |
])->toString(); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
/** |
| 219 |
* Flush all documents markup |
| 220 |
* |
| 221 |
* @return void |
| 222 |
*/ |
| 223 |
function depicter_flush_documents_cache() { |
| 224 |
|
| 225 |
try{ |
| 226 |
$documents = \Depicter::documentRepository()->select( [ 'id' ] )->findAll()->get(); |
| 227 |
if ( $documents ) { |
| 228 |
$documents = $documents->toArray(); |
| 229 |
foreach( $documents as $document ) { |
| 230 |
\Depicter::front()->render()->flushDocumentCache( $document['id'] ); |
| 231 |
} |
| 232 |
} |
| 233 |
} catch( \Exception $e ){ |
| 234 |
} |
| 235 |
} |
| 236 |
add_action( 'depicter/plugin/updated', 'depicter_flush_documents_cache' ); |
| 237 |
|