Skip to content

Commit

Permalink
Merge pull request #2 from bkrnetic/feature/acf-cache-handling
Browse files Browse the repository at this point in the history
Feature/acf cache handling
  • Loading branch information
Ninodevo authored Jul 28, 2021
2 parents 3b7fca2 + a2b6d36 commit 514b3dc
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 90 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
/vendor/
72 changes: 30 additions & 42 deletions src/ACFDataProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace degordian\wpHelpers;

Expand All @@ -8,24 +9,16 @@
*/
class ACFDataProvider
{
/**
*
*/
const OPTION = 'option';
/**
* @var null
*/
private static $instance = null;

/**
* @var string
*/
private $prefix = '';
private static ?ACFDataProvider $instance = null;

private string $prefix = '';

/**
* @var array
* @var array<string, mixed>
*/
private $fields = [];
private array $fields = [];

/**
* DataProvider constructor.
Expand All @@ -34,10 +27,7 @@ private function __construct()
{
}

/**
* @return ACFDataProvider|null
*/
public static function getInstance()
public static function getInstance(): ACFDataProvider
{
if (self::$instance === null) {
self::$instance = new ACFDataProvider();
Expand All @@ -46,26 +36,21 @@ public static function getInstance()
return self::$instance;
}


/**
* @param $name
* @param bool $prefixed
* @return bool|mixed|null
* @return mixed
*/
public function getOptionField($name, $prefixed = true)
public function getOptionField(string $name, bool $prefixed = true)
{
return $this->getField($name, self::OPTION, $prefixed);
}

/**
* @param $name
* @param bool $postID
* @param bool $prefixed
* @return bool|mixed|null
* @param null|string|int $postID
* @return mixed
*/
public function getField($name, $postID = false, $prefixed = true)
public function getField(string $name, $postID = null, bool $prefixed = true)
{
$postID = $postID !== false ? $postID : get_the_ID();
$postID = $postID !== null ? $postID : get_the_ID();
$key = ($prefixed ? $this->prefix : '' ) . $name;

$cacheKey = 'field_' . $key . '_' . $postID;
Expand All @@ -81,30 +66,29 @@ public function getField($name, $postID = false, $prefixed = true)
return $this->fields[$cacheKey];
}

/**
* @param string $prefix
* @return $this
*/
public function setPrefix($prefix)
public function setPrefix(string $prefix): self
{
$this->prefix = $prefix;
return $this;
}

/**
* @return $this
*/
public function clearPrefix()
public function clearPrefix(): self
{
$this->prefix = '';
return $this;
}

public function clearCache(): self
{
$this->fields = [];
return $this;
}

/**
* @param bool $postID
* @return array|bool
* @param null|string|int $postID
* @return array<string, mixed>|bool
*/
public function getFields($postID = false)
public function getFields($postID = null)
{
$postID = $postID !== false ? $postID : get_the_ID();
$key = 'fields_' . $postID;
Expand All @@ -116,13 +100,17 @@ public function getFields($postID = false)
return $this->fields[$key];
}

public function getUserField($name, $userID = null, $prefixed = true)
/**
* @param null|string|int $userID
* @return mixed
*/
public function getUserField(string $name, $userID = null, bool $prefixed = true)
{
if ($userID === null && is_single()) {
$userID = get_the_author_meta('ID');
}

if ($userID) {
if ($userID !== null) {
return $this->getField($name, 'user_' . $userID, $prefixed);
}

Expand Down
45 changes: 26 additions & 19 deletions src/AssetBundle.php
Original file line number Diff line number Diff line change
@@ -1,49 +1,56 @@
<?php
/**
* Created by PhpStorm.
* Date: 24/07/2017
* Time: 15:34
*/
declare(strict_types=1);

namespace degordian\wpHelpers;

use Exception;

class AssetBundle
{
protected static $includeBasePath = '/static/';
protected static string $includeBasePath = '/static/';

public $js = [];
public $css = [];
public array $js = [];
public array $css = [];

public $asyncCss = false;
public bool $asyncCss = false;

public function getBasePath()
public function getBasePath(): string
{
return INCLUDE_URL . self::$includeBasePath;
}

/**
* @throws Exception
*/
public static function register()
{
$bundle = new static();
$bundle->enqueueScripts();
$bundle->enqueueStyles();
}

protected function enqueueScripts()
/**
* @throws Exception
*/
protected function enqueueScripts(): void
{
foreach ($this->js as $handle => $data) {
if (isset($data['path']) === false) {
throw new \Exception('Missing path definition for ' . $handle);
throw new Exception('Missing path definition for ' . $handle);
}

$path = $data['path'];
$version = isset($data['version']) ? $data['version'] : 1.0;
$inFooter = isset($data['inFooter']) ? $data['inFooter'] : true;
$version = $data['version'] ?? 1.0;
$inFooter = $data['inFooter'] ?? true;

wp_enqueue_script($handle, $this->getBasePath() . $path, [], $version, $inFooter);
}
}

protected function enqueueStyles()
/**
* @throws Exception
*/
protected function enqueueStyles(): void
{
if ($this->asyncCss) {
add_action('wp_head', function () {
Expand Down Expand Up @@ -77,15 +84,15 @@ function loadCSS(e, n, o, t) {
} else {
foreach ($this->css as $handle => $data) {
if (isset($data['path']) === false) {
throw new \Exception('Missing path definition for ' . $handle);
throw new Exception('Missing path definition for ' . $handle);
}

$path = $data['path'];
$version = isset($data['version']) ? $data['version'] : 1.0;
$inFooter = isset($data['inFooter']) ? $data['inFooter'] : true;
$version = $data['version'] ?? 1.0;
$inFooter = $data['inFooter'] ?? true;

wp_enqueue_style($handle, $this->getBasePath() . $path, [], $version, $inFooter);
}
}
}
}
}
19 changes: 7 additions & 12 deletions src/FlexibleLayoutRenderer.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
<?php
/**
* Created by PhpStorm.
* Date: 16/05/2017
* Time: 12:29
*/
declare(strict_types=1);

namespace degordian\wpHelpers;

class FlexibleLayoutRenderer
{
private $basePath = '';
private string $basePath = '';

/**
* FlexibleLayoutRenderer constructor.
* @param string $basePath
*/
public function __construct($basePath)
public function __construct(string $basePath)
{
$this->basePath = $basePath;
}

/**
* @return void|string
*/
public function render($data)
{
$index = 0;
Expand All @@ -37,7 +32,7 @@ public function render($data)
}
}

private function getPartialPath($partialName)
private function getPartialPath(string $partialName): string
{
return $this->basePath . DIRECTORY_SEPARATOR . $partialName;
}
Expand Down
18 changes: 9 additions & 9 deletions src/SocialSharer.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
<?php
declare(strict_types=1);

namespace degordian\wpHelpers;

class SocialSharer
{

public function getFBShareLink($url)
public function getFBShareLink($url): string
{
return sprintf('https://www.facebook.com/sharer/sharer.php?u=%s', $url);
}

public function getTwitterShareLink($url)
public function getTwitterShareLink($url): string
{
return sprintf('https://twitter.com/home?status=%s', $url);
}

public function getLinkedInShareLink($url)
public function getLinkedInShareLink($url): string
{
return sprintf('https://www.linkedin.com/shareArticle?mini=true&url=%s&title=%s&summary=&source=', $url, 'Mercury Processing');
}

public function getGooglePlusShareLink($url)
public function getGooglePlusShareLink($url): string
{
return sprintf('https://plus.google.com/share?url=%s', $url);
}

public function getEmailShareLink($url)
public function getEmailShareLink($url): string
{
return sprintf('mailto:?to=&body=%s&subject=%s', $url, 'Mercury Processing');
}

public function getFBShareCount($url)
public function getFBShareCount($url): int
{
$link = sprintf('https://graph.facebook.com/?id=%s', $url);

Expand All @@ -47,7 +47,7 @@ public function getFBShareCount($url)
}
}

public function getTwitterShareCount($url)
public function getTwitterShareCount($url): int
{
$link = sprintf('http://opensharecount.com/count.json?url=%s', $url);

Expand All @@ -61,7 +61,7 @@ public function getTwitterShareCount($url)
}
}

public function getLinkedInShareCount($url)
public function getLinkedInShareCount($url): int
{
$link = sprintf('http://www.linkedin.com/countserv/count/share?url=%s&format=json', $url);

Expand Down
Loading

0 comments on commit 514b3dc

Please sign in to comment.