-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-scarcitycounter.php
130 lines (118 loc) · 3.75 KB
/
class-scarcitycounter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* Plugin Name: Scarcity Counter
* Description: WordPress Plugin for artificial scarcity. Great for marketing purposes.
* Version: 1.1.0
* Author: Simon Mayerhofer
* Author URI: https://mayerhofer.it
* Plugin URI: https://github.com/SimonMayerhofer/scarcity-counter
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: scarcity-counter
* Domain Path: /languages
*
* Scarcity Counter is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* any later version.
*
* Scarcity Counter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scarcity Counter. If not, see https://www.gnu.org/licenses/gpl-2.0.html.
*
* @package scarcity-counter
*/
defined( 'ABSPATH' ) || die( 'No script kiddies please!' );
require_once ABSPATH . 'wp-admin/includes/plugin.php';
if ( ! class_exists( 'ScarcityCounter' ) ) {
/**
* ScarcityCounter init class
*/
class ScarcityCounter {
const DOMAIN = 'scarcity-counter';
/**
* Initializes the plugin
*
* It registers the scripts and initializes hooks
*/
public static function init() {
ScarcityCounter::init_hooks();
ScarcityCounter::init_shortcodes();
}
/**
* Shortcode function for [scarcity-counter] shortcode.
*
* Attributes:
* id - id to identify the counter. if used multiple times, the same number is shown.
* start - number to start with.
* min - lower limit. the counter won't ever get below that limit.
* reduction - upper range limit. E.g. if 5 is set as the limit, the counter will be
* reduced by a number between 1 and 5.
* timelimit - number in milliseconds how long to wait until another reduction.
*
* @param array $atts passed attributes.
*/
public static function get_shortcode( $atts ) {
// enqueue script only if shortcode is used.
wp_enqueue_script( ScarcityCounter::DOMAIN . '-script' );
// Attributes.
$atts = shortcode_atts(
array(
'id' => 'id',
'start' => '27',
'min' => '3',
'reduction' => '3',
'timelimit' => '5000',
),
$atts,
'scarcity-counter'
);
$el = '<span class="scarcity-counter" ';
$el .= 'data-id="' . $atts['id'] . '" ';
$el .= 'data-start="' . $atts['start'] . '" ';
$el .= 'data-min="' . $atts['min'] . '" ';
$el .= 'data-reduction="' . $atts['reduction'] . '" ';
$el .= 'data-timelimit="' . $atts['timelimit'] . '" ';
$el .= '></span>';
return $el;
}
/**
* Enqueue all scripts needed by the plugin
*/
public static function enqueue_scripts() {
// register script.
wp_register_script(
ScarcityCounter::DOMAIN . '-script',
plugins_url( 'js/build/build.js', __FILE__ ),
false,
filemtime( plugin_dir_path( __FILE__ ) . 'js/build/build.js' ),
true
);
}
/**
* Initializes all shortcodes
*
* Available Shortcodes:
* - [scarcity-counter]
*/
public static function init_shortcodes() {
$self = new self();
$shortcode_func = array( $self, 'get_shortcode' );
add_shortcode( 'scarcity-counter', $shortcode_func );
}
/**
* Initializes all hooks
*/
public static function init_hooks() {
$self = new self();
// Enqueue scripts and styles.
$enqueue_scripts_function = array( $self, 'enqueue_scripts' );
add_action( 'wp_enqueue_scripts', $enqueue_scripts_function );
}
}
ScarcityCounter::init(); // load plugin.
}