WYSIJA_help_wp_tools

The MailPoet Newsletters WYSIJA help wp tools class.

Defined (1)

The class is defined in the following location(s).

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