| 1 |
<?php |
| 2 |
|
| 3 |
namespace Photonic_Plugin\Core; |
| 4 |
|
| 5 |
use Photonic_Plugin\Layouts\Core_Layout; |
| 6 |
use Photonic_Plugin\Layouts\Grid; |
| 7 |
use Photonic_Plugin\Layouts\Slideshow; |
| 8 |
use Photonic_Plugin\Platforms\Base; |
| 9 |
use Photonic_Plugin\Platforms\Flickr; |
| 10 |
use Photonic_Plugin\Platforms\Google_Photos; |
| 11 |
use Photonic_Plugin\Platforms\Instagram; |
| 12 |
use Photonic_Plugin\Platforms\Native; |
| 13 |
use Photonic_Plugin\Platforms\SmugMug; |
| 14 |
use Photonic_Plugin\Platforms\Zenfolio; |
| 15 |
use Photonic_Plugin\Platforms\DeviantArt; |
| 16 |
|
| 17 |
class Gallery { |
| 18 |
private array $attr; |
| 19 |
/** @var Base */ |
| 20 |
private Base $module; |
| 21 |
|
| 22 |
/** @var Core_Layout */ |
| 23 |
private Core_Layout $layout; |
| 24 |
|
| 25 |
public function __construct($attr) { |
| 26 |
$this->attr = $attr; |
| 27 |
$type = $this->attr['type']; |
| 28 |
|
| 29 |
$this->set_platform($type); |
| 30 |
|
| 31 |
if ((!empty($attr['layout']) && in_array($type, ['flickr', 'smugmug', 'google', 'zenfolio', 'instagram', 'deviantart'], true) && in_array($attr['layout'], ['strip-above', 'strip-below', 'strip-right', 'no-strip'], true)) || |
| 32 |
(!empty($attr['style']) && in_array($type, ['default', 'wp'], true) && in_array($attr['style'], ['strip-above', 'strip-below', 'strip-right', 'no-strip'], true))) { |
| 33 |
require_once PHOTONIC_PATH . '/Layouts/Slideshow.php'; |
| 34 |
$this->layout = Slideshow::get_instance(); |
| 35 |
} |
| 36 |
else { |
| 37 |
require_once PHOTONIC_PATH . '/Layouts/Grid.php'; |
| 38 |
$this->layout = Grid::get_instance(); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
private function set_platform(?string $type) { |
| 43 |
if ('flickr' === $type) { |
| 44 |
require_once PHOTONIC_PATH . "/Platforms/Flickr.php"; |
| 45 |
$this->module = Flickr::get_instance(); |
| 46 |
} |
| 47 |
elseif ('smugmug' === $type || 'smug' === $type) { |
| 48 |
require_once PHOTONIC_PATH . "/Platforms/SmugMug.php"; |
| 49 |
$this->module = SmugMug::get_instance(); |
| 50 |
} |
| 51 |
elseif ('google' === $type) { |
| 52 |
require_once PHOTONIC_PATH . "/Platforms/Google_Photos.php"; |
| 53 |
$this->module = Google_Photos::get_instance(); |
| 54 |
} |
| 55 |
elseif ('instagram' === $type) { |
| 56 |
require_once PHOTONIC_PATH . "/Platforms/Instagram.php"; |
| 57 |
$this->module = Instagram::get_instance(); |
| 58 |
} |
| 59 |
elseif ('zenfolio' === $type) { |
| 60 |
require_once PHOTONIC_PATH . "/Platforms/Zenfolio.php"; |
| 61 |
$this->module = Zenfolio::get_instance(); |
| 62 |
} |
| 63 |
elseif ('deviantart' === $type) { |
| 64 |
require_once PHOTONIC_PATH . "/Platforms/DeviantArt.php"; |
| 65 |
$this->module = DeviantArt::get_instance(); |
| 66 |
} |
| 67 |
else { |
| 68 |
require_once PHOTONIC_PATH . "/Platforms/Native.php"; |
| 69 |
$this->module = Native::get_instance(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Fetch the contents of a gallery. This first invokes the <code>get_gallery_images</code> method for each platform. |
| 75 |
* Once the results are obtained, this method prints out the results. |
| 76 |
* |
| 77 |
* @return string |
| 78 |
*/ |
| 79 |
public function get_contents(): string { |
| 80 |
$this->module->increment_gallery_index(); |
| 81 |
$contents = $this->module->get_gallery_images($this->attr); |
| 82 |
|
| 83 |
$output = ''; |
| 84 |
foreach ($contents as $component) { |
| 85 |
if (method_exists($component, 'html')) { |
| 86 |
$output .= $component->html($this->module, $this->layout); |
| 87 |
} |
| 88 |
else { |
| 89 |
$output .= $component; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// Special case --> when a native gallery is called with no <code>style</code> attribute, or if <code>style='default'</code>... |
| 94 |
if (empty($output)) { |
| 95 |
return ''; |
| 96 |
} |
| 97 |
|
| 98 |
return $this->finalize_markup($output); |
| 99 |
} |
| 100 |
|
| 101 |
public function get_helper_contents(): string { |
| 102 |
return $this->module->execute_helper($this->attr); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Wraps the output of a gallery in markup tags indicating that it is a Photonic gallery. |
| 107 |
* |
| 108 |
* @param $content |
| 109 |
* @return string |
| 110 |
*/ |
| 111 |
public function finalize_markup($content): string { |
| 112 |
if ('modal' !== $this->attr['display']) { |
| 113 |
$additional_classes = ''; |
| 114 |
if (!empty($this->attr['custom_classes'])) { |
| 115 |
$additional_classes = esc_attr($this->attr['custom_classes']); |
| 116 |
} |
| 117 |
if (!empty($this->attr['alignment'])) { |
| 118 |
$additional_classes .= ' align' . esc_attr($this->attr['alignment']); |
| 119 |
} |
| 120 |
$ret = "<div class='photonic-{$this->module->provider}-stream photonic-stream $additional_classes' id='photonic-{$this->module->provider}-stream-{$this->module->gallery_index}'>\n"; |
| 121 |
} |
| 122 |
else { |
| 123 |
$popup_id = "id='photonic-{$this->module->provider}-panel-" . $this->attr['panel'] . "'"; |
| 124 |
$ret = "<div class='photonic-{$this->module->provider}-panel photonic-panel' $popup_id>\n"; |
| 125 |
} |
| 126 |
$ret .= $content . "\n"; |
| 127 |
$ret .= "</div><!-- .photonic-stream or .photonic-panel -->\n"; |
| 128 |
return $ret; |
| 129 |
} |
| 130 |
} |
| 131 |
|