class.simple-wp-membership.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. <?php
  2. include_once('class.swpm-utils-misc.php');
  3. include_once('class.swpm-utils.php');
  4. include_once('class.swpm-utils-member.php');
  5. include_once('class.swpm-utils-membership-level.php');
  6. include_once('class.swpm-utils-template.php');
  7. include_once('class.swpm-init-time-tasks.php');
  8. include_once('class.swpm-wp-loaded-tasks.php');
  9. include_once('class.swpm-self-action-handler.php');
  10. include_once('class.swpm-comment-form-related.php');
  11. include_once('class.swpm-settings.php');
  12. include_once('class.swpm-protection.php');
  13. include_once('class.swpm-permission.php');
  14. include_once('class.swpm-auth.php');
  15. include_once('class.swpm-access-control.php');
  16. include_once('class.swpm-form.php');
  17. include_once('class.swpm-transfer.php');
  18. include_once('class.swpm-front-form.php');
  19. include_once('class.swpm-level-form.php');
  20. include_once('class.swpm-membership-levels.php');
  21. include_once('class.swpm-log.php');
  22. include_once('class.swpm-messages.php');
  23. include_once('class.swpm-ajax.php');
  24. include_once('class.swpm-registration.php');
  25. include_once('class.swpm-front-registration.php');
  26. include_once('class.swpm-admin-registration.php');
  27. include_once('class.swpm-membership-level.php');
  28. include_once('class.swpm-membership-level-custom.php');
  29. include_once('class.swpm-permission-collection.php');
  30. include_once('class.swpm-auth-permission-collection.php');
  31. include_once('class.swpm-transactions.php');
  32. include_once('shortcode-related/class.swpm-shortcodes-handler.php');
  33. include_once('class-swpm-member-subscriptions.php');
  34. class SimpleWpMembership {
  35. public function __construct() {
  36. new SwpmShortcodesHandler(); //Tackle the shortcode definitions and implementation.
  37. new SwpmSelfActionHandler(); //Tackle the self action hook handling.
  38. add_action('admin_menu', array(&$this, 'menu'));
  39. add_action('init', array(&$this, 'init_hook'));
  40. add_action('wp_loaded', array(&$this, 'handle_wp_loaded_tasks'));
  41. add_filter('the_content', array(&$this, 'filter_content'), 20, 1);
  42. add_filter('widget_text', 'do_shortcode');
  43. add_filter('show_admin_bar', array(&$this, 'hide_adminbar'));
  44. add_filter('comment_text', array(&$this, 'filter_comment'));
  45. add_filter('comment_form_defaults', array('SwpmCommentFormRelated', 'customize_comment_fields'));
  46. add_filter('wp_get_attachment_url', array(&$this, 'filter_attachment_url'), 10, 2);
  47. add_filter('wp_get_attachment_metadata', array(&$this, 'filter_attachment'), 10, 2);
  48. add_filter('attachment_fields_to_save', array(&$this, 'save_attachment_extra'), 10, 2);
  49. //TODO - refactor these shortcodes into the shortcodes handler class
  50. add_shortcode("swpm_registration_form", array(&$this, 'registration_form'));
  51. add_shortcode('swpm_profile_form', array(&$this, 'profile_form'));
  52. add_shortcode('swpm_login_form', array(&$this, 'login'));
  53. add_shortcode('swpm_reset_form', array(&$this, 'reset'));
  54. add_action('wp_head', array(&$this, 'wp_head_callback'));
  55. add_action('save_post', array(&$this, 'save_postdata'));
  56. add_action('admin_notices', array(&$this, 'do_admin_notices'));
  57. add_action('wp_enqueue_scripts', array(&$this, 'front_library'));
  58. add_action('load-toplevel_page_simple_wp_membership', array(&$this, 'admin_library'));
  59. add_action('load-wp-membership_page_simple_wp_membership_levels', array(&$this, 'admin_library'));
  60. add_action('wp_login', array(&$this, 'wp_login_hook_handler'), 10, 2);
  61. add_action('wp_authenticate', array(&$this, 'wp_authenticate_handler'), 1, 2);
  62. add_action('wp_logout', array(&$this, 'wp_logout'));
  63. add_action('swpm_logout', array(&$this, 'swpm_do_user_logout'));
  64. add_action('user_register', array(&$this, 'swpm_handle_wp_user_registration'));
  65. add_action('profile_update', array(&$this, 'sync_with_wp_profile'), 10, 2);
  66. //AJAX hooks
  67. add_action('wp_ajax_swpm_validate_email', 'SwpmAjax::validate_email_ajax');
  68. add_action('wp_ajax_nopriv_swpm_validate_email', 'SwpmAjax::validate_email_ajax');
  69. add_action('wp_ajax_swpm_validate_user_name', 'SwpmAjax::validate_user_name_ajax');
  70. add_action('wp_ajax_nopriv_swpm_validate_user_name', 'SwpmAjax::validate_user_name_ajax');
  71. //init is too early for settings api.
  72. add_action('admin_init', array(&$this, 'admin_init_hook'));
  73. add_action('plugins_loaded', array(&$this, "plugins_loaded"));
  74. add_action('password_reset', array(&$this, 'wp_password_reset_hook'), 10, 2);
  75. }
  76. public function wp_head_callback() {
  77. //This function is triggered by the wp_head action hook
  78. //Check if members only commenting is allowed then customize the form accordingly
  79. SwpmCommentFormRelated::customize_comment_form();
  80. //Other wp_head related tasks go here.
  81. }
  82. function wp_password_reset_hook($user, $pass) {
  83. $swpm_user = SwpmMemberUtils::get_user_by_user_name($user->user_login);
  84. //Check if SWPM user entry exists
  85. if (empty($swpm_user)) {
  86. SwpmLog::log_auth_debug("wp_password_reset_hook() - SWPM user not found for username: '" . $user->user_login ."'. This is OK, assuming that this user was created directly in WP Users menu (not using SWPM).", true);
  87. return;
  88. }
  89. $swpm_id = $swpm_user->member_id;
  90. if (!empty($swpm_id)) {
  91. $password_hash = SwpmUtils::encrypt_password($pass);
  92. global $wpdb;
  93. $wpdb->update($wpdb->prefix . "swpm_members_tbl", array('password' => $password_hash), array('member_id' => $swpm_id));
  94. }
  95. }
  96. public function save_attachment_extra($post, $attachment) {
  97. $this->save_postdata($post['ID']);
  98. return $post;
  99. }
  100. public function filter_attachment($content, $post_id) {
  101. if (is_admin()) {//No need to filter on the admin side
  102. return $content;
  103. }
  104. $acl = SwpmAccessControl::get_instance();
  105. if (has_post_thumbnail($post_id)) {
  106. return $content;
  107. }
  108. $post = get_post($post_id);
  109. if ($acl->can_i_read_post($post)) {
  110. return $content;
  111. }
  112. if (isset($content['file'])) {
  113. $content['file'] = 'restricted-icon.png';
  114. $content['width'] = '400';
  115. $content['height'] = '400';
  116. }
  117. if (isset($content['sizes'])) {
  118. if ($content['sizes']['thumbnail']) {
  119. $content['sizes']['thumbnail']['file'] = 'restricted-icon.png';
  120. $content['sizes']['thumbnail']['mime-type'] = 'image/png';
  121. }
  122. if ($content['sizes']['medium']) {
  123. $content['sizes']['medium']['file'] = 'restricted-icon.png';
  124. $content['sizes']['medium']['mime-type'] = 'image/png';
  125. }
  126. if (isset($content['sizes']['post-thumbnail'])) {
  127. $content['sizes']['post-thumbnail']['file'] = 'restricted-icon.png';
  128. $content['sizes']['post-thumbnail']['mime-type'] = 'image/png';
  129. }
  130. }
  131. return $content;
  132. }
  133. public function filter_attachment_url($content, $post_id) {
  134. if (is_admin()) {//No need to filter on the admin side
  135. return $content;
  136. }
  137. $acl = SwpmAccessControl::get_instance();
  138. if (has_post_thumbnail($post_id)) {
  139. return $content;
  140. }
  141. $post = get_post($post_id);
  142. if ($acl->can_i_read_post($post)) {
  143. return $content;
  144. }
  145. return SwpmUtils::get_restricted_image_url();
  146. }
  147. public function admin_init_hook() {
  148. //This hook is triggered in the wp-admin side only.
  149. $this->common_library(); //Load the common JS libraries and Styles
  150. $swpm_settings_obj = SwpmSettings::get_instance();
  151. //Check if the "Disable Access to WP Dashboard" option is enabled.
  152. $disable_wp_dashboard_for_non_admins = $swpm_settings_obj->get_value('disable-access-to-wp-dashboard');
  153. if ($disable_wp_dashboard_for_non_admins) {
  154. //This option is enabled
  155. if ((defined('DOING_AJAX') && DOING_AJAX)) {
  156. //This is an ajax request. Don't do the disable dashboard check for ajax.
  157. } else {
  158. //Not an ajax request. Do the check.
  159. if (!current_user_can('administrator')) {
  160. //This is a non-admin user. Do not show the wp dashboard.
  161. $message = '<p>' . SwpmUtils::_('The admin of this site does not allow users to access the wp dashboard.') . '</p>';
  162. $message .= '<p>' . SwpmUtils::_('Go back to the home page by ') . '<a href="' . SIMPLE_WP_MEMBERSHIP_SITE_HOME_URL . '">' . SwpmUtils::_('clicking here') . '</a>.' . '</p>';
  163. wp_die($message);
  164. }
  165. }
  166. }
  167. //Initialize the settings menu hooks.
  168. $swpm_settings_obj->init_config_hooks();
  169. $addon_saved = filter_input(INPUT_POST, 'swpm-addon-settings');
  170. if (!empty($addon_saved) && current_user_can('manage_options')) {
  171. check_admin_referer('swpm_addon_settings_section', 'swpm_addon_settings_section_save_settings');
  172. do_action('swpm_addon_settings_save');
  173. }
  174. }
  175. public function hide_adminbar() {
  176. //Never show admin toolbar if the user is not even logged in
  177. if (!is_user_logged_in()) {
  178. return false;
  179. }
  180. //Show admin toolbar to admin only feature is enabled.
  181. $show_to_admin = SwpmSettings::get_instance()->get_value('show-adminbar-admin-only');
  182. if ($show_to_admin) {
  183. if (current_user_can('administrator')) {
  184. //This is an admin user so show the tooldbar
  185. return true;
  186. } else {
  187. return false;
  188. }
  189. }
  190. //Hide admin toolbar if the hide adminbar feature is enabled
  191. $hide = SwpmSettings::get_instance()->get_value('hide-adminbar');
  192. return $hide ? FALSE : TRUE;
  193. }
  194. public function shutdown() {
  195. SwpmLog::writeall();
  196. }
  197. public static function swpm_login($username, $pass, $rememberme = true) {
  198. if (is_user_logged_in()) {
  199. $current_user = wp_get_current_user();
  200. SwpmLog::log_auth_debug("static function swpm_login(). User is logged in. WP Username: " . $current_user->user_login, true);
  201. if ($current_user->user_login == $username) {
  202. return;
  203. }
  204. }
  205. SwpmLog::log_auth_debug("Trying wp_signon() with username: " . $username, true);
  206. add_filter('wordfence_ls_require_captcha', '__return_false');//For Wordfence plugin's captcha compatibility
  207. $user_obj = wp_signon(array('user_login' => $username, 'user_password' => $pass, 'remember' => $rememberme), is_ssl());
  208. if ($user_obj instanceof WP_User) {
  209. wp_set_current_user($user_obj->ID, $user_obj->user_login);
  210. SwpmLog::log_auth_debug("Setting current WP user to: " . $user_obj->user_login, true);
  211. } else {
  212. SwpmLog::log_auth_debug("wp_signon() failed for the corresponding WP user account.", false);
  213. if (is_wp_error($user_obj)) {
  214. //SwpmLog::log_auth_debug("Error Message: ". $user_obj->get_error_message(), false);
  215. $force_wp_user_sync = SwpmSettings::get_instance()->get_value('force-wp-user-sync');
  216. if (!empty($force_wp_user_sync)) {
  217. //Force WP user login sync is enabled. Show error and exit out since the WP user login failed.
  218. $error_msg = SwpmUtils::_("Error! This site has the force WP user login feature enabled in the settings. We could not find a WP user record for the given username: ") . $username;
  219. $error_msg .= "<br /><br />" . SwpmUtils::_("This error is triggered when a member account doesn't have a corresponding WP user account. So the plugin fails to log the user into the WP User system.");
  220. $error_msg .= "<br /><br />" . SwpmUtils::_("Contact the site admin and request them to check your username in the WP Users menu to see what happened with the WP user entry of your account.");
  221. $error_msg .= "<br /><br />" . SwpmUtils::_("The site admin can disable the Force WP User Synchronization feature in the settings to disable this feature and this error will go away.");
  222. $error_msg .= "<br /><br />" . SwpmUtils::_("You can use the back button of your browser to go back to the site.");
  223. wp_die($error_msg);
  224. }
  225. }
  226. }
  227. $proceed_after_auth = apply_filters('swpm_login_auth_completed_filter', true);
  228. if (!$proceed_after_auth) {
  229. $auth = SwpmAuth::get_instance();
  230. $auth->logout();
  231. return;
  232. }
  233. SwpmLog::log_auth_debug("Triggering swpm_after_login hook.", true);
  234. do_action('swpm_after_login');
  235. if (!SwpmUtils::is_ajax()) {
  236. $redirect_url = apply_filters('swpm_after_login_redirect_url', SIMPLE_WP_MEMBERSHIP_SITE_HOME_URL);
  237. wp_redirect($redirect_url);
  238. exit(0);
  239. }
  240. }
  241. public function swpm_do_user_logout() {
  242. if (is_user_logged_in()) {
  243. wp_logout();
  244. wp_set_current_user(0);
  245. }
  246. }
  247. /* This function can be used to authenticate a member using currently logged in wp user. */
  248. public function set_current_user_handler() {
  249. $auth = SwpmAuth::get_instance();
  250. if ($auth->is_logged_in()) {
  251. return;
  252. }
  253. $user = wp_get_current_user();
  254. if (empty($user) || $user->ID === 0) {
  255. return false;
  256. }
  257. SwpmLog::log_auth_debug('set_current_user action. Attempting to login user ' . $user->user_login, true);
  258. //remove hook in order for it to not be called several times in the process
  259. remove_action('set_current_user', array($this, 'set_current_user_handler'));
  260. $auth->login_to_swpm_using_wp_user($user);
  261. }
  262. /* Used to log the user into SWPM system using the wp_login hook. Some social plugins use this hook to handle the login */
  263. public function wp_login_hook_handler($user_login, $user){
  264. SwpmLog::log_auth_debug('wp_login hook triggered. Username: ' . $user_login, true);
  265. $auth = SwpmAuth::get_instance();
  266. if ($auth->is_logged_in()) {
  267. //User is already logged-in. Nothing to do.
  268. return;
  269. }
  270. $auth->login_to_swpm_using_wp_user($user);
  271. }
  272. public function wp_authenticate_handler($username, $password) {
  273. $auth = SwpmAuth::get_instance();
  274. if (($auth->is_logged_in() && ($auth->userData->user_name == $username))) {
  275. SwpmLog::log_auth_debug('wp_authenticate action. User with username: ' . $username . ' is already logged in.', true);
  276. return;
  277. }
  278. if (!empty($username)) {
  279. SwpmLog::log_auth_debug('wp_authenticate action. Handling login for username: ' . $username, true);
  280. $auth->login($username, $password, true);
  281. } else {
  282. //empty username can mean some plugin trying to login WP user using its own methods.
  283. //Let's add hook for set_current_user action and let it handle the login if needed.
  284. SwpmLog::log_auth_debug('wp_authenticate action. Empty username provided. Adding set_current_username hook to catch potential login attempt.', true);
  285. add_action('set_current_user', array($this, 'set_current_user_handler'));
  286. }
  287. }
  288. public function login() {
  289. ob_start();
  290. $auth = SwpmAuth::get_instance();
  291. if ($auth->is_logged_in()) {
  292. //Load the template for logged-in member
  293. SwpmUtilsTemplate::swpm_load_template('loggedin.php', false);
  294. } else {
  295. //Load the login widget template
  296. SwpmUtilsTemplate::swpm_load_template('login.php', false);
  297. }
  298. return ob_get_clean();
  299. }
  300. public function wp_logout() {
  301. $auth = SwpmAuth::get_instance();
  302. if ($auth->is_logged_in()) {
  303. $auth->logout();
  304. }
  305. }
  306. public function sync_with_wp_profile($wp_user_id) {
  307. global $wpdb;
  308. $wp_user_data = get_userdata($wp_user_id);
  309. $query = $wpdb->prepare("SELECT * FROM " . $wpdb->prefix . "swpm_members_tbl WHERE " . ' user_name=%s', $wp_user_data->user_login);
  310. $profile = $wpdb->get_row($query, ARRAY_A);
  311. $profile = (array) $profile;
  312. if (empty($profile)) {
  313. return;
  314. }
  315. $profile['user_name'] = $wp_user_data->user_login;
  316. $profile['email'] = $wp_user_data->user_email;
  317. $profile['password'] = $wp_user_data->user_pass;
  318. $profile['first_name'] = $wp_user_data->user_firstname;
  319. $profile['last_name'] = $wp_user_data->user_lastname;
  320. $wpdb->update($wpdb->prefix . "swpm_members_tbl", $profile, array('member_id' => $profile['member_id']));
  321. }
  322. function swpm_handle_wp_user_registration($user_id) {
  323. $swpm_settings_obj = SwpmSettings::get_instance();
  324. $enable_auto_create_swpm_members = $swpm_settings_obj->get_value('enable-auto-create-swpm-members');
  325. $default_level = $swpm_settings_obj->get_value('auto-create-default-membership-level');
  326. $default_ac_status = $swpm_settings_obj->get_value('auto-create-default-account-status');
  327. if (empty($enable_auto_create_swpm_members)) {
  328. return;
  329. }
  330. if (empty($default_level)) {
  331. return;
  332. }
  333. $user_info = get_userdata($user_id);
  334. if (SwpmMemberUtils::get_user_by_user_name($user_info->user_login)) {
  335. SwpmLog::log_simple_debug("swpm_handle_wp_user_registration() - SWPM member account with this username already exists! No new account will be created for this user.", false);
  336. return;
  337. }
  338. if (SwpmMemberUtils::get_user_by_email($user_info->user_email)) {
  339. SwpmLog::log_simple_debug("swpm_handle_wp_user_registration() - SWPM member account with this email already exists! No new account will be created for this user.", false);
  340. return;
  341. }
  342. $fields = array();
  343. $fields['user_name'] = $user_info->user_login;
  344. $fields['password'] = $user_info->user_pass;
  345. $fields['email'] = $user_info->user_email;
  346. $fields['first_name'] = $user_info->first_name;
  347. $fields['last_name'] = $user_info->last_name;
  348. $fields['birth_place'] = $user_info->birth_place;
  349. $fields['membership_level'] = $default_level;
  350. $fields['member_since'] = SwpmUtils::get_current_date_in_wp_zone();
  351. $fields['account_state'] = $default_ac_status;
  352. $fields['subscription_starts'] = SwpmUtils::get_current_date_in_wp_zone();
  353. SwpmMemberUtils::create_swpm_member_entry_from_array_data($fields);
  354. }
  355. public function reset() {
  356. $succeeded = $this->notices();
  357. if ($succeeded) {
  358. return '';
  359. }
  360. ob_start();
  361. //Load the forgot password template
  362. SwpmUtilsTemplate::swpm_load_template('forgot_password.php', false);
  363. return ob_get_clean();
  364. }
  365. public function profile_form() {
  366. $auth = SwpmAuth::get_instance();
  367. $this->notices();
  368. if ($auth->is_logged_in()) {
  369. $out = apply_filters('swpm_profile_form_override', '');
  370. if (!empty($out)) {
  371. return $out;
  372. }
  373. ob_start();
  374. //Load the edit profile template
  375. SwpmUtilsTemplate::swpm_load_template('edit.php', false);
  376. return ob_get_clean();
  377. }
  378. return SwpmUtils::_('You are not logged in.');
  379. }
  380. /* If any message/notice was set during the execution then this function will output that message */
  381. public function notices() {
  382. $message = SwpmTransfer::get_instance()->get('status');
  383. $succeeded = false;
  384. if (empty($message)) {
  385. return false;
  386. }
  387. if ($message['succeeded']) {
  388. echo "<div id='swpm_message' class='swpm_success'>";
  389. $succeeded = true;
  390. } else {
  391. echo "<div id='swpm_message' class='swpm_error'>";
  392. }
  393. echo $message['message'];
  394. $extra = isset($message['extra']) ? $message['extra'] : array();
  395. if (is_string($extra)) {
  396. echo $extra;
  397. } else if (is_array($extra)) {
  398. echo '<ul>';
  399. foreach ($extra as $key => $value) {
  400. echo '<li>' . $value . '</li>';
  401. }
  402. echo '</ul>';
  403. }
  404. echo "</div>";
  405. if (isset($message['pass_reset_sent'])) {
  406. $succeeded = true;
  407. }
  408. return $succeeded;
  409. }
  410. /*
  411. * This function is hooked to WordPress's admin_notices action hook
  412. * It is used to show any plugin specific notices/warnings in the admin interface
  413. */
  414. public function do_admin_notices() {
  415. $this->notices(); //Show any execution specific notices in the admin interface.
  416. //Show any other general warnings/notices to the admin.
  417. if (SwpmMiscUtils::is_swpm_admin_page()) {
  418. //we are in an admin page for SWPM plugin.
  419. $msg = '';
  420. //Show notice if running in sandbox mode.
  421. $settings = SwpmSettings::get_instance();
  422. $sandbox_enabled = $settings->get_value('enable-sandbox-testing');
  423. if ($sandbox_enabled) {
  424. $msg .= '<p>' . SwpmUtils::_('You have the sandbox payment mode enabled in plugin settings. Make sure to turn off the sandbox mode when you want to do live transactions.') . '</p>';
  425. }
  426. if (!empty($msg)) {//Show warning messages if any.
  427. echo '<div id="message" class="error">';
  428. echo $msg;
  429. echo '</div>';
  430. }
  431. }
  432. }
  433. public function meta_box() {
  434. if (function_exists('add_meta_box')) {
  435. $post_types = get_post_types();
  436. foreach ($post_types as $post_type => $post_type) {
  437. add_meta_box('swpm_sectionid', __('Simple WP Membership Protection', 'simple-membership'), array(&$this, 'inner_custom_box'), $post_type, 'advanced');
  438. }
  439. } else {//older version doesn't have custom post type so modification isn't needed.
  440. add_action('dbx_post_advanced', array(&$this, 'show_old_custom_box'));
  441. add_action('dbx_page_advanced', array(&$this, 'show_old_custom_box'));
  442. }
  443. }
  444. public function show_old_custom_box() {
  445. echo '<div class="dbx-b-ox-wrapper">' . "\n";
  446. echo '<fieldset id="swpm_fieldsetid" class="dbx-box">' . "\n";
  447. echo '<div class="dbx-h-andle-wrapper"><h3 class="dbx-handle">' .
  448. __('Simple Membership Protection options', 'simple-membership') . "</h3></div>";
  449. echo '<div class="dbx-c-ontent-wrapper"><div class="dbx-content">';
  450. // output editing form
  451. $this->inner_custom_box();
  452. // end wrapper
  453. echo "</div></div></fieldset></div>\n";
  454. }
  455. public function inner_custom_box() {
  456. global $post, $wpdb;
  457. $id = $post->ID;
  458. $protection_obj = SwpmProtection::get_instance();
  459. $is_protected = $protection_obj->is_protected($id);
  460. //Nonce input
  461. echo '<input type="hidden" name="swpm_post_protection_box_nonce" value="' . wp_create_nonce('swpm_post_protection_box_nonce_action') . '" />';
  462. // The actual fields for data entry
  463. echo '<h4>' . __("Do you want to protect this content?", 'simple-membership') . '</h4>';
  464. echo '<input type="radio" ' . ((!$is_protected) ? 'checked' : "") . ' name="swpm_protect_post" value="1" /> ' . SwpmUtils::_('No, Do not protect this content.') . '<br/>';
  465. echo '<input type="radio" ' . (($is_protected) ? 'checked' : "") . ' name="swpm_protect_post" value="2" /> ' . SwpmUtils::_('Yes, Protect this content.') . '<br/>';
  466. echo $protection_obj->get_last_message();
  467. echo '<h4>' . __("Select the membership level that can access this content:", 'simple-membership') . "</h4>";
  468. $query = "SELECT * FROM " . $wpdb->prefix . "swpm_membership_tbl WHERE id !=1 ";
  469. $levels = $wpdb->get_results($query, ARRAY_A);
  470. foreach ($levels as $level) {
  471. echo '<input type="checkbox" ' . (SwpmPermission::get_instance($level['id'])->is_permitted($id) ? "checked='checked'" : "") .
  472. ' name="swpm_protection_level[' . $level['id'] . ']" value="' . $level['id'] . '" /> ' . $level['alias'] . "<br/>";
  473. }
  474. }
  475. public function save_postdata($post_id) {
  476. global $wpdb;
  477. $post_type = filter_input(INPUT_POST, 'post_type');
  478. $swpm_protect_post = filter_input(INPUT_POST, 'swpm_protect_post');
  479. if (wp_is_post_revision($post_id)) {
  480. return;
  481. }
  482. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  483. return $post_id;
  484. }
  485. //Check nonce
  486. $swpm_post_protection_box_nonce = filter_input(INPUT_POST, 'swpm_post_protection_box_nonce');
  487. if (!wp_verify_nonce($swpm_post_protection_box_nonce, 'swpm_post_protection_box_nonce_action')) {
  488. //Nonce check failed.
  489. return $post_id;
  490. }
  491. if ('page' == $post_type) {
  492. if (!current_user_can('edit_page', $post_id)) {
  493. return $post_id;
  494. }
  495. } else {
  496. if (!current_user_can('edit_post', $post_id)) {
  497. return $post_id;
  498. }
  499. }
  500. if (empty($swpm_protect_post)) {
  501. return;
  502. }
  503. // OK, we're authenticated: we need to find and save the data
  504. $isprotected = ($swpm_protect_post == 2);
  505. $args = array('swpm_protection_level' => array(
  506. 'filter' => FILTER_VALIDATE_INT,
  507. 'flags' => FILTER_REQUIRE_ARRAY,
  508. ));
  509. $swpm_protection_level = filter_input_array(INPUT_POST, $args);
  510. $swpm_protection_level = $swpm_protection_level['swpm_protection_level'];
  511. if (!empty($post_type)) {
  512. if ($isprotected) {
  513. SwpmProtection::get_instance()->apply(array($post_id), $post_type);
  514. } else {
  515. SwpmProtection::get_instance()->remove(array($post_id), $post_type);
  516. }
  517. SwpmProtection::get_instance()->save();
  518. $query = "SELECT id FROM " . $wpdb->prefix . "swpm_membership_tbl WHERE id !=1 ";
  519. $level_ids = $wpdb->get_col($query);
  520. foreach ($level_ids as $level) {
  521. if (isset($swpm_protection_level[$level])) {
  522. SwpmPermission::get_instance($level)->apply(array($post_id), $post_type)->save();
  523. } else {
  524. SwpmPermission::get_instance($level)->remove(array($post_id), $post_type)->save();
  525. }
  526. }
  527. }
  528. $enable_protection = array();
  529. $enable_protection['protect'] = $swpm_protect_post;
  530. $enable_protection['level'] = $swpm_protection_level;
  531. return $enable_protection;
  532. }
  533. public function filter_comment($content) {
  534. if (is_admin()) {
  535. //Do not apply filtering for admin side viewing
  536. return $content;
  537. }
  538. $acl = SwpmAccessControl::get_instance();
  539. global $comment;
  540. return $acl->filter_comment($comment, $content);
  541. }
  542. public function filter_content($content) {
  543. if (is_preview() || is_admin()) {
  544. //If the user is logged-in as an admin user then do not apply filtering for admin side viewing or preview page viewing.
  545. if ( current_user_can('administrator') ){
  546. //The user is logged in as admin in this browser.
  547. return $content;
  548. }
  549. }
  550. $acl = SwpmAccessControl::get_instance();
  551. global $post;
  552. return $acl->filter_post($post, $content);
  553. }
  554. public function init_hook() {
  555. $init_tasks = new SwpmInitTimeTasks();
  556. $init_tasks->do_init_tasks();
  557. }
  558. public function handle_wp_loaded_tasks() {
  559. $wp_loaded_tasks = new SwpmWpLoadedTasks();
  560. $wp_loaded_tasks->do_wp_loaded_tasks();
  561. }
  562. public function admin_library() {
  563. //Only loaded on selective swpm admin menu page rendering.
  564. $this->common_library();
  565. wp_enqueue_script('password-strength-meter');
  566. wp_enqueue_script('swpm.password-meter', SIMPLE_WP_MEMBERSHIP_URL . '/js/swpm.password-meter.js', array('jquery'), SIMPLE_WP_MEMBERSHIP_VER);
  567. //jQuery UI style
  568. wp_register_style('swpm-jquery-ui', SIMPLE_WP_MEMBERSHIP_URL . '/css/jquery-ui.min.css', array(), SIMPLE_WP_MEMBERSHIP_VER);
  569. wp_enqueue_style('swpm-jquery-ui');
  570. wp_enqueue_script('jquery-ui-datepicker');
  571. $settings = array('statusChangeEmailHead' => SwpmSettings::get_instance()->get_value('account-change-email-subject'),
  572. 'statusChangeEmailBody' => SwpmSettings::get_instance()->get_value('account-change-email-body'));
  573. wp_localize_script('swpm.password-meter', 'SwpmSettings', $settings);
  574. }
  575. public function front_library() {
  576. $this->common_library();
  577. }
  578. private function common_library() {
  579. wp_enqueue_script('jquery');
  580. wp_enqueue_style('swpm.common', SIMPLE_WP_MEMBERSHIP_URL . '/css/swpm.common.css', array(), SIMPLE_WP_MEMBERSHIP_VER);
  581. //In order to not clog WP with scripts and styles we're only using with forms, let's just register those for now
  582. //Scripts will be queued when forms are actually displayed
  583. wp_register_style('validationEngine.jquery', SIMPLE_WP_MEMBERSHIP_URL . '/css/validationEngine.jquery.css', array(), SIMPLE_WP_MEMBERSHIP_VER);
  584. wp_register_script('jquery.validationEngine', SIMPLE_WP_MEMBERSHIP_URL . '/js/jquery.validationEngine.js', array('jquery'), SIMPLE_WP_MEMBERSHIP_VER);
  585. wp_register_script('jquery.validationEngine-en', SIMPLE_WP_MEMBERSHIP_URL . '/js/jquery.validationEngine-en.js', array('jquery'), SIMPLE_WP_MEMBERSHIP_VER);
  586. wp_register_script('swpm.validationEngine-localization', SIMPLE_WP_MEMBERSHIP_URL . '/js/swpm.validationEngine-localization.js', array('jquery'), SIMPLE_WP_MEMBERSHIP_VER);
  587. }
  588. public static function enqueue_validation_scripts($add_params = array()) {
  589. //Localization for jquery.validationEngine
  590. //This array will be merged with $.validationEngineLanguage.allRules object from jquery.validationEngine-en.js file
  591. $loc_data = array(
  592. 'ajaxUserCall' => array(
  593. 'url' => admin_url('admin-ajax.php'),
  594. 'alertTextLoad' => '* ' . SwpmUtils::_('Validating, please wait'),
  595. ),
  596. 'ajaxEmailCall' => array(
  597. 'url' => admin_url('admin-ajax.php'),
  598. 'alertTextLoad' => '* ' . SwpmUtils::_('Validating, please wait'),
  599. ),
  600. 'email' => array(
  601. 'alertText' => '* ' . SwpmUtils::_('Invalid email address'),
  602. ),
  603. 'required' => array(
  604. 'alertText' => '* ' . SwpmUtils::_('This field is required'),
  605. ),
  606. 'strongPass' => array(
  607. 'alertText' => '* ' . SwpmUtils::_('Password must contain at least:').'<br>'.SwpmUtils::_('- a digit').'<br>'.SwpmUtils::_('- an uppercase letter').'<br>'.SwpmUtils::_('- a lowercase letter'),
  608. ),
  609. 'SWPMUserName' => array(
  610. 'alertText' => '* ' . SwpmUtils::_('Invalid Username').'<br>'.SwpmUtils::_('Usernames can only contain: letters, numbers and .-_*@'),
  611. ),
  612. 'minSize' => array(
  613. 'alertText' => '* ' . SwpmUtils::_('Minimum '),
  614. 'alertText2' => SwpmUtils::_(' characters required'),
  615. ),
  616. 'noapostrophe' => array(
  617. 'alertText' => '* ' . SwpmUtils::_('Apostrophe character is not allowed'),
  618. ),
  619. );
  620. $nonce=wp_create_nonce( 'swpm-rego-form-ajax-nonce' );
  621. if ($add_params) {
  622. // Additional parameters should be added to the array, replacing existing ones
  623. if (isset($add_params['ajaxEmailCall'])) {
  624. if (isset($add_params['ajaxEmailCall']['extraData'])) {
  625. $add_params['ajaxEmailCall']['extraData'].='&nonce='.$nonce;
  626. }
  627. }
  628. $loc_data = array_replace_recursive($add_params, $loc_data);
  629. }
  630. wp_localize_script('swpm.validationEngine-localization', 'swpm_validationEngine_localization', $loc_data);
  631. wp_localize_script('jquery.validationEngine-en', 'swpmRegForm', array('nonce' => $nonce));
  632. wp_enqueue_style('validationEngine.jquery');
  633. wp_enqueue_script('jquery.validationEngine');
  634. wp_enqueue_script('jquery.validationEngine-en');
  635. wp_enqueue_script('swpm.validationEngine-localization');
  636. }
  637. public function registration_form($atts) {
  638. $succeeded = $this->notices();
  639. if ($succeeded) {
  640. return;
  641. }
  642. $is_free = SwpmSettings::get_instance()->get_value('enable-free-membership');
  643. $free_level = absint(SwpmSettings::get_instance()->get_value('free-membership-id'));
  644. $level = isset($atts['level']) ? absint($atts['level']) : ($is_free ? $free_level : null);
  645. return SwpmFrontRegistration::get_instance()->regigstration_ui($level);
  646. }
  647. public function menu() {
  648. $menu_parent_slug = 'simple_wp_membership';
  649. add_menu_page(__("WP Membership", 'simple-membership'), __("WP Membership", 'simple-membership'), SWPM_MANAGEMENT_PERMISSION, $menu_parent_slug, array(&$this, "admin_members_menu"), 'dashicons-id');
  650. add_submenu_page($menu_parent_slug, __("Members", 'simple-membership'), __('Members', 'simple-membership'), SWPM_MANAGEMENT_PERMISSION, 'simple_wp_membership', array(&$this, "admin_members_menu"));
  651. add_submenu_page($menu_parent_slug, __("Membership Levels", 'simple-membership'), __("Membership Levels", 'simple-membership'), SWPM_MANAGEMENT_PERMISSION, 'simple_wp_membership_levels', array(&$this, "admin_membership_levels_menu"));
  652. add_submenu_page($menu_parent_slug, __("Settings", 'simple-membership'), __("Settings", 'simple-membership'), SWPM_MANAGEMENT_PERMISSION, 'simple_wp_membership_settings', array(&$this, "admin_settings_menu"));
  653. add_submenu_page($menu_parent_slug, __("Payments", 'simple-membership'), __("Payments", 'simple-membership'), SWPM_MANAGEMENT_PERMISSION, 'simple_wp_membership_payments', array(&$this, "admin_payments_menu"));
  654. add_submenu_page($menu_parent_slug, __("Add-ons", 'simple-membership'), __("Add-ons", 'simple-membership'), SWPM_MANAGEMENT_PERMISSION, 'simple_wp_membership_addons', array(&$this, "admin_add_ons_menu"));
  655. do_action('swpm_after_main_admin_menu', $menu_parent_slug);
  656. $this->meta_box();
  657. }
  658. /* Render the members menu in admin dashboard */
  659. public function admin_members_menu() {
  660. include_once(SIMPLE_WP_MEMBERSHIP_PATH . 'classes/class.swpm-members.php');
  661. $members = new SwpmMembers();
  662. $members->handle_main_members_admin_menu();
  663. }
  664. /* Render the membership levels menu in admin dashboard */
  665. public function admin_membership_levels_menu() {
  666. include_once(SIMPLE_WP_MEMBERSHIP_PATH . 'classes/class.swpm-membership-levels.php');
  667. $levels = new SwpmMembershipLevels();
  668. $levels->handle_main_membership_level_admin_menu();
  669. }
  670. /* Render the settings menu in admin dashboard */
  671. public function admin_settings_menu() {
  672. $settings = SwpmSettings::get_instance();
  673. $settings->handle_main_settings_admin_menu();
  674. }
  675. public function admin_payments_menu() {
  676. include_once(SIMPLE_WP_MEMBERSHIP_PATH . 'classes/admin-includes/class.swpm-payments-admin-menu.php');
  677. $payments_admin = new SwpmPaymentsAdminMenu();
  678. $payments_admin->handle_main_payments_admin_menu();
  679. }
  680. public function admin_add_ons_menu() {
  681. include(SIMPLE_WP_MEMBERSHIP_PATH . 'views/admin_add_ons_page.php');
  682. }
  683. public function plugins_loaded() {
  684. //Runs when plugins_loaded action gets fired
  685. if (is_admin()) {
  686. //Check and run DB upgrade operation (if needed)
  687. if (get_option('swpm_db_version') != SIMPLE_WP_MEMBERSHIP_DB_VER) {
  688. include_once('class.swpm-installation.php');
  689. SwpmInstallation::run_safe_installer();
  690. }
  691. }
  692. }
  693. public static function activate() {
  694. wp_schedule_event(time(), 'daily', 'swpm_account_status_event');
  695. wp_schedule_event(time(), 'daily', 'swpm_delete_pending_account_event');
  696. include_once('class.swpm-installation.php');
  697. SwpmInstallation::run_safe_installer();
  698. }
  699. public static function deactivate() {
  700. wp_clear_scheduled_hook('swpm_account_status_event');
  701. wp_clear_scheduled_hook('swpm_delete_pending_account_event');
  702. }
  703. }