PageRenderTime 49ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/strong-testimonials/admin/admin.php

https://bitbucket.org/ishishmarev/violin
PHP | 329 lines | 168 code | 49 blank | 112 comment | 35 complexity | fa5a6c86e2feee83b6395a62455cd045 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * Strong Testimonials admin functions.
  4. *
  5. * 1. Check for required WordPress version.
  6. * 2. Check for plugin update.
  7. * 3. Initialize.
  8. */
  9. /**
  10. * Check for required WordPress version.
  11. */
  12. function wpmtst_version_check() {
  13. global $wp_version;
  14. $require_wp_version = "3.7";
  15. if ( version_compare( $wp_version, $require_wp_version ) == -1 ) {
  16. deactivate_plugins( WPMTST_PLUGIN );
  17. /* translators: %s is the name of the plugin. */
  18. $message = '<h2>' . sprintf( _x( 'Unable to load %s', 'installation', 'strong-testimonials' ), 'Strong Testimonials' ) . '</h2>';
  19. /* translators: %s is a WordPress version number. */
  20. $message .= '<p>' . sprintf( _x( 'This plugin requires <strong>WordPress %s</strong> or higher so it has been deactivated.', 'installation', 'strong-testimonials' ), $require_wp_version ) . '</p>';
  21. $message .= '<p>' . _x( 'Please upgrade WordPress and try again.', 'installation', 'strong-testimonials' ) . '</p>';
  22. $message .= '<p>' . sprintf( _x( 'Back to the WordPress <a href="%s">Plugins page</a>', 'installation', 'strong-testimonials' ), get_admin_url( null, 'plugins.php' ) ) . '</p>';
  23. wp_die( $message );
  24. }
  25. }
  26. add_action( 'admin_init', 'wpmtst_version_check', 1 );
  27. /**
  28. * Check for plugin update.
  29. *
  30. * @since 2.28.4 Before other admin_init actions.
  31. */
  32. function wpmtst_update_check() {
  33. $version = get_option( 'wpmtst_plugin_version', false );
  34. if ( $version == WPMTST_VERSION ) {
  35. return;
  36. }
  37. // Redirect to About page afterwards. On new install or (de)activation only.
  38. if ( false === $version ) {
  39. add_option( 'wpmtst_do_activation_redirect', true );
  40. }
  41. require_once WPMTST_ADMIN . 'class-strong-testimonials-updater.php';
  42. $updater = new Strong_Testimonials_Updater();
  43. $updater->update();
  44. }
  45. add_action( 'admin_init', 'wpmtst_update_check', 5 );
  46. /**
  47. * Initialize.
  48. */
  49. function wpmtst_admin_init() {
  50. /**
  51. * Redirect to About page for new installs only
  52. *
  53. * @since 2.28.4
  54. */
  55. wpmtst_activation_redirect();
  56. /**
  57. * Custom action hooks
  58. *
  59. * @since 1.18.4
  60. */
  61. if ( isset( $_REQUEST['action'] ) && '' != $_REQUEST['action'] ) {
  62. do_action( 'wpmtst_' . $_REQUEST['action'] );
  63. }
  64. }
  65. add_action( 'admin_init', 'wpmtst_admin_init' );
  66. /**
  67. * Custom action link in admin notice.
  68. *
  69. * @since 2.29.0
  70. */
  71. function wpmtst_action_captcha_options_changed() {
  72. wpmtst_delete_admin_notice( 'captcha-options-changed' );
  73. wp_redirect( admin_url( 'edit.php?post_type=wpm-testimonial&page=testimonial-settings&tab=form#captcha-section' ) );
  74. exit;
  75. }
  76. add_action( 'admin_action_captcha-options-changed', 'wpmtst_action_captcha_options_changed' );
  77. /**
  78. * Redirect to About page.
  79. *
  80. * @since 2.28.4
  81. */
  82. function wpmtst_activation_redirect() {
  83. if ( get_option( 'wpmtst_do_activation_redirect', false ) ) {
  84. delete_option( 'wpmtst_do_activation_redirect' );
  85. wp_redirect( admin_url( 'edit.php?post_type=wpm-testimonial&page=about-strong-testimonials' ) );
  86. exit;
  87. }
  88. }
  89. /**
  90. * Are we on a testimonial admin screen?
  91. *
  92. * Used by add-ons too!
  93. *
  94. * @return bool
  95. */
  96. function wpmtst_is_testimonial_screen() {
  97. $screen = get_current_screen();
  98. return ( $screen && 'wpm-testimonial' == $screen->post_type );
  99. }
  100. /**
  101. * Add pending numbers to post types on admin menu.
  102. * Thanks http://wordpress.stackexchange.com/a/105470/32076
  103. *
  104. * @param $menu
  105. *
  106. * @return mixed
  107. */
  108. function wpmtst_pending_indicator( $menu ) {
  109. if ( ! current_user_can( 'edit_posts' ) )
  110. return $menu;
  111. $options = get_option( 'wpmtst_options' );
  112. if ( ! isset( $options['pending_indicator'] ) || ! $options['pending_indicator'] )
  113. return $menu;
  114. $types = array( 'wpm-testimonial' );
  115. $status = 'pending';
  116. foreach ( $types as $type ) {
  117. $num_posts = wp_count_posts( $type, 'readable' );
  118. $pending_count = 0;
  119. if ( ! empty( $num_posts->$status ) )
  120. $pending_count = $num_posts->$status;
  121. if ( $type == 'post' )
  122. $menu_str = 'edit.php';
  123. else
  124. $menu_str = 'edit.php?post_type=' . $type;
  125. foreach ( $menu as $menu_key => $menu_data ) {
  126. if ( $menu_str != $menu_data[2] )
  127. continue;
  128. $menu[ $menu_key ][0] .= " <span class='update-plugins count-$pending_count'><span class='plugin-count'>" . number_format_i18n( $pending_count ) . '</span></span>';
  129. }
  130. }
  131. return $menu;
  132. }
  133. add_filter( 'add_menu_classes', 'wpmtst_pending_indicator' );
  134. /**
  135. * The [restore default] icon.
  136. *
  137. * @param $for
  138. *
  139. * @since 2.18.0
  140. */
  141. function wpmtst_restore_default_icon( $for ) {
  142. if ( !$for ) return;
  143. ?>
  144. <input type="button" class="button secondary restore-default"
  145. title="<?php _e( 'restore default', 'strong-testimonials' ); ?>"
  146. value="&#xf171"
  147. data-for="<?php echo $for; ?>"/>
  148. <?php
  149. }
  150. /**
  151. * Add plugin links.
  152. *
  153. * @param $plugin_meta
  154. * @param $plugin_file
  155. * @param array $plugin_data
  156. * @param string $status
  157. *
  158. * @return array
  159. */
  160. function wpmtst_plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data = array(), $status = '' ) {
  161. if ( $plugin_file == WPMTST_PLUGIN ) {
  162. $plugin_meta[] = sprintf(
  163. '<a class="strong-plugin-link" href="%s" target="_blank" title="%s">%s</a>',
  164. 'https://strongplugins.com/documents/',
  165. __( 'For guides and tutorials', 'strong-testimonials' ),
  166. __( 'Documentation', 'strong-testimonials' ) );
  167. $plugin_meta[] = sprintf(
  168. '<a class="strong-plugin-link" href="%s" target="_blank" title="%s">%s</a>',
  169. 'https://support.strongplugins.com/',
  170. __( 'For direct support requests', 'strong-testimonials' ),
  171. __( 'Support', 'strong-testimonials' ) );
  172. $plugin_meta[] = sprintf(
  173. '<a class="strong-plugin-link" href="%s" target="_blank" title="%s">%s</a>',
  174. 'https://strongplugins.com/',
  175. __( 'Get more features with premium add-ons', 'strong-testimonials' ),
  176. __( 'Add-ons', 'strong-testimonials' ) );
  177. }
  178. return $plugin_meta;
  179. }
  180. add_filter( 'plugin_row_meta', 'wpmtst_plugin_row_meta' , 10, 4 );
  181. /**
  182. * Check for configuration issues when options are updated.
  183. *
  184. * @since 2.24.0
  185. * @param $option
  186. * @param $old_value
  187. * @param $value
  188. */
  189. function wpmtst_updated_option( $option, $old_value, $value ) {
  190. if ( 'wpmtst_' == substr( $option, 0, 7 ) ) {
  191. do_action( 'wpmtst_check_config' );
  192. }
  193. }
  194. add_action( 'updated_option', 'wpmtst_updated_option', 10, 3 );
  195. /**
  196. * Store configuration error.
  197. *
  198. * @since 2.24.0
  199. * @param $key
  200. */
  201. function wpmtst_add_config_error( $key ) {
  202. $errors = get_option( 'wpmtst_config_errors', array() );
  203. $errors[] = $key;
  204. update_option( 'wpmtst_config_errors', array_unique( $errors ) );
  205. wpmtst_add_admin_notice( $key, true );
  206. }
  207. /**
  208. * Delete configuration error.
  209. *
  210. * @since 2.24.0
  211. * @param $key
  212. */
  213. function wpmtst_delete_config_error( $key ) {
  214. $errors = get_option( 'wpmtst_config_errors', array() );
  215. $errors = array_diff( $errors, array ( $key ) );
  216. update_option( 'wpmtst_config_errors', $errors );
  217. wpmtst_delete_admin_notice( $key );
  218. }
  219. /**
  220. * Save a View.
  221. *
  222. * @param $view
  223. * @param string $action
  224. * @usedby Strong_Testimonials_Update:update_views
  225. *
  226. * @return bool|false|int
  227. */
  228. function wpmtst_save_view( $view, $action = 'edit' ) {
  229. global $wpdb;
  230. if ( ! $view ) return false;
  231. $table_name = $wpdb->prefix . 'strong_views';
  232. $serialized = serialize( $view['data'] );
  233. if ( 'add' == $action || 'duplicate' == $action ) {
  234. $sql = "INSERT INTO {$table_name} (name, value) VALUES (%s, %s)";
  235. $sql = $wpdb->prepare( $sql, $view['name'], $serialized );
  236. $wpdb->query( $sql );
  237. $view['id'] = $wpdb->insert_id;
  238. $return = $view['id'];
  239. }
  240. else {
  241. $sql = "UPDATE {$table_name} SET name = %s, value = %s WHERE id = %d";
  242. $sql = $wpdb->prepare( $sql, $view['name'], $serialized, intval( $view['id'] ) );
  243. $wpdb->query( $sql );
  244. $return = $wpdb->last_error ? 0 : 1;
  245. }
  246. do_action( 'wpmtst_view_saved', $view );
  247. return $return;
  248. }
  249. /**
  250. * @param $field
  251. *
  252. * @return mixed
  253. */
  254. function wpmtst_get_field_label( $field ) {
  255. if ( isset( $field['field'] ) ) {
  256. $custom_fields = wpmtst_get_custom_fields();
  257. if ( isset( $custom_fields[ $field['field'] ]['label'] ) ) {
  258. return $custom_fields[ $field['field'] ]['label'];
  259. }
  260. $builtin_fields = wpmtst_get_builtin_fields();
  261. if ( isset( $builtin_fields[ $field['field'] ]['label'] ) ) {
  262. return $builtin_fields[ $field['field'] ]['label'];
  263. }
  264. }
  265. return '';
  266. }
  267. /**
  268. * @param string $field_name
  269. *
  270. * @return mixed
  271. */
  272. function wpmtst_get_field_by_name( $field_name = '' ) {
  273. if ( $field_name ) {
  274. $custom_fields = wpmtst_get_custom_fields();
  275. if ( isset( $custom_fields[ $field_name ] ) ) {
  276. return $custom_fields[ $field_name ];
  277. }
  278. }
  279. return '';
  280. }