-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwishlist-completed-contents.php
308 lines (274 loc) · 7.48 KB
/
wishlist-completed-contents.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/**
* Plugin Name: WishList Completed Contents
* Description: Display the list of completed contents from WishList Member for a user on their profile.
* Version: 1.0.0
* Author: Obi Juan & Team Fabi Paolini
* Author URI: http://www.obijuan.dev
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wishlist-completed-contents
* Domain Path: /languages
* Requires PHP: 7.4
* Requires at least: 6.0
*
* @package wishlistCompletedContents
* @category plugin
* @author Obi Juan & Team Fabi Paolini
* @license http://www.gnu.org/licenses/gpl-2.0.html
* @link http://www.obijuan.dev
* @since 1.0
*/
defined( 'ABSPATH' ) || exit( 'Get outta here!' );
/**
* WishList_Completed_Contents
*
* @since 1.0
*/
final class WishList_Completed_Contents {
/**
*Plugin version.
*/
const VERSION = '1.0.0';
/**
* Plugin slug.
*/
const SLUG = 'wishlist-completed-contents';
/**
* Coursecure slug.
*/
const COURSECURE = 'CourseCure';
/**
* Singleton instance.
*
* @var WishList_Completed_Contents
*/
private static $_instance;
/**
* Singleton instance
*
* @return WishList_Completed_Contents
*/
public static function get_instance() {
if ( ! isset( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Constructor
*
* @since 1.0
*/
private function __construct() {
$this->_define_constants();
$this->wishlist_check_php_version_requirement();
$this->coursecure_check_dependency();
$this->hooks_init();
$this->get_active_plugins();
}
/**
* Define constants
*
* @since 1.0
*/
private function _define_constants() {
define( 'WISHLIST_COMPLETED_CONTENTS_VERSION', self::VERSION );
define( 'WISHLIST_COMPLETED_CONTENTS_SLUG', self::SLUG );
define( 'WISHLIST_COMPLETED_CONTENTS_TEXTDOMAIN', self::SLUG );
define( 'WISHLIST_COMPLETED_CONTENTS_PHP_MINIMUM_VERSION', '7.4' );
define( 'WISHLIST_COMPLETED_CONTENTS_NAME', 'WishList Completed Contents' );
define( 'WISHLIST_COMPLETED_CONTENTS_DIR', plugin_dir_path( __FILE__ ) );
define( 'WISHLIST_COMPLETED_CONTENTS_URL', plugin_dir_url( __FILE__ ) );
}
/**
* Hooks init
*
* Initialize mandatory hooks.
*
* @since 1.0
*/
private function hooks_init() {
// Add completed contents table to user profile.
add_action( 'show_user_profile', array( $this, 'display_completed_contents_table' ) );
// Add completed contents table to user profile.
add_action( 'edit_user_profile', array( $this, 'display_completed_contents_table' ) );
}
/**
* Load text domain
*
* @since 1.0
*/
public function load_text_domain() {
load_plugin_textdomain(
WISHLIST_COMPLETED_CONTENTS_TEXTDOMAIN,
false,
dirname( plugin_basename( __FILE__ ) ) . '/languages'
);
}
/**
* Compares PHP version requirement
*
* @access private
* @return bool. True if PHP version requirement is met.
* @since 1.0
*/
public function wishlist_site_meets_php_minimum_version() {
return version_compare( phpversion(), WISHLIST_COMPLETED_CONTENTS_PHP_MINIMUM_VERSION, '>=' );
}
/**
* Admin notice
*
* Display an admin notice if PHP version requirement is not met.
*
* @since 1.0
*/
public function wishlist_check_php_version_requirement() {
// Check if PHP version meets requirement.
if ( ! $this->wishlist_site_meets_php_minimum_version() ) {
// Display admin notice.
add_action( 'admin_notices', array( $this, 'wishlist_admin_notice_php_version' ) );
}
}
/**
* Admin notice
*
* Display an admin notice if PHP version requirement is not met.
*
* @since 1.0
*/
public function coursecure_check_dependency() {
if ( ! $this->search_string_in_array( 'coursecure', $this->get_active_plugins() ) ) {
add_action( 'admin_notices', array( $this, 'coursecoure_admin_notice_dependency' ) );
}
}
/**
* Admin notice
*
* Admin notice markup for the PHP version requirement evaluation.
*
* @since 1.0
*/
public function wishlist_admin_notice_php_version() {
?>
<div class="notice notice-error">
<p>
<?php
echo wp_kses_post(
sprintf(
/* translators: %s: Minimum required PHP version */
__( '%1$s requires PHP version %2$s or later. Please upgrade PHP or disable the plugin.', 'wishlist-completed-contents' ),
esc_html( WISHLIST_COMPLETED_CONTENTS_NAME ),
esc_html( WISHLIST_COMPLETED_CONTENTS_PHP_MINIMUM_VERSION )
)
);
?>
</p>
</div>
<?php
}
/**
* Admin notice
*
* Admin notice markup for the 'CourseCure' plugin requirement evaluation.
*
* @since 1.0
*/
public function coursecoure_admin_notice_dependency() {
?>
<div class="notice notice-error">
<p>
<?php
echo wp_kses_post(
sprintf(
/* translators: %s: Wishlist Completed Contents plugin name. 2nd: 'CourseCure' plugin name. */
__( '%1$s requires the %2$s plugin. Please install it or disable the plugin.', 'wishlist-completed-contents' ),
esc_html( WISHLIST_COMPLETED_CONTENTS_NAME ),
esc_html( self::COURSECURE )
)
);
?>
</p>
</div>
<?php
}
/**
* Display completed contents table
*
* Display the list of completed contents from WishList Member for a user on their profile.
*
* @param object $user. The user object.
* @since 1.0
*/
public function display_completed_contents_table( object $user ) {
$completed_contents = get_user_meta( $user->ID, 'completed_contents', true );
$completed_contents_array = maybe_unserialize( $completed_contents );
if ( is_array( $completed_contents_array ) ) {
echo '<h3 id="completedContents">Completed Contents</h3>';
// Modify the button's onclick event to append a query parameter and reload.
echo '<table>';
echo '<tr><th>Post ID</th><th>Post Title</th><th>Post Type</th><th>Timestamp</th><th>Post Actions</th></tr>';
foreach ( $completed_contents_array as $post_id => $timestamp ) {
$edit_post_link = get_edit_post_link( $post_id );
$view_post_link = get_permalink( $post_id );
$post_title = get_the_title( $post_id );
$post_type = get_post_type( $post_id );
echo '<tr onMouseOver="this.style.background=\'#00acc8\'" onMouseOut="this.style.background=\'#f0f0f1\'">';
echo '<td>' . esc_html( $post_id ) . '</td>';
echo '<td>' . esc_html( $post_title ) . '</td>';
echo '<td>' . esc_html( $post_type ) . '</td>';
echo '<td>' . esc_html( gmdate( 'Y-m-d H:i:s', $timestamp ) ) . '</td>'; // Format the timestamp in GMT time.
echo '<td>';
if ( $edit_post_link ) {
echo '<a href="' . esc_url( $edit_post_link ) . '" target="_blank">Edit</a> | ';
}
if ( $view_post_link ) {
echo '<a href="' . esc_url( $view_post_link ) . '" target="_blank">View</a>';
}
echo '</td>';
echo '</tr>';
}
echo '</table>';
} else {
echo 'No completed contents data available.';
}
}
/**
* Search string in array
*
* Helper method to search for a string in an array.
*
* @param $search_string. The string to search for.
* @param $array. The array to search.
* @return bool
*/
public function search_string_in_array( string $search_string, array $array ) {
foreach ( $array as $element ) {
if ( strpos( $element, $search_string ) !== false ) {
return true;
}
}
return false;
}
/**
* Get active plugins
*
* Helper method to get the active plugins.
*
* @return array
*/
public function get_active_plugins() {
return get_option( 'active_plugins' );
}
}
/**
* Initialize the plugin.
*
* @since 1.0
*/
add_action(
'plugins_loaded',
function () {
WishList_Completed_Contents::get_instance();
}
);