PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.2.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.2.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / hooks.php

hooks.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.2.0, at includes/hooks.php

147 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace StoreEngine;
4
5 use stdClass;
6 use StoreEngine\Classes\FrontendRequestHandler;
7 use StoreEngine\Compatibility\Elementor;
8 use StoreEngine\Hooks\Integration;
9 use StoreEngine\hooks\Kses;
10 use StoreEngine\hooks\Payment;
11 use StoreEngine\Hooks\PermissionsAndCapabilities;
12 use StoreEngine\Utils\Formatting;
13 use StoreEngine\Utils\Geolocation;
14 use StoreEngine\Utils\Helper;
15 use WP_Post;
16
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit;
19 }
20
21 class Hooks {
22
23 public static function init() {
24 PermissionsAndCapabilities::init();
25 Formatting::init_hooks();
26 Kses::init();
27 Payment::init();
28 Hooks\Price::init();
29 Integration::init();
30 Hooks\AfterPurchase::init();
31
32 // Compatibility with external plugins
33 Elementor::init();
34
35 /**
36 * Excludes order comments from generic comment queries and feeds.
37 */
38 add_filter( 'comments_clauses', [ __CLASS__, 'exclude_order_comments' ] );
39 add_filter( 'comment_feed_where', [ __CLASS__, 'exclude_order_comments_from_feed_where' ] );
40
41 FrontendRequestHandler::init();
42
43 add_filter( 'storeengine/template/get_content', [ __CLASS__, 'remove_empty_spaces' ], -10, 2 );
44 add_filter( 'do_shortcode_tag', [ __CLASS__, 'remove_empty_spaces' ], -10, 2 );
45
46 add_action( 'save_post', [ __CLASS__, 'save_post' ], 10, 2 );
47 add_action( 'delete_post', [ __CLASS__, 'delete_post' ], 10, 2 );
48
49 add_filter( 'storeengine_settings', [ __CLASS__, 'set_maxmind_database_path' ] );
50 add_filter( 'storeengine/api/settings', [ __CLASS__, 'set_maxmind_database_path' ] );
51 add_filter( 'storeengine/geolocation/geo-locate-ip', [ Geolocation::class, 'maxmind_locate_ip' ], 10, 2 );
52
53 self::handle_cache_last_changed();
54 }
55
56 /**
57 * @param stdClass $settings
58 *
59 * @return stdClass
60 */
61 public static function set_maxmind_database_path( stdClass $settings ): stdClass {
62 $settings->maxmind_db_path = Geolocation::get_maxmind_db_path();
63
64 return $settings;
65 }
66
67 /**
68 * @return void
69 * @see update_metadata()
70 * @see delete_metadata()
71 * @see add_metadata()
72 */
73 protected static function handle_cache_last_changed() {
74 $cache_groups = [
75 'payment_token' => 'storeengine_payment_tokenmeta',
76 'order_item' => 'storeengine_order_item_meta',
77 'order' => 'storeengine_orders_meta',
78 ];
79
80 foreach ( $cache_groups as $meta_type => $cache_group ) {
81 add_action( "added_{$meta_type}_meta", fn() => wp_cache_set_last_changed( $cache_group ) );
82 add_action( "updated_{$meta_type}_meta", fn() => wp_cache_set_last_changed( $cache_group ) );
83 add_action( "deleted_{$meta_type}_meta", fn() => wp_cache_set_last_changed( $cache_group ) );
84 }
85 }
86
87 /**
88 * Exclude order comments from queries and RSS.
89 *
90 * This code should exclude shop_order comments from queries. Some queries (like the recent comments widget on the dashboard) are hardcoded.
91 * and are not filtered, however, the code current_user_can( 'read_post', $comment->comment_post_ID ) should keep them safe since only admin and.
92 * shop managers can view orders anyway.
93 *
94 * The frontend view order pages get around this filter by removing the order-comment exclusion on the comments_clauses filter.
95 *
96 * @param array $clauses A compacted array of comment query clauses.
97 *
98 * @return array
99 */
100 public static function exclude_order_comments( array $clauses ): array {
101 $clauses['where'] .= ( $clauses['where'] ? ' AND ' : '' ) . " comment_type != 'order_note' ";
102
103 return $clauses;
104 }
105
106 public static function include_order_comments( array $clauses ): array {
107 $clauses['where'] .= ( $clauses['where'] ? ' AND ' : '' ) . " comment_type = 'order_note' AND comment_agent = 'StoreEngine' ";
108
109 return $clauses;
110 }
111
112 /**
113 * Exclude order comments from queries and RSS.
114 *
115 * @param string $where The WHERE clause of the query.
116 *
117 * @return string
118 */
119 public static function exclude_order_comments_from_feed_where( string $where ): string {
120 return $where . ( $where ? ' AND ' : '' ) . " comment_type != 'order_note' ";
121 }
122
123 /**
124 * Cleanup empty extra whitespaces.
125 *
126 * @param mixed|string $content
127 * @param string $tag
128 *
129 * @return mixed|string
130 */
131 public static function remove_empty_spaces( $content, string $tag ) {
132 if ( ! Helper::is_fse_theme() || ( ! str_starts_with( $tag, 'storeengine_' ) && ! str_starts_with( $tag, 'academy_' ) ) ) {
133 return $content;
134 }
135
136 return Formatting::clean_html_whitespaces( (string) $content );
137 }
138
139 public static function save_post( $post_id, WP_Post $post ) {
140 wp_cache_set( 'storeengine:get_page_by_title:' . sanitize_title( $post->post_title ), absint( $post_id ), $post->post_type );
141 }
142
143 public static function delete_post( $post_id, WP_Post $post ) {
144 wp_cache_delete( 'storeengine:get_page_by_title:' . sanitize_title( $post->post_title ), $post->post_type );
145 }
146 }
147