1+ <?php
2+ /**
3+ * Plugin Name: WishList Completed Contents
4+ * Description: Display the list of completed contents from WishList Member for a user on their profile.
5+ * Version: 1.0.0
6+ * Author: Obi Juan & Team Fabi Paolini
7+ * Author URI: http://www.obijuan.dev
8+ * License: GPLv2 or later
9+ * License URI: http://www.gnu.org/licenses/gpl-2.0.html
10+ * Text Domain: wishlist-completed-contents
11+ * Domain Path: /languages
12+ * Requires PHP: 7.4
13+ * Requires at least: 6.0
14+ *
15+ * @package wishlistCompletedContents
16+ * @category plugin
17+ * @author Obi Juan & Team Fabi Paolini
18+ * @license http://www.gnu.org/licenses/gpl-2.0.html
19+ * @link http://www.obijuan.dev
20+ * @since 1.0
21+ */
22+
23+ defined ( 'ABSPATH ' ) || exit ( 'Get outta here! ' );
24+
25+ /**
26+ * WishList_Completed_Contents
27+ *
28+ * @since 1.0
29+ */
30+ final class WishList_Completed_Contents {
31+
32+ /**
33+ *Plugin version.
34+ */
35+ const VERSION = '1.0.0 ' ;
36+
37+ /**
38+ * Plugin slug.
39+ */
40+ const SLUG = 'wishlist-completed-contents ' ;
41+
42+ /**
43+ * Coursecure slug.
44+ */
45+ const COURSECURE = 'CourseCure ' ;
46+
47+ /**
48+ * Singleton instance.
49+ *
50+ * @var WishList_Completed_Contents
51+ */
52+ private static $ _instance ;
53+
54+ /**
55+ * Singleton instance
56+ *
57+ * @return WishList_Completed_Contents
58+ */
59+ public static function get_instance () {
60+ if ( ! isset ( self ::$ _instance ) ) {
61+ self ::$ _instance = new self ();
62+ }
63+ return self ::$ _instance ;
64+ }
65+
66+ /**
67+ * Constructor
68+ *
69+ * @since 1.0
70+ */
71+ private function __construct () {
72+
73+ $ this ->_define_constants ();
74+
75+ $ this ->wishlist_check_php_version_requirement ();
76+
77+ $ this ->coursecure_check_dependency ();
78+
79+ $ this ->hooks_init ();
80+
81+ $ this ->get_active_plugins ();
82+ }
83+
84+ /**
85+ * Define constants
86+ *
87+ * @since 1.0
88+ */
89+ private function _define_constants () {
90+ define ( 'WISHLIST_COMPLETED_CONTENTS_VERSION ' , self ::VERSION );
91+ define ( 'WISHLIST_COMPLETED_CONTENTS_SLUG ' , self ::SLUG );
92+ define ( 'WISHLIST_COMPLETED_CONTENTS_TEXTDOMAIN ' , self ::SLUG );
93+ define ( 'WISHLIST_COMPLETED_CONTENTS_PHP_MINIMUM_VERSION ' , '7.4 ' );
94+ define ( 'WISHLIST_COMPLETED_CONTENTS_NAME ' , 'WishList Completed Contents ' );
95+ define ( 'WISHLIST_COMPLETED_CONTENTS_DIR ' , plugin_dir_path ( __FILE__ ) );
96+ define ( 'WISHLIST_COMPLETED_CONTENTS_URL ' , plugin_dir_url ( __FILE__ ) );
97+ }
98+
99+ /**
100+ * Hooks init
101+ *
102+ * Initialize mandatory hooks.
103+ *
104+ * @since 1.0
105+ */
106+ private function hooks_init () {
107+ // Add completed contents table to user profile.
108+ add_action ( 'show_user_profile ' , array ( $ this , 'display_completed_contents_table ' ) );
109+
110+ // Add completed contents table to user profile.
111+ add_action ( 'edit_user_profile ' , array ( $ this , 'display_completed_contents_table ' ) );
112+ }
113+
114+ /**
115+ * Load text domain
116+ *
117+ * @since 1.0
118+ */
119+ public function load_text_domain () {
120+ load_plugin_textdomain (
121+ WISHLIST_COMPLETED_CONTENTS_TEXTDOMAIN ,
122+ false ,
123+ dirname ( plugin_basename ( __FILE__ ) ) . '/languages '
124+ );
125+ }
126+
127+ /**
128+ * Compares PHP version requirement
129+ *
130+ * @access private
131+ * @return bool. True if PHP version requirement is met.
132+ * @since 1.0
133+ */
134+ public function wishlist_site_meets_php_minimum_version () {
135+ return version_compare ( phpversion (), WISHLIST_COMPLETED_CONTENTS_PHP_MINIMUM_VERSION , '>= ' );
136+ }
137+
138+ /**
139+ * Admin notice
140+ *
141+ * Display an admin notice if PHP version requirement is not met.
142+ *
143+ * @since 1.0
144+ */
145+ public function wishlist_check_php_version_requirement () {
146+ // Check if PHP version meets requirement.
147+ if ( ! $ this ->wishlist_site_meets_php_minimum_version () ) {
148+
149+ // Display admin notice.
150+ add_action ( 'admin_notices ' , array ( $ this , 'wishlist_admin_notice_php_version ' ) );
151+ }
152+ }
153+
154+ /**
155+ * Admin notice
156+ *
157+ * Display an admin notice if PHP version requirement is not met.
158+ *
159+ * @since 1.0
160+ */
161+ public function coursecure_check_dependency () {
162+ if ( ! $ this ->search_string_in_array ( 'coursecure ' , $ this ->get_active_plugins () ) ) {
163+ add_action ( 'admin_notices ' , array ( $ this , 'coursecoure_admin_notice_dependency ' ) );
164+ }
165+ }
166+
167+ /**
168+ * Admin notice
169+ *
170+ * Admin notice markup for the PHP version requirement evaluation.
171+ *
172+ * @since 1.0
173+ */
174+ public function wishlist_admin_notice_php_version () {
175+ ?>
176+
177+ <div class="notice notice-error">
178+ <p>
179+ <?php
180+ echo wp_kses_post (
181+ sprintf (
182+ /* translators: %s: Minimum required PHP version */
183+ __ ( '%1$s requires PHP version %2$s or later. Please upgrade PHP or disable the plugin. ' , 'wishlist-completed-contents ' ),
184+ esc_html ( WISHLIST_COMPLETED_CONTENTS_NAME ),
185+ esc_html ( WISHLIST_COMPLETED_CONTENTS_PHP_MINIMUM_VERSION )
186+ )
187+ );
188+ ?>
189+ </p>
190+ </div>
191+ <?php
192+ }
193+
194+ /**
195+ * Admin notice
196+ *
197+ * Admin notice markup for the 'CourseCure' plugin requirement evaluation.
198+ *
199+ * @since 1.0
200+ */
201+ public function coursecoure_admin_notice_dependency () {
202+ ?>
203+
204+ <div class="notice notice-error">
205+ <p>
206+ <?php
207+ echo wp_kses_post (
208+ sprintf (
209+ /* translators: %s: Wishlist Completed Contents plugin name. 2nd: 'CourseCure' plugin name. */
210+ __ ( '%1$s requires the %2$s plugin. Please install it or disable the plugin. ' , 'wishlist-completed-contents ' ),
211+ esc_html ( WISHLIST_COMPLETED_CONTENTS_NAME ),
212+ esc_html ( self ::COURSECURE )
213+ )
214+ );
215+ ?>
216+ </p>
217+ </div>
218+ <?php
219+ }
220+
221+ /**
222+ * Display completed contents table
223+ *
224+ * Display the list of completed contents from WishList Member for a user on their profile.
225+ *
226+ * @param object $user. The user object.
227+ * @since 1.0
228+ */
229+ public function display_completed_contents_table ( object $ user ) {
230+
231+ $ completed_contents = get_user_meta ( $ user ->ID , 'completed_contents ' , true );
232+ $ completed_contents_array = maybe_unserialize ( $ completed_contents );
233+
234+ if ( is_array ( $ completed_contents_array ) ) {
235+ echo '<h3 id="completedContents">Completed Contents</h3> ' ;
236+ // Modify the button's onclick event to append a query parameter and reload.
237+ echo '<table> ' ;
238+ echo '<tr><th>Post ID</th><th>Post Title</th><th>Post Type</th><th>Timestamp</th><th>Post Actions</th></tr> ' ;
239+
240+ foreach ( $ completed_contents_array as $ post_id => $ timestamp ) {
241+ $ edit_post_link = get_edit_post_link ( $ post_id );
242+ $ view_post_link = get_permalink ( $ post_id );
243+ $ post_title = get_the_title ( $ post_id );
244+ $ post_type = get_post_type ( $ post_id );
245+
246+ echo '<tr onMouseOver="this.style.background= \'#00acc8 \'" onMouseOut="this.style.background= \'#f0f0f1 \'"> ' ;
247+ echo '<td> ' . esc_html ( $ post_id ) . '</td> ' ;
248+ echo '<td> ' . esc_html ( $ post_title ) . '</td> ' ;
249+ echo '<td> ' . esc_html ( $ post_type ) . '</td> ' ;
250+ echo '<td> ' . esc_html ( gmdate ( 'Y-m-d H:i:s ' , $ timestamp ) ) . '</td> ' ; // Format the timestamp in GMT time.
251+ echo '<td> ' ;
252+ if ( $ edit_post_link ) {
253+ echo '<a href=" ' . esc_url ( $ edit_post_link ) . '" target="_blank">Edit</a> | ' ;
254+ }
255+ if ( $ view_post_link ) {
256+ echo '<a href=" ' . esc_url ( $ view_post_link ) . '" target="_blank">View</a> ' ;
257+ }
258+ echo '</td> ' ;
259+ echo '</tr> ' ;
260+ }
261+
262+ echo '</table> ' ;
263+ } else {
264+ echo 'No completed contents data available. ' ;
265+ }
266+ }
267+
268+ /**
269+ * Search string in array
270+ *
271+ * Helper method to search for a string in an array.
272+ *
273+ * @param $search_string. The string to search for.
274+ * @param $array. The array to search.
275+ * @return bool
276+ */
277+ public function search_string_in_array ( string $ search_string , array $ array ) {
278+ foreach ( $ array as $ element ) {
279+ if ( strpos ( $ element , $ search_string ) !== false ) {
280+ return true ;
281+ }
282+ }
283+ return false ;
284+ }
285+
286+ /**
287+ * Get active plugins
288+ *
289+ * Helper method to get the active plugins.
290+ *
291+ * @return array
292+ */
293+ public function get_active_plugins () {
294+ return get_option ( 'active_plugins ' );
295+ }
296+ }
297+
298+ /**
299+ * Initialize the plugin.
300+ *
301+ * @since 1.0
302+ */
303+ add_action (
304+ 'plugins_loaded ' ,
305+ function () {
306+ WishList_Completed_Contents::get_instance ();
307+ }
308+ );
0 commit comments