1414use Municipio \Helper \SiteSwitcher \SiteSwitcher ;
1515use Municipio \HooksRegistrar \HooksRegistrar ;
1616use ModularityLikePosts \Helper \CacheBust ;
17+ use WpUtilService \WpUtilService ;
1718
1819/**
1920 * Class App
@@ -28,11 +29,13 @@ public function __construct(
2829 private Blade $ bladeInstance ,
2930 private SiteSwitcher $ siteSwitcher ,
3031 private WpService $ wpService ,
32+ private WpUtilService $ wpUtilService ,
3133 private GetPosts $ getPostsHelper ,
3234 private GetOptionFields $ getOptionFieldsHelper ,
3335 private CacheBust $ cacheBust
3436 )
3537 {
38+ $ this ->setUpEnqueue ();
3639 $ this ->setUpRestEndpoints ();
3740 $ this ->setUpLikeIconCounter ();
3841 }
@@ -72,7 +75,6 @@ private function setUpLikeIconCounter(): void
7275 */
7376 public function addHooks (): void
7477 {
75- $ this ->wpService ->addAction ('wp_enqueue_scripts ' , array ($ this , 'enqueueFrontend ' ));
7678 $ this ->wpService ->addFilter ('acf/load_field/name=liked_post_types_to_show ' , array ($ this , 'setModulePostTypes ' ));
7779 $ this ->wpService ->addAction ('init ' , array ($ this , 'registerModule ' ));
7880 $ this ->wpService ->addFilter ('Municipio/Helper/Post/CallToActionItems ' , array ($ this , 'postsIcon ' ), 10 , 2 );
@@ -155,43 +157,30 @@ public function postsIcon($callToActionArray, $post)
155157 }
156158
157159 /**
158- * Enqueue required scripts
160+ * Set up enqueueing of scripts and styles.
161+ *
159162 * @return void
160163 */
161- public function enqueueFrontend ()
164+ private function setUpEnqueue (): void
162165 {
166+ $ enqueue = $ this ->wpUtilService ->enqueue (__DIR__ , 'dist ' );
163167
164- //TODO: Implement wputilservice
165- $ this ->wpService ->wpEnqueueStyle (
166- 'like-posts-css ' ,
167- MODULARITYLIKEPOSTS_URL . '/dist/ ' .
168- $ this ->cacheBust ->name ('css/like-posts.css ' )
169- );
170-
171- $ this ->wpService ->wpRegisterScript (
172- 'like-posts-js ' ,
173- MODULARITYLIKEPOSTS_URL . '/dist/ ' .
174- $ this ->cacheBust ->name ('js/like-posts.js ' )
175- );
176-
177- $ userId = $ this ->wpService ->wpGetCurrentUser ()->ID ?? 0 ;
178-
179- $ data = [
180- 'currentUser ' => $ userId ,
181- 'currentBlogId ' => $ this ->wpService ->getCurrentBlogId (),
182- 'likedPostsMeta ' => (object ) $ this ->wpService ->getUserMeta (
183- $ userId ,
184- 'likedPosts ' ,
185- true
186- ) ?? [],
187- 'tooltipUnlike ' => $ this ->getOptionFieldsHelper ->getTooltipUnlike (),
188- 'tooltipLike ' => $ this ->getOptionFieldsHelper ->getTooltipLike ()
189- ];
190-
191- $ inlineJs = 'window.likedPosts = ' . wp_json_encode ($ data ) . '; ' ;
192- $ this ->wpService ->wpAddInlineScript ('like-posts-js ' , $ inlineJs , 'before ' );
193-
194- $ this ->wpService ->wpEnqueueScript ('like-posts-js ' );
168+ $ enqueue ->on ('wp_enqueue_scripts ' , 20 )->add ('css/like-posts.css ' );
169+
170+ $ enqueue ->on ('wp_enqueue_scripts ' , 20 )
171+ ->add ('js/like-posts.js ' )
172+ ->with ()
173+ ->data ('likedPosts ' , [
174+ 'currentUser ' => fn () => $ this ->wpService ->wpGetCurrentUser ()->ID ?? 0 ,
175+ 'currentBlogId ' => fn () => $ this ->wpService ->getCurrentBlogId (),
176+ 'likedPostsMeta ' => fn () => (object ) $ this ->wpService ->getUserMeta (
177+ $ this ->wpService ->wpGetCurrentUser ()->ID ?? 0 ,
178+ 'likedPosts ' ,
179+ true
180+ ) ?? [],
181+ 'tooltipUnlike ' => fn () => $ this ->getOptionFieldsHelper ->getTooltipUnlike (),
182+ 'tooltipLike ' => fn () => $ this ->getOptionFieldsHelper ->getTooltipLike ()
183+ ]);
195184 }
196185
197186 /**
0 commit comments