PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wpclef/includes/class.clef-utils.php

https://bitbucket.org/ChendeyY/docklandsmedia
PHP | 270 lines | 217 code | 13 blank | 40 comment | 12 complexity | 2d59c5f1125ccd821c309ca71dde8b11 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Plugin-wide utility functions
  4. *
  5. * @package Clef
  6. * @since 2.0
  7. */
  8. class ClefUtils {
  9. public static $default_roles = array("Subscriber", "Contributor", "Author", "Editor", "Administrator", "Super Administrator" );
  10. /**
  11. * Runs esc_html on strings. Leaves input untouched if it's not a string.
  12. *
  13. * @return mixed
  14. */
  15. private static function escape_string($maybe_string) {
  16. $escaped = $maybe_string;
  17. if (is_string($maybe_string)) {
  18. $escaped = esc_html($maybe_string);
  19. }
  20. return $escaped;
  21. }
  22. /**
  23. * Renders the specified template, giving it access to $variables.
  24. * Strings are escaped.
  25. *
  26. * @param string $name The name (with no .php extension) of a file in
  27. * templates/.
  28. * @param array $variables A list of variables to be used in the
  29. * template.
  30. * @return string
  31. */
  32. public static function render_template($name, $variables=false, $sanitize=true) {
  33. if ($variables) {
  34. $escaped_variables = $variables;
  35. if ($sanitize) {
  36. $escaped_variables = array_map(array(__CLASS__, 'escape_string'), $variables);
  37. }
  38. extract($escaped_variables, EXTR_SKIP);
  39. }
  40. ob_start();
  41. require(CLEF_TEMPLATE_PATH . $name . '.php');
  42. return ob_get_clean();
  43. }
  44. /**
  45. * Return $_GET[$key] if it exists.
  46. *
  47. * @param string $key
  48. * @return mixed
  49. */
  50. public static function isset_GET($key) {
  51. return isset($_GET[$key]) ? $_GET[$key] : null;
  52. }
  53. /**
  54. * Return $_POST[$key] if it exists.
  55. *
  56. * @param string $key
  57. * @return mixed
  58. */
  59. public static function isset_POST($key) {
  60. return isset($_POST[$key]) ? $_POST[$key] : null;
  61. }
  62. /**
  63. * Return $_REQUEST[$key] if it exists.
  64. *
  65. * @param string $key
  66. * @return mixed
  67. */
  68. public static function isset_REQUEST($key) {
  69. return isset($_REQUEST[$key]) ? $_REQUEST[$key] : null;
  70. }
  71. public static function set_html_content_type() {
  72. return 'text/html';
  73. }
  74. public static function register_script($name, $dependencies=array('jquery')) {
  75. $ident = "wpclef-" . $name;
  76. if (!CLEF_DEBUG) {
  77. $name .= '.min';
  78. }
  79. $name .= '.js';
  80. wp_register_script(
  81. $ident,
  82. CLEF_URL .'assets/dist/js/' . $name,
  83. $dependencies,
  84. CLEF_VERSION,
  85. TRUE
  86. );
  87. wp_localize_script($ident, "clefTranslations", ClefTranslation::javascript());
  88. return $ident;
  89. }
  90. public static function register_style($name) {
  91. $ident = "wpclef-" . $name;
  92. if (!CLEF_DEBUG) {
  93. $name .= '.min';
  94. }
  95. $name .= '.css';
  96. wp_register_style(
  97. $ident,
  98. CLEF_URL . 'assets/dist/css/' . $name,
  99. FALSE,
  100. CLEF_VERSION
  101. );
  102. return $ident;
  103. }
  104. public static function style_has_been_added($name) {
  105. $ident = "wpclef-" . $name;
  106. return wp_style_is($ident, 'enqueued')
  107. || wp_style_is($ident, 'done')
  108. || wp_style_is($ident, 'to_do');
  109. }
  110. public static function script_has_been_added($name) {
  111. $ident = "wpclef-" . $name;
  112. return wp_script_is($ident, 'enqueued')
  113. || wp_script_is($ident, 'done')
  114. || wp_script_is($ident, 'to_do');
  115. }
  116. public static function user_has_clef($user=false) {
  117. # if no user is provided, defaults to current user
  118. if (!$user) $user = wp_get_current_user();
  119. return !!get_user_meta($user->ID, "clef_id", true);
  120. }
  121. public static function associate_clef_id($clef_id, $user_id=false) {
  122. if (!$user_id) {
  123. $user_id = wp_get_current_user()->ID;
  124. }
  125. $user = get_users(array(
  126. 'meta_key' => 'clef_id',
  127. 'meta_value' => $clef_id,
  128. 'blog_id' => false
  129. ));
  130. if (!empty($user)) {
  131. return new WP_Error(
  132. 'clef_id_already_associated',
  133. __("The Clef account you're trying to connect is already associated to a different WordPress account", "clef")
  134. );
  135. }
  136. update_user_meta($user_id, 'clef_id', $clef_id);
  137. }
  138. public static function dissociate_clef_id($user_id=false) {
  139. if (!$user_id) {
  140. $user_id = wp_get_current_user()->ID;
  141. }
  142. delete_user_meta($user_id, "clef_id");
  143. }
  144. public static function exchange_oauth_code_for_info($code, $settings=null, $app_id=false, $app_secret=false) {
  145. ClefUtils::verify_state();
  146. if ($settings) {
  147. if (!$app_id) $app_id = $settings->get( 'clef_settings_app_id' );
  148. if (!$app_secret) $app_secret = $settings->get( 'clef_settings_app_secret' );
  149. }
  150. $args = array(
  151. 'code' => $code,
  152. 'app_id' => $app_id,
  153. 'app_secret' => $app_secret,
  154. );
  155. $response = wp_remote_post( CLEF_API_BASE . 'authorize', array( 'method'=> 'POST', 'body' => $args, 'timeout' => 20 ) );
  156. if ( is_wp_error($response) ) {
  157. throw new LoginException(__( "Something went wrong: ", 'clef' ) . $response->get_error_message());
  158. }
  159. $body = json_decode( $response['body'] );
  160. if ( !isset($body->success) || $body->success != 1 ) {
  161. throw new LoginException(__( 'Error retrieving Clef access token: ', 'clef') . $body->error);
  162. }
  163. $access_token = $body->access_token;
  164. // Get info
  165. $response = wp_remote_get( CLEF_API_BASE . "info?access_token={$access_token}" );
  166. if ( is_wp_error($response) ) {
  167. throw new LoginException(__( "Something went wrong: ", 'clef' ) . $response->get_error_message());
  168. }
  169. $body = json_decode( $response['body'] );
  170. if ( !isset($body->success) || $body->success != 1 ) {
  171. throw new LoginException(__('Error retrieving Clef user data: ', 'clef') . $body->error);
  172. }
  173. return $body->info;
  174. }
  175. public static function user_fulfills_role($user, $role) {
  176. $fulfills_role = false;
  177. $role_map = array(
  178. "subscriber",
  179. "contributor",
  180. "author",
  181. "editor",
  182. "administrator",
  183. "super administrator"
  184. );
  185. foreach ($user->roles as &$user_role) {
  186. $rank = array_search($user_role, $role_map);
  187. if ($rank != 0 && $rank >= array_search($role, $role_map)) {
  188. $fulfills_role = true;
  189. break;
  190. }
  191. }
  192. if ($role == "super administrator" && is_super_admin($user->ID)) {
  193. $fulfills_role = true;
  194. }
  195. return $fulfills_role;
  196. }
  197. public static function get_custom_roles() {
  198. $all_roles = get_editable_roles();
  199. $custom_roles = array();
  200. foreach($all_roles as $role => $role_obj) {
  201. if (isset($role_obj['name'])) {
  202. $role_name = $role_obj['name'];
  203. if (!in_array($role_name, self::$default_roles)) {
  204. $custom_roles[$role] = $role_obj;
  205. }
  206. }
  207. }
  208. return $custom_roles;
  209. }
  210. public static function initialize_state($override = false) {
  211. if (!$override && isset($_COOKIE['_clef_state']) && $_COOKIE['_clef_state']) return;
  212. $state = wp_generate_password(24, false);
  213. @setcookie('_clef_state', $state, (time() + 60 * 60 * 24), '/', '', is_ssl(), true);
  214. $_COOKIE['_clef_state'] = $state;
  215. return $state;
  216. }
  217. public static function get_state() {
  218. if (!isset($$_COOKIE['_clef_state']) || !$_COOKIE['_clef_state']) ClefUtils::initialize_state();
  219. return $_COOKIE['_clef_state'];
  220. }
  221. public static function verify_state() {
  222. $request_state = ClefUtils::isset_GET('state') ? ClefUtils::isset_GET('state') : ClefUtils::isset_POST('state');
  223. $correct_state = ClefUtils::get_state();
  224. if ($request_state && $correct_state && $correct_state == $request_state) {
  225. ClefUtils::initialize_state(true);
  226. return true;
  227. } else {
  228. throw new ClefStateException('The state parameter is not verified. This may be due to this page being cached by another WordPress plugin. Please refresh your page and try again');
  229. }
  230. }
  231. }
  232. ?>