| 1 |
<?php |
| 2 |
namespace LWS\WOOREWARDS\Ui; |
| 3 |
|
| 4 |
// don't call the file directly |
| 5 |
if( !defined( 'ABSPATH' ) ) exit(); |
| 6 |
|
| 7 |
/** Manage achievenent display. */ |
| 8 |
class Achievement |
| 9 |
{ |
| 10 |
function __construct() |
| 11 |
{ |
| 12 |
\add_action('lws_woorewards_achievement_push', array(__CLASS__, 'push'), 10, 1); |
| 13 |
|
| 14 |
\add_action('admin_footer', array($this, 'footer')); |
| 15 |
\add_action('wp_footer', array($this, 'footer')); |
| 16 |
\add_action('admin_enqueue_scripts', array($this , 'scripts'), 99999); // late to let anybody going first |
| 17 |
\add_action('wp_enqueue_scripts', array($this , 'scripts'), 99999); // late to let anybody going first |
| 18 |
} |
| 19 |
|
| 20 |
/** Register an achievement for display on the page (backend or frontend). |
| 21 |
* @param $options (array) with: |
| 22 |
* * 'title' (string) At least a title is required for custom achievement. |
| 23 |
* * 'message' (string) optional, display a custom achievement with that message. |
| 24 |
* * 'image' (url) Achievement icon, if no url given, a default image is picked. |
| 25 |
* * 'user' (int) recipient user id. |
| 26 |
* * 'origin' (mixed, optional) source of the achievement. |
| 27 |
* * 'badge_id' (int) source of the achievement but as an ID. |
| 28 |
* * 'visible' (bool) optional, default true. If false, the achievement is logged only but not shown to the user. |
| 29 |
* @param $user (int) recipient user id, if false, use $options, if still undefined, use \get_current_user_id(). |
| 30 |
* |
| 31 |
* If recipient user cannot be defined (not set, and no one logged in), |
| 32 |
* the message will be displayed to the first logged out guy that load a page, whoever he is. */ |
| 33 |
static function push($options=array(), $user=false) |
| 34 |
{ |
| 35 |
if( $options && is_array($options) && isset($options['title']) ) |
| 36 |
{ |
| 37 |
$data = array( |
| 38 |
'user_id' => self::getUserId($user, $options), |
| 39 |
'title' => $options['title'], |
| 40 |
); |
| 41 |
if( isset($options['message']) ) |
| 42 |
$data['message'] = $options['message']; |
| 43 |
if( isset($options['image']) ) |
| 44 |
$data['image'] = $options['image']; |
| 45 |
if( isset($options['background']) ) |
| 46 |
$data['background'] = $options['background']; |
| 47 |
if( isset($options['origin']) ) |
| 48 |
$data['origin'] = is_array($options['origin']) ? serialize($options['origin']) : $options['origin']; |
| 49 |
if( isset($options['badge_id']) ) |
| 50 |
$data['badge_id'] = intval($options['badge_id']); |
| 51 |
if( isset($options['visible']) ) |
| 52 |
$data['popup'] = boolval($options['visible']) ? 1 : 0; |
| 53 |
|
| 54 |
global $wpdb; |
| 55 |
$wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 56 |
self::tableName(), |
| 57 |
$data |
| 58 |
); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
static function tableName() |
| 63 |
{ |
| 64 |
global $wpdb; |
| 65 |
return $wpdb->prefix.'lws_wr_achieved_log'; |
| 66 |
} |
| 67 |
|
| 68 |
static protected function getUserId($user=false, $options=array()) |
| 69 |
{ |
| 70 |
if( !empty($user = intval($user)) ) |
| 71 |
return $user; |
| 72 |
if( is_array($options) && isset($options['user']) ) |
| 73 |
{ |
| 74 |
if( !empty($user = intval($options['user'])) ) |
| 75 |
return $user; |
| 76 |
} |
| 77 |
return intval(\get_current_user_id()); |
| 78 |
} |
| 79 |
|
| 80 |
protected function get() |
| 81 |
{ |
| 82 |
global $wpdb; |
| 83 |
$table = self::tableName(); |
| 84 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- custom table |
| 85 |
return $wpdb->get_results($wpdb->prepare("SELECT * FROM {$table} WHERE `display` IS NULL AND `popup`=1 AND user_id=%d ORDER BY id ASC", self::getUserId())); |
| 86 |
} |
| 87 |
|
| 88 |
protected function count() |
| 89 |
{ |
| 90 |
global $wpdb; |
| 91 |
$table = self::tableName(); |
| 92 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- custom table |
| 93 |
return $wpdb->get_var($wpdb->prepare("SELECT COUNT(id) FROM {$table} WHERE `display` IS NULL AND `popup`=1 AND user_id=%d", self::getUserId())); |
| 94 |
} |
| 95 |
|
| 96 |
protected function clear() |
| 97 |
{ |
| 98 |
global $wpdb; |
| 99 |
$table = self::tableName(); |
| 100 |
if( \get_option('lws_woorewards_achievement_log_delete_on_clear', false) ) { |
| 101 |
$wpdb->query( $wpdb->prepare( "DELETE FROM {$table} WHERE `display` IS NULL AND user_id=%d", self::getUserId() ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 102 |
} else { |
| 103 |
$wpdb->query( $wpdb->prepare( "UPDATE {$table} SET display=CURRENT_TIMESTAMP WHERE `display` IS NULL AND user_id=%d", self::getUserId() ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
protected function getScriptDependencies() |
| 108 |
{ |
| 109 |
return array('jquery', 'jquery-ui-core','jquery-ui-widget'); |
| 110 |
} |
| 111 |
|
| 112 |
public function scripts() |
| 113 |
{ |
| 114 |
\wp_register_script('lws-wre-badge', LWS_WOOREWARDS_JS.'/badge.js', $this->getScriptDependencies(), LWS_WOOREWARDS_VERSION, true); |
| 115 |
$key = 'lws_wre_badge_css'; |
| 116 |
$url = \apply_filters($key, LWS_WOOREWARDS_CSS.'/badge.css'); |
| 117 |
\wp_register_style($key, $url, array(), LWS_WOOREWARDS_VERSION); |
| 118 |
} |
| 119 |
|
| 120 |
/** Display achievement DOM. */ |
| 121 |
function footer() |
| 122 |
{ |
| 123 |
static $first = true; |
| 124 |
$achievements = $this->get(); |
| 125 |
while( !empty($achievements) ) |
| 126 |
{ |
| 127 |
$this->clear(); |
| 128 |
|
| 129 |
foreach( $achievements as $options ) |
| 130 |
{ |
| 131 |
$options = \apply_filters('lws_woorewards_achievement_options', $options); |
| 132 |
if( $options ) |
| 133 |
{ |
| 134 |
$title = esc_attr($options->title); |
| 135 |
$image = esc_attr($options->image ? $options->image : LWS_WOOREWARDS_IMG.'/badge-reward.png'); |
| 136 |
$background = esc_attr($options->background ? $options->background : LWS_WOOREWARDS_IMG.'/badge-star.png'); |
| 137 |
echo "<div class='lws_wre_badge' data-title='" . esc_attr($title) . "' data-imageurl='" . esc_attr($image) . "' data-bgurl='" . esc_attr($background) . "'>" . wp_kses_post($options->message) . "</div>"; |
| 138 |
if( $first ) |
| 139 |
{ |
| 140 |
$first = false; |
| 141 |
foreach( $this->getScriptDependencies() as $inc ) |
| 142 |
\wp_enqueue_script($inc); |
| 143 |
\wp_enqueue_script('lws-wre-badge'); |
| 144 |
\wp_enqueue_style('lws_wre_badge_css'); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
// cause unlock an achievement can produce achievement |
| 150 |
$achievements = $this->get(); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
} |