flexible-shipping
/
vendor_prefixed
/
wpdesk
/
wp-pointer
/
src
/
WPDesk
/
Pointer
/
PointerMessage.php
views
2 weeks ago
PointerConditions.php
2 weeks ago
PointerMessage.php
2 weeks ago
PointerPosition.php
2 weeks ago
PointersScripts.php
2 weeks ago
PointerMessage.php
288 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FSVendor\WPDesk\Pointer; |
| 4 | |
| 5 | /** |
| 6 | * WordPress admin pointer message. |
| 7 | * |
| 8 | * @package WPDesk\Pointer |
| 9 | */ |
| 10 | class PointerMessage |
| 11 | { |
| 12 | const USER_META_DISMISSED_WP_POINTERS = 'dismissed_wp_pointers'; |
| 13 | /** |
| 14 | * Is action added? |
| 15 | * @var bool |
| 16 | */ |
| 17 | private $actionAdded = \false; |
| 18 | /** |
| 19 | * @var string |
| 20 | */ |
| 21 | private $id; |
| 22 | /** |
| 23 | * @var string |
| 24 | */ |
| 25 | private $anchor; |
| 26 | /** |
| 27 | * @var string |
| 28 | */ |
| 29 | private $content; |
| 30 | /** |
| 31 | * @var string |
| 32 | */ |
| 33 | private $title; |
| 34 | /** |
| 35 | * @var PointerPosition |
| 36 | */ |
| 37 | private $position; |
| 38 | /** |
| 39 | * @var string|array |
| 40 | */ |
| 41 | private $pointerClass; |
| 42 | /** |
| 43 | * @var int |
| 44 | */ |
| 45 | private $pointerWidth; |
| 46 | /** |
| 47 | * @var PointerConditions |
| 48 | */ |
| 49 | private $conditions; |
| 50 | /** |
| 51 | * @var array |
| 52 | */ |
| 53 | private $pointerCss = array(); |
| 54 | /** |
| 55 | * @var array |
| 56 | */ |
| 57 | private $defaultPointerCss = array('top' => '50%', 'left' => '100%', '-webkit-transform' => 'translateY(-50%)', '-ms-transform' => 'translateY(-50%)', 'transform' => 'translateY(-50%)'); |
| 58 | /** |
| 59 | * PointerMessage constructor. |
| 60 | * |
| 61 | * @param string $id |
| 62 | * @param string $anchor |
| 63 | * @param string $title |
| 64 | * @param string $content |
| 65 | * @param PointerPosition $position |
| 66 | * @param string pointerClass |
| 67 | * @param int $pointerWidth |
| 68 | * @param null|PointerConditions $conditions Pointer conditions. |
| 69 | * @param array $pointerCss Pointer CSS. |
| 70 | */ |
| 71 | public function __construct($id, $anchor, $title, $content, $position = null, $pointerClass = 'wp-pointer', $pointerWidth = 320, $conditions = null, $pointerCss = array()) |
| 72 | { |
| 73 | $this->id = $id; |
| 74 | $this->anchor = $anchor; |
| 75 | $this->title = $title; |
| 76 | $this->content = $content; |
| 77 | if ($position === null) { |
| 78 | $position = new PointerPosition(); |
| 79 | } |
| 80 | $this->position = $position; |
| 81 | $this->pointerClass = $pointerClass; |
| 82 | $this->pointerWidth = $pointerWidth; |
| 83 | if (null === $conditions) { |
| 84 | $this->conditions = new PointerConditions(); |
| 85 | } else { |
| 86 | $this->conditions = $conditions; |
| 87 | } |
| 88 | $this->pointerCss = $pointerCss; |
| 89 | $this->addAction(); |
| 90 | } |
| 91 | /** |
| 92 | * Enqueue scripts. |
| 93 | */ |
| 94 | public function enqueueScripts() |
| 95 | { |
| 96 | wp_enqueue_style('wp-pointer'); |
| 97 | wp_enqueue_script('wp-pointer'); |
| 98 | } |
| 99 | /** |
| 100 | * Add notice action. |
| 101 | */ |
| 102 | protected function addAction() |
| 103 | { |
| 104 | if (!$this->actionAdded) { |
| 105 | add_action('admin_print_footer_scripts', array($this, 'maybeRenderJavascript')); |
| 106 | add_action('admin_enqueue_scripts', array($this, 'enqueueScripts')); |
| 107 | $this->actionAdded = \true; |
| 108 | } |
| 109 | } |
| 110 | /** |
| 111 | * Remove action. |
| 112 | */ |
| 113 | public function removeAction() |
| 114 | { |
| 115 | if ($this->actionAdded) { |
| 116 | remove_action('admin_print_footer_scripts', array($this, 'maybeRenderJavascript')); |
| 117 | remove_action('admin_enqueue_scripts', array($this, 'enqueueScripts')); |
| 118 | $this->actionAdded = \false; |
| 119 | } |
| 120 | } |
| 121 | /** |
| 122 | * @return string |
| 123 | */ |
| 124 | public function getId() |
| 125 | { |
| 126 | return $this->id; |
| 127 | } |
| 128 | /** |
| 129 | * @param string $id |
| 130 | */ |
| 131 | public function setId($id) |
| 132 | { |
| 133 | $this->id = $id; |
| 134 | } |
| 135 | /** |
| 136 | * @return string |
| 137 | */ |
| 138 | public function getAnchor() |
| 139 | { |
| 140 | return $this->anchor; |
| 141 | } |
| 142 | /** |
| 143 | * @param string $anchor |
| 144 | */ |
| 145 | public function setAnchor($anchor) |
| 146 | { |
| 147 | $this->anchor = $anchor; |
| 148 | } |
| 149 | /** |
| 150 | * @return string |
| 151 | */ |
| 152 | public function getContent() |
| 153 | { |
| 154 | return $this->content; |
| 155 | } |
| 156 | /** |
| 157 | * @param string $content |
| 158 | */ |
| 159 | public function setContent($content) |
| 160 | { |
| 161 | $this->content = $content; |
| 162 | } |
| 163 | /** |
| 164 | * @return string |
| 165 | */ |
| 166 | public function getTitle() |
| 167 | { |
| 168 | return $this->title; |
| 169 | } |
| 170 | /** |
| 171 | * @param string $title |
| 172 | */ |
| 173 | public function setTitle($title) |
| 174 | { |
| 175 | $this->title = $title; |
| 176 | } |
| 177 | /** |
| 178 | * @return array|string |
| 179 | */ |
| 180 | public function getPosition() |
| 181 | { |
| 182 | return $this->position; |
| 183 | } |
| 184 | /** |
| 185 | * @param array|string $position |
| 186 | */ |
| 187 | public function setPosition($position) |
| 188 | { |
| 189 | $this->position = $position; |
| 190 | } |
| 191 | /** |
| 192 | * @return array|string |
| 193 | */ |
| 194 | public function getPointerClass() |
| 195 | { |
| 196 | return $this->pointerClass; |
| 197 | } |
| 198 | /** |
| 199 | * @param array|string $pointerClass |
| 200 | */ |
| 201 | public function setPointerClass($pointerClass) |
| 202 | { |
| 203 | $this->pointerClass = $pointerClass; |
| 204 | } |
| 205 | /** |
| 206 | * @return int |
| 207 | */ |
| 208 | public function getPointerWidth() |
| 209 | { |
| 210 | return $this->pointerWidth; |
| 211 | } |
| 212 | /** |
| 213 | * @param int $pointerWidth |
| 214 | */ |
| 215 | public function setPointerWidth($pointerWidth) |
| 216 | { |
| 217 | $this->pointerWidth = $pointerWidth; |
| 218 | } |
| 219 | /** |
| 220 | * @return PointerConditions |
| 221 | */ |
| 222 | public function getConditions() |
| 223 | { |
| 224 | return $this->conditions; |
| 225 | } |
| 226 | /** |
| 227 | * @return array |
| 228 | */ |
| 229 | public function getPointerCss() |
| 230 | { |
| 231 | return $this->pointerCss; |
| 232 | } |
| 233 | /** |
| 234 | * @param PointerConditions $conditions |
| 235 | */ |
| 236 | public function setConditions($conditions) |
| 237 | { |
| 238 | $this->conditions = $conditions; |
| 239 | } |
| 240 | /** |
| 241 | * Render Java Script for pointer message. |
| 242 | */ |
| 243 | public function renderJavaScript() |
| 244 | { |
| 245 | $pointerAnchor = $this->getAnchor(); |
| 246 | $pointerClass = $this->getPointerClass(); |
| 247 | $pointerContentId = 'wpdesk_pointer_content_' . $this->getId(); |
| 248 | $pointerWidth = $this->getPointerWidth(); |
| 249 | $pointerContent = sprintf('<h3>%1$s</h3><p id="%2$s">%3$s</p>', $this->title, $pointerContentId, $this->content); |
| 250 | $pointerPosition = $this->getPosition(); |
| 251 | $pointerId = $this->getId(); |
| 252 | $pointerCss = array_merge($this->defaultPointerCss, $this->getPointerCss()); |
| 253 | include 'views/html-script-pointer-message.php'; |
| 254 | } |
| 255 | /** |
| 256 | * Meybe render Java Script for pointer message. |
| 257 | */ |
| 258 | public function maybeRenderJavascript() |
| 259 | { |
| 260 | if ($this->conditions->areConditionsMet() && !$this->isDismissed()) { |
| 261 | $this->renderJavaScript(); |
| 262 | } |
| 263 | } |
| 264 | /** |
| 265 | * Is pointer message already dismissed? |
| 266 | * |
| 267 | * @return bool |
| 268 | */ |
| 269 | private function isDismissed() |
| 270 | { |
| 271 | $dismissedPointerMessages = array_filter(explode(',', (string) get_user_meta(get_current_user_id(), self::USER_META_DISMISSED_WP_POINTERS, \true))); |
| 272 | return in_array($this->id, $dismissedPointerMessages, \true); |
| 273 | } |
| 274 | /** |
| 275 | * Un dismiss pointer message. |
| 276 | */ |
| 277 | public function unDismiss() |
| 278 | { |
| 279 | $dismissedPointerMessages = array_filter(explode(',', (string) get_user_meta(get_current_user_id(), self::USER_META_DISMISSED_WP_POINTERS, \true))); |
| 280 | foreach ($dismissedPointerMessages as $key => $value) { |
| 281 | if ($value === $this->getId()) { |
| 282 | unset($dismissedPointerMessages[$key]); |
| 283 | update_user_meta(get_current_user_id(), self::USER_META_DISMISSED_WP_POINTERS, implode(',', $dismissedPointerMessages)); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 |