first commit

This commit is contained in:
2026-03-05 13:07:40 +01:00
commit 64ba0721ee
25709 changed files with 4691006 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
{
"name": "wpdesk\/wp-pointer",
"description": "Library for displaying Wordpress pointer messages.",
"license": "MIT",
"keywords": [
"wordpress",
"pointer",
"admin"
],
"homepage": "https:\/\/gitlab.com\/wpdesk\/wp-pointer",
"minimum-stability": "stable",
"authors": [
{
"name": "grola",
"email": "grola@wpdesk.net"
}
],
"require": {
"php": ">=5.5",
"wpdesk\/wp-builder": "^1.1|^2"
},
"require-dev": {
"phpunit\/phpunit": "<7",
"wp-coding-standards\/wpcs": "^0.14.1",
"squizlabs\/php_codesniffer": "^3.0.2",
"mockery\/mockery": "*",
"10up\/wp_mock": "*",
"wimg\/php-compatibility": "^8"
},
"autoload": {
"psr-4": {
"FSVendor\\WPDesk\\Pointer\\": "src\/WPDesk\/Pointer\/"
}
},
"autoload-dev": {},
"scripts": {
"phpcs": "phpcs",
"phpunit-unit": "phpunit --configuration phpunit-unit.xml --coverage-text --colors=never",
"phpunit-unit-fast": "phpunit --configuration phpunit-unit.xml --no-coverage",
"phpunit-integration": "phpunit --configuration phpunit-integration.xml --coverage-text --colors=never",
"phpunit-integration-fast": "phpunit --configuration phpunit-integration.xml --no-coverage"
}
}

View File

@@ -0,0 +1,97 @@
<?php
namespace FSVendor\WPDesk\Pointer;
/**
* Pointer conditions.
*
* @package WPDesk\Pointer
*/
class PointerConditions
{
/**
* @var null|string|array
*/
private $screenId;
/**
* @var null|string
*/
private $capability;
/**
* PointerConditions constructor.
*
* @param null|string|array $screenId Screen id. null or empty string - all screens.
* @param null|string $capability User capability. null or empty string for all capabilities.
*/
public function __construct($screenId = null, $capability = null)
{
$this->screenId = $screenId;
$this->capability = $capability;
}
/**
* @return array|string|null
*/
public function getScreenId()
{
return $this->screenId;
}
/**
* @param array|string|null $screenId
*/
public function setScreenId($screenId)
{
$this->screenId = $screenId;
}
/**
* @return string
*/
public function getCapability()
{
return $this->capability;
}
/**
* @param string $capability
*/
public function setCapability($capability)
{
$this->capability = $capability;
}
/**
* @return bool
*/
private function areScreenIdMet()
{
$screenIdMet = \false;
if (!empty($this->screenId)) {
if (!\is_array($this->screenId)) {
$this->screenId = array($this->screenId);
}
$screen = \get_current_screen();
if (null !== $screen && \in_array($screen->id, $this->screenId, \true)) {
$screenIdMet = \true;
}
} else {
$screenIdMet = \true;
}
return $screenIdMet;
}
/**
* @return bool
*/
private function areCapabilityMet()
{
if (!empty($this->capability)) {
$capabilityMet = \current_user_can($this->capability);
} else {
$capabilityMet = \true;
}
return $capabilityMet;
}
/**
* @return bool
*/
public function areConditionsMet()
{
return $this->areCapabilityMet() && $this->areScreenIdMet();
}
}

View File

@@ -0,0 +1,287 @@
<?php
namespace FSVendor\WPDesk\Pointer;
/**
* WordPress admin pointer message.
*
* @package WPDesk\Pointer
*/
class PointerMessage
{
const USER_META_DISMISSED_WP_POINTERS = 'dismissed_wp_pointers';
/**
* Is action added?
* @var bool
*/
private $actionAdded = \false;
/**
* @var string
*/
private $id;
/**
* @var string
*/
private $anchor;
/**
* @var string
*/
private $content;
/**
* @var string
*/
private $title;
/**
* @var PointerPosition
*/
private $position;
/**
* @var string|array
*/
private $pointerClass;
/**
* @var int
*/
private $pointerWidth;
/**
* @var PointerConditions
*/
private $conditions;
/**
* @var array
*/
private $pointerCss = array();
/**
* @var array
*/
private $defaultPointerCss = array('top' => '50%', 'left' => '100%', '-webkit-transform' => 'translateY(-50%)', '-ms-transform' => 'translateY(-50%)', 'transform' => 'translateY(-50%)');
/**
* PointerMessage constructor.
*
* @param string $id
* @param string $anchor
* @param string $title
* @param string $content
* @param PointerPosition $position
* @param string pointerClass
* @param int $pointerWidth
* @param null|PointerConditions $conditions Pointer conditions.
* @param array $pointerCss Pointer CSS.
*/
public function __construct($id, $anchor, $title, $content, $position = null, $pointerClass = 'wp-pointer', $pointerWidth = 320, $conditions = null, $pointerCss = array())
{
$this->id = $id;
$this->anchor = $anchor;
$this->title = $title;
$this->content = $content;
if ($position === null) {
$position = new \FSVendor\WPDesk\Pointer\PointerPosition();
}
$this->position = $position;
$this->pointerClass = $pointerClass;
$this->pointerWidth = $pointerWidth;
if (null === $conditions) {
$this->conditions = new \FSVendor\WPDesk\Pointer\PointerConditions();
} else {
$this->conditions = $conditions;
}
$this->pointerCss = $pointerCss;
$this->addAction();
}
/**
* Enqueue scripts.
*/
public function enqueueScripts()
{
\wp_enqueue_style('wp-pointer');
\wp_enqueue_script('wp-pointer');
}
/**
* Add notice action.
*/
protected function addAction()
{
if (!$this->actionAdded) {
\add_action('admin_print_footer_scripts', array($this, 'maybeRenderJavascript'));
\add_action('admin_enqueue_scripts', array($this, 'enqueueScripts'));
$this->actionAdded = \true;
}
}
/**
* Remove action.
*/
public function removeAction()
{
if ($this->actionAdded) {
\remove_action('admin_print_footer_scripts', array($this, 'maybeRenderJavascript'));
\remove_action('admin_enqueue_scripts', array($this, 'enqueueScripts'));
$this->actionAdded = \false;
}
}
/**
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* @param string $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return string
*/
public function getAnchor()
{
return $this->anchor;
}
/**
* @param string $anchor
*/
public function setAnchor($anchor)
{
$this->anchor = $anchor;
}
/**
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* @param string $content
*/
public function setContent($content)
{
$this->content = $content;
}
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* @return array|string
*/
public function getPosition()
{
return $this->position;
}
/**
* @param array|string $position
*/
public function setPosition($position)
{
$this->position = $position;
}
/**
* @return array|string
*/
public function getPointerClass()
{
return $this->pointerClass;
}
/**
* @param array|string $pointerClass
*/
public function setPointerClass($pointerClass)
{
$this->pointerClass = $pointerClass;
}
/**
* @return int
*/
public function getPointerWidth()
{
return $this->pointerWidth;
}
/**
* @param int $pointerWidth
*/
public function setPointerWidth($pointerWidth)
{
$this->pointerWidth = $pointerWidth;
}
/**
* @return PointerConditions
*/
public function getConditions()
{
return $this->conditions;
}
/**
* @return array
*/
public function getPointerCss()
{
return $this->pointerCss;
}
/**
* @param PointerConditions $conditions
*/
public function setConditions($conditions)
{
$this->conditions = $conditions;
}
/**
* Render Java Script for pointer message.
*/
public function renderJavaScript()
{
$pointerAnchor = $this->getAnchor();
$pointerClass = $this->getPointerClass();
$pointerContentId = 'wpdesk_pointer_content_' . $this->getId();
$pointerWidth = $this->getPointerWidth();
$pointerContent = \sprintf('<h3>%1$s</h3><p id="%2$s">%3$s</p>', $this->title, $pointerContentId, $this->content);
$pointerPosition = $this->getPosition();
$pointerId = $this->getId();
$pointerCss = \array_merge($this->defaultPointerCss, $this->getPointerCss());
include 'views/html-script-pointer-message.php';
}
/**
* Meybe render Java Script for pointer message.
*/
public function maybeRenderJavascript()
{
if ($this->conditions->areConditionsMet() && !$this->isDismissed()) {
$this->renderJavaScript();
}
}
/**
* Is pointer message already dismissed?
*
* @return bool
*/
private function isDismissed()
{
$dismissedPointerMessages = \array_filter(\explode(',', (string) \get_user_meta(\get_current_user_id(), self::USER_META_DISMISSED_WP_POINTERS, \true)));
return \in_array($this->id, $dismissedPointerMessages, \true);
}
/**
* Un dismiss pointer message.
*/
public function unDismiss()
{
$dismissedPointerMessages = \array_filter(\explode(',', (string) \get_user_meta(\get_current_user_id(), self::USER_META_DISMISSED_WP_POINTERS, \true)));
foreach ($dismissedPointerMessages as $key => $value) {
if ($value === $this->getId()) {
unset($dismissedPointerMessages[$key]);
\update_user_meta(\get_current_user_id(), self::USER_META_DISMISSED_WP_POINTERS, \implode(',', $dismissedPointerMessages));
}
}
}
}

View File

@@ -0,0 +1,64 @@
<?php
namespace FSVendor\WPDesk\Pointer;
/**
* WordPress admin pointer message position.
*
* @package WPDesk\Pointer
*/
class PointerPosition
{
const TOP = 'top';
const RIGHT = 'right';
const BOTTOM = 'bottom';
const LEFT = 'left';
/**
* @var string
*/
private $edge = \false;
/**
* @var string
*/
private $align;
public function __construct($edge = 'left', $align = 'top')
{
$this->edge = $edge;
$this->align = $align;
}
/**
* @return string
*/
public function getEdge()
{
return $this->edge;
}
/**
* @param string $edge
*/
public function setEdge($edge)
{
$this->edge = $edge;
}
/**
* @return string
*/
public function getAlign()
{
return $this->align;
}
/**
* @param string $align
*/
public function setAlign($align)
{
$this->align = $align;
}
/**
* Render as JSON.
*/
public function render()
{
echo \json_encode(array('edge' => $this->edge, 'align' => $this->align));
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace FSVendor\WPDesk\Pointer;
use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable;
/**
* Pointers handler.
*
* @package WPDesk\Pointer
*/
class PointersScripts implements \FSVendor\WPDesk\PluginBuilder\Plugin\Hookable
{
/**
* @var array
*/
private $enqueueOnScreens = array();
/**
* PointersScripts constructor.
*
* @param null|string|array $enqueueOnScreens Empty for all screens.
*/
public function __construct($enqueueOnScreens = array())
{
if (null === $enqueueOnScreens) {
$enqueueOnScreens = array();
}
if (!\is_array($enqueueOnScreens)) {
$enqueueOnScreens = array($enqueueOnScreens);
}
$this->enqueueOnScreens = $enqueueOnScreens;
}
/**
* Hooks.
*/
public function hooks()
{
\add_action('admin_enqueue_scripts', array($this, 'enqueueScripts'));
}
/**
* Enqueue scripts.
*
* @param string $hook
*/
public function enqueueScripts($hook)
{
if (\count($this->enqueueOnScreens) === 0 || \in_array($hook, $this->enqueueOnScreens, \true)) {
\wp_enqueue_style('wp-pointer');
\wp_enqueue_script('wp-pointer');
}
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace FSVendor;
/**
* @var string $pointerAnchor
* @var string $pointerContent
* @var \WPDesk\Pointer\PointerPosition $pointerPosition
* @var string $pointerId
* @var string $pointerContentId
* @var int $pointerWidth
* @var array $pointerCss
*/
?>
<script type="text/javascript">
jQuery(document).ready( function($) {
if(typeof(jQuery().pointer) != 'undefined') {
$('<?php
echo $pointerAnchor;
?>').pointer({
pointerWidth: <?php
echo $pointerWidth;
?>,
content: <?php
echo \json_encode($pointerContent);
?>,
position: <?php
echo $pointerPosition->render();
?>,
<?php
if ($pointerCss) {
?>
show: function(event, t){
t.pointer.css(<?php
echo \json_encode($pointerCss);
?>);
$('<?php
echo $pointerAnchor;
?>').css('position', 'relative');
t.pointer.appendTo('<?php
echo $pointerAnchor;
?>');
},
<?php
}
?>
close: function() {
$('#<?php
echo $pointerContentId;
?>').remove();
$.post(ajaxurl, {
pointer: '<?php
echo $pointerId;
?>',
action: 'dismiss-wp-pointer'
});
},
}).pointer('open');
}
});
</script>
<?php