33namespace ModularityLikePosts ;
44
55use ModularityLikePosts \Helper \GetOptionFields ;
6+ use WpService \WpService ;
67
7- class LikeIconCounter {
8- private bool $ hasLikeModule = false ;
9- private string $ likeIcon = 'favorite ' ;
10- private array $ likedPostsPageIds = [];
11-
8+ class LikeIconCounter implements \Municipio \HooksRegistrar \Hookable {
129 /**
1310 * Constructor method.
1411 *
1512 * Initializes the LikeIconCounter object and sets up the save_post action hook if the like_counter option is not empty.
1613 */
17- public function __construct (private GetOptionFields $ getOptionFieldsHelper ) {
18- add_action ('init ' , array ($ this , 'init ' ));
14+ public function __construct (
15+ private GetOptionFields $ getOptionFieldsHelper ,
16+ private WpService $ wpService
17+ ) {}
18+
19+ /**
20+ * Adds hooks for the LikeIconCounter functionality.
21+ *
22+ * This method registers the init action hook to initialize the like icon counter functionality.
23+ */
24+ public function addHooks (): void {
25+ $ this ->wpService ->addAction ('init ' , [$ this , 'init ' ]);
1926 }
2027
28+ /**
29+ * Initializes the like icon counter functionality.
30+ *
31+ * This method checks if the like counter option is enabled and, if so, registers the save_post action hook
32+ * and the Municipio/Navigation/Item filter hook to handle updating the like counter and adding menu item icons.
33+ */
2134 public function init () {
22- $ useMenuCounter = $ this ->getOptionFieldsHelper ->getCounter ();
23-
24- if (!empty ($ useMenuCounter )) {
25- $ this ->likeIcon = $ this ->getOptionFieldsHelper ->getIcon ();
26- $ this ->likedPostsPageIds = get_option ('liked_posts_page_ids ' , []);
27-
28- add_action ('save_post ' , array ($ this , 'checkForLikedModule ' ), 10 , 2 );
29- add_filter ('Municipio/Navigation/Item ' , array ($ this , 'addMenuItemIcon ' ), 10 , 2 );
35+ if (!empty ($ this ->getOptionFieldsHelper ->getCounter ())) {
36+ $ this ->wpService ->addAction ('save_post ' , [$ this , 'checkForLikedModule ' ], 10 , 2 );
37+ $ this ->wpService ->addFilter ('Municipio/Navigation/Item ' , [$ this , 'addMenuItemIcon ' ], 10 , 2 );
3038 }
3139 }
3240
@@ -42,11 +50,11 @@ public function addMenuItemIcon($menuItem, $menuId) {
4250 return $ menuItem ;
4351 }
4452
45- $ pageId = ! empty ( $ menuItem [ ' page_id ' ]) ? $ menuItem [ ' page_id ' ] : $ menuItem [ ' id ' ] ;
53+ $ likedPostsPageIds = $ this -> getOptionFieldsHelper -> getLikedPostsPageIds () ;
4654
47- if (isset ($ this -> likedPostsPageIds [$ pageId ])) {
55+ if (isset ($ likedPostsPageIds [$ menuItem [ ' id ' ] ?? 0 ])) {
4856 $ menuItem ['icon ' ] = [
49- 'icon ' => $ this ->likeIcon ,
57+ 'icon ' => $ this ->getOptionFieldsHelper -> getIcon () ,
5058 'size ' => 'md ' ,
5159 'filled ' => true ,
5260 'attributeList ' => [
@@ -73,47 +81,65 @@ public function checkForLikedModule($postId, $post) {
7381 $ this ->updateLikeCounterPageIdOption ($ postId );
7482 }
7583
76- private function checkifLikedPostsBlockExistsOnPage ($ post )
77- {
78- if (!empty ($ post ->post_content ) && !$ this ->hasLikeModule ) {
79- if (str_contains ($ post ->post_content , 'wp:acf/liked-posts ' )) {
80- $ this ->hasLikeModule = true ;
81- }
82- }
83- }
84-
8584 /**
86- * Checks if the Liked Posts module exists on the page for a given post ID .
85+ * Checks if the liked posts block exists in the content of the given post.
8786 *
88- * @param int $postId The ID of the post to check.
87+ * @param WP_Post $post The post object to check.
8988 * @return void
9089 */
91- private function checkIfLikedPostsModuleExistsOnPage ( $ postId )
90+ private function checkifLikedPostsBlockExistsOnPage ( null | object $ post )
9291 {
93- $ sidebarsArray = get_post_meta ($ postId , 'modularity-modules ' , false );
92+ if (empty ($ post ) || !property_exists ($ post , 'ID ' )) {
93+ return false ;
94+ }
9495
95- $ this ->lookForLikedPostsModule ($ sidebarsArray );
96+ static $ hasLikeModule = [];
97+ if (!empty ($ post ->post_content ) && empty ($ hasLikeModule [$ post ->ID ])) {
98+ if (str_contains ($ post ->post_content , 'wp:acf/liked-posts ' )) {
99+ $ hasLikeModule [$ post ->ID ] = true ;
100+ }
101+ }
96102 }
97103
98104 /**
99105 * Recursively looks for a liked posts module in the given array.
100106 *
101107 * @param array $array The array to search for the liked posts module.
108+ * @return bool True if the liked posts module is found, false otherwise.
102109 */
103- private function lookForLikedPostsModule ($ array ) {
104- if (empty ($ array ) || !is_array ($ array ) || $ this -> hasLikeModule ) {
105- return ;
110+ private function lookForLikedPostsModule ($ array ): bool {
111+ if (empty ($ array ) || !is_array ($ array )) {
112+ return false ;
106113 }
107114
108115 if (isset ($ array ['name ' ]) && $ array ['name ' ] === 'mod-liked-posts ' ) {
109- $ this ->hasLikeModule = true ;
110- return ;
116+ return true ;
111117 }
118+
112119 foreach ($ array as $ value ) {
113- if (is_array ($ value )) {
114- $ this -> lookForLikedPostsModule ( $ value ) ;
120+ if (is_array ($ value ) && $ this -> lookForLikedPostsModule ( $ value ) ) {
121+ return true ;
115122 }
116123 }
124+ return false ;
125+ }
126+
127+ /**
128+ * Checks if the Liked Posts module exists on the page for a given post ID.
129+ *
130+ * @param int $postId The ID of the post to check.
131+ * @return bool True if the liked posts module exists, false otherwise.
132+ */
133+ private function checkIfLikedPostsModuleExistsOnPage ($ postId ): bool {
134+ $ sidebarsArray = $ this ->wpService ->getPostMeta (
135+ $ postId ,
136+ 'modularity-modules ' ,
137+ false
138+ );
139+
140+ return $ this ->lookForLikedPostsModule (
141+ is_array ($ sidebarsArray ) && !empty ($ sidebarsArray ) ? $ sidebarsArray : []
142+ );
117143 }
118144
119145 /**
@@ -123,14 +149,18 @@ private function lookForLikedPostsModule($array) {
123149 */
124150 private function updateLikeCounterPageIdOption ($ postId )
125151 {
126- $ likedPostsPageIds = $ this ->likedPostsPageIds ;
127- if ($ this ->hasLikeModule ) {
152+ $ likedPostsPageIds = $ this ->getOptionFieldsHelper ->getLikedPostsPageIds ();
153+ $ hasLikeModule = $ this ->checkIfLikedPostsModuleExistsOnPage ($ postId );
154+
155+ if ($ hasLikeModule ) {
128156 $ likedPostsPageIds [$ postId ] = $ postId ;
129157 } else {
130158 unset($ likedPostsPageIds [$ postId ]);
131159 }
132160
133- $ this ->likedPostsPageIds = $ likedPostsPageIds ;
134- update_option ('liked_posts_page_ids ' , $ likedPostsPageIds );
161+ $ this ->wpService ->updateOption (
162+ 'liked_posts_page_ids ' ,
163+ $ likedPostsPageIds
164+ );
135165 }
136166}
0 commit comments