Skip to content

Commit 681b3c1

Browse files
author
Sebastian Thulin
committed
fix: implement util service.
1 parent c8e2455 commit 681b3c1

File tree

2 files changed

+27
-34
lines changed

2 files changed

+27
-34
lines changed

like-posts.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,16 @@
8686
$getOptionFieldsHelper,
8787
$siteSwitcher
8888
);
89+
$wpUtilService = new WpUtilService\WpUtilService(
90+
$wpService
91+
);
8992

9093
//Init
9194
(new ModularityLikePosts\App(
9295
$bladeInstance,
9396
$siteSwitcher,
9497
$wpService,
98+
$wpUtilService,
9599
$getPostsHelper,
96100
$getOptionFieldsHelper,
97101
$cacheBust

source/php/App.php

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Municipio\Helper\SiteSwitcher\SiteSwitcher;
1515
use Municipio\HooksRegistrar\HooksRegistrar;
1616
use 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

Comments
 (0)