PageRenderTime 36ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wysija-newsletters/helpers/wp_tools.php

https://gitlab.com/Gashler/sg
PHP | 327 lines | 221 code | 44 blank | 62 comment | 30 complexity | b37113c4d427eb7e43b189a6f725e78c MD5 | raw file
  1. <?php
  2. defined('WYSIJA') or die('Restricted access');
  3. class WYSIJA_help_wp_tools extends WYSIJA_object{
  4. function WYSIJA_help_wp_tools(){
  5. }
  6. /**
  7. * add wysija's default capabilities to the admin and super admin roles
  8. */
  9. function set_default_rolecaps(){
  10. //add role capabilities
  11. //get the "administrator" role object
  12. $rolesadmin=array('administrator','super_admin');
  13. foreach($rolesadmin as $roladm){
  14. $role = get_role($roladm);
  15. if(!$role) continue;
  16. //add wysija's capabilities to it so that other widgets can reuse it
  17. $arr=array('wysija_newsletters','wysija_subscribers','wysija_config','wysija_theme_tab','wysija_style_tab', 'wysija_stats_dashboard');
  18. foreach($arr as $arrkey){
  19. if(!$role->has_cap($arrkey)) $role->add_cap( $arrkey );
  20. }
  21. }
  22. }
  23. /**
  24. * get an array of WordPress roles with a numbered index
  25. * @global type $wp_roles
  26. * @return array
  27. */
  28. function wp_get_roles() {
  29. //Careful WordPress global
  30. global $wp_roles;
  31. $all_roles = $wp_roles->roles;
  32. $editable_roles = apply_filters('editable_roles', $all_roles);
  33. $rolearray=array();
  34. $sum=6;
  35. foreach($editable_roles as $keyrol => $roledetails){
  36. switch($keyrol){
  37. case 'super_admin':
  38. $index=1;
  39. break;
  40. case 'administrator':
  41. $index=2;
  42. break;
  43. case 'editor':
  44. $index=3;
  45. break;
  46. case 'author':
  47. $index=4;
  48. break;
  49. case 'contributor':
  50. $index=5;
  51. break;
  52. case 'subscriber':
  53. $index=6;
  54. break;
  55. default:
  56. $sum++;
  57. $index=$sum;
  58. }
  59. $rolearray[$index]=array('key'=>$keyrol,'name'=>$roledetails['name']);
  60. }
  61. ksort($rolearray);
  62. return $rolearray;
  63. }
  64. /**
  65. * get an array of WordPress roles with a special capability of that role as index
  66. * @global type $wp_roles
  67. * @return array
  68. */
  69. function wp_get_editable_roles() {
  70. //Careful WordPress global
  71. global $wp_roles;
  72. $all_roles = $wp_roles->roles;
  73. $editable_roles = apply_filters('editable_roles', $all_roles);
  74. $possible_values=array();
  75. foreach ( $all_roles as $role => $details ) {
  76. $name = translate_user_role($details['name'] );
  77. switch($role){
  78. case 'administrator':
  79. $keyrole='switch_themes';
  80. break;
  81. case 'editor':
  82. $keyrole='moderate_comments';
  83. break;
  84. case 'author':
  85. $keyrole='upload_files';
  86. break;
  87. case 'contributor':
  88. $keyrole='edit_posts';
  89. break;
  90. case 'subscriber':
  91. $keyrole='read';
  92. break;
  93. default:
  94. $keyrole=$role;
  95. }
  96. $possible_values[$keyrole]=$name;
  97. //$possible_values[key($details['capabilities'])]=$name;
  98. }
  99. return $possible_values;
  100. }
  101. /**
  102. * get roles by name ? Not so sure why use that function
  103. * @global type $wp_roles
  104. * @return array
  105. */
  106. function wp_get_all_roles() {
  107. //Careful WordPress global
  108. global $wp_roles;
  109. $all_roles = $wp_roles->get_names();
  110. return $all_roles;
  111. }
  112. /**
  113. * check whether there is a caching plugin active on this site, we were using that function at some point, it can be useful
  114. * @global type $cache_enabled
  115. * @global type $super_cache_enabled
  116. * @return boolean
  117. */
  118. function is_caching_active(){
  119. $checkPlugins=array(
  120. 'wp-super-cache/wp-cache.php' ,
  121. 'w3-total-cache/w3-total-cache.php',
  122. 'quick-cache/quick-cache.php',
  123. 'hyper-cache/plugin.php'
  124. );
  125. foreach($checkPlugins as $pluginFileName){
  126. if(WYSIJA::is_plugin_active($pluginFileName)){
  127. switch($pluginFileName){
  128. case 'wp-super-cache/wp-cache.php':
  129. global $cache_enabled, $super_cache_enabled;
  130. if(!(WP_CACHE && $cache_enabled && $super_cache_enabled)) continue(2);
  131. break;
  132. case 'w3-total-cache/w3-total-cache.php':
  133. $config = & w3_instance("W3_Config");
  134. if(!(WP_CACHE && $config->get_boolean("pgcache.enabled"))) continue(2);
  135. break;
  136. case 'quick-cache/quick-cache.php':
  137. if(!(WP_CACHE && $GLOBALS["WS_PLUGIN__"]["qcache"]["o"]["enabled"])) continue(2);
  138. break;
  139. case 'hyper-cache/plugin.php':
  140. if(!(WP_CACHE)) continue(2);
  141. break;
  142. default:
  143. continue(2);
  144. }
  145. return true;
  146. }
  147. }
  148. return false;
  149. }
  150. /**
  151. * extends the get_permalink of WordPress since at the beginning we had a lot of problems with people who didn't have pretty urls activated etc..
  152. * @param int $pageid
  153. * @param array $params pass an array of parameters to the url
  154. * @param boolean $simple leading to the home not sure in which case we need that again
  155. * @return string
  156. */
  157. function get_permalink($pageid,$params=array(),$simple=false){
  158. $post = get_post($pageid);
  159. $url=get_permalink($post);
  160. if(!$url){
  161. //we need to recreate the subscription page
  162. $values=array();
  163. $helperInstall=WYSIJA::get('install','helper');
  164. $helperInstall->createPage($values);
  165. $modelConf=WYSIJA::get('config','model');
  166. $modelConf->save($values);
  167. $post = get_post($values['confirm_email_link']);
  168. $url=get_permalink($post);
  169. }
  170. $paramsquery=parse_url($url);
  171. if($params!==false) $params[$post->post_type]=$post->post_name;
  172. //make a simple url leading to the home
  173. if($simple){
  174. $url=site_url();
  175. // make sure we have a trailing slash at the end
  176. if($url{strlen($url) - 1} !== '/') $url .= '/';
  177. }
  178. if(isset($paramsquery['query'])){
  179. $myparams=explode('&',$paramsquery['query']);
  180. //get the param from the url obtain in permalink and transfer it to our url
  181. foreach($myparams as $paramvalu){
  182. $splitkeyval=explode('=',$paramvalu);
  183. $params[$splitkeyval[0]]=$splitkeyval[1];
  184. }
  185. }
  186. // make sure we include the port if it's specified
  187. if(isset($paramsquery['port']) && strlen(trim($paramsquery['port'])) > 0) {
  188. $port = ':'.(int)$paramsquery['port'];
  189. } else {
  190. $port = '';
  191. }
  192. // build url
  193. $url = sprintf('%s://%s%s%s', $paramsquery['scheme'], $paramsquery['host'], $port, $paramsquery['path']);
  194. if($params) {
  195. if(strpos($url, '?') !== false) $charStart='&';
  196. else $charStart='?';
  197. $url.=$charStart;
  198. $paramsinline=array();
  199. foreach($params as $k => $v){
  200. if(is_array($v)) $v = http_build_query(array($k => $v));
  201. $paramsinline[]=$k.'='.$v;
  202. }
  203. $url.=implode('&',$paramsinline);
  204. }
  205. // Transform relative URLs in Absolute URLs (Protect from external URL transforming plugins)
  206. $parsed_url = parse_url($url);
  207. if (empty($parsed_url['scheme'])) {
  208. $url = get_bloginfo('url') . $url;
  209. }
  210. return $url;
  211. }
  212. /**
  213. * return a list of post types
  214. * @return mixed
  215. */
  216. function get_post_types($return_type = 'objects') {
  217. $args=array(
  218. 'public' => true,
  219. '_builtin' => false,
  220. 'show_in_menu'=>true,
  221. 'show_ui'=>true,
  222. );
  223. return get_post_types($args, $return_type);
  224. }
  225. /**
  226. * return a list of post types
  227. * @return mixed
  228. */
  229. function get_post_statuses(){
  230. return array_merge(get_post_statuses(), array('future'=>__('Scheduled',WYSIJA)));
  231. }
  232. /**
  233. * get a multidimensionnal array that returns the categories per CPT
  234. */
  235. function get_categories() {
  236. // For now we wont need the post types as we only "post"
  237. $post_types = get_post_types( array(), 'names' );
  238. // Grab all taxonomies
  239. $taxonomies = get_object_taxonomies( $post_types, 'object' );
  240. // Get all terms from the taxonomies
  241. $terms = get_terms(
  242. array_keys( $taxonomies ),
  243. array(
  244. 'order_by' => 'name',
  245. 'show_count' => false,
  246. 'hide_empty' => false,
  247. )
  248. );
  249. // backwards compatibility later lets use `term_id`
  250. foreach ( $terms as $key => $term ){
  251. $term->id = $term->term_id;
  252. $terms[$key] = (array) $term;
  253. }
  254. // Create a response with the needed structure
  255. $response = array();
  256. // If the Post Type has categories associated it will have the terms
  257. foreach ( $post_types as $post_type ){
  258. foreach( $terms as $term ) {
  259. if ( ! in_array( $post_type, $taxonomies[ $term['taxonomy'] ]->object_type ) ){
  260. continue;
  261. }
  262. $term['name'] = esc_attr( $taxonomies[ $term['taxonomy'] ]->labels->singular_name . ": " . $term['name'] );
  263. $response[ $post_type ][] = $term;
  264. }
  265. if ( isset( $response[ $post_type ] ) && is_array( $response[ $post_type ] ) ) {
  266. usort( $response[ $post_type ], array( $this, 'sort_by_name' ) );
  267. }
  268. }
  269. return $response;
  270. }
  271. function get_post_category_ids( $post = null ) {
  272. if ( ! is_object( $post ) || ! is_a( $post, 'WP_Post' ) ) {
  273. return array();
  274. }
  275. return wp_get_post_terms( $post->ID, get_object_taxonomies( $post->post_type ), array( 'fields' => 'ids' ) );
  276. }
  277. function get_post_categories( $post = null ) {
  278. if ( ! is_array( $post ) ) {
  279. return array();
  280. }
  281. return wp_get_post_terms( $post['ID'], get_object_taxonomies( $post['post_type'] ), array( 'fields' => 'names' ) );
  282. }
  283. }