PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/class-user-meta-display.php

https://github.com/lamegaurbana2014/user-meta-display
PHP | 597 lines | 313 code | 90 blank | 194 comment | 60 complexity | 358d82d4e4a037098a19a4ba3ff2a51f MD5 | raw file
  1. <?php
  2. /**
  3. * User Meta Display.
  4. *
  5. * @package User_Meta_Display
  6. * @author Myles McNamara <myles@hostt.net>
  7. * @license GPL-2.0+
  8. * @link http://smyl.es
  9. * @copyright 2014 Myles McNamara
  10. */
  11. /**
  12. * Plugin class.
  13. * @package User_Meta_Display
  14. * @author Myles McNamara <myles@hostt.net>
  15. */
  16. class User_Meta_Display {
  17. /**
  18. * @var string
  19. */
  20. const VERSION = '1.2.2';
  21. /**
  22. * @var string
  23. */
  24. protected $plugin_slug = 'user-meta-display';
  25. /**
  26. * @var object
  27. */
  28. protected static $instance = null;
  29. /**
  30. * @var array
  31. */
  32. protected $element_instances = array();
  33. /**
  34. * @var array
  35. */
  36. protected $element_css_once = array();
  37. /**
  38. * @var array
  39. */
  40. protected $elements = array();
  41. /**
  42. * @var string
  43. */
  44. protected $plugin_screen_hook_suffix = null;
  45. /**
  46. * Initialize the plugin by setting localization, filters, and administration functions.
  47. *
  48. */
  49. private function __construct() {
  50. // Load plugin text domain
  51. add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
  52. // Activate plugin when new blog is added
  53. add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) );
  54. // Load admin style sheet and JavaScript.
  55. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_stylescripts' ) );
  56. add_action('wp_footer', array( $this, 'footer_scripts' ) );
  57. // Detect element before rendering the page so that we can enque scripts and styles needed
  58. if(!is_admin()){
  59. add_action( 'wp', array( $this, 'detect_elements' ) );
  60. }
  61. }
  62. /**
  63. * Return an instance of this class.
  64. *
  65. *
  66. * @return object A single instance of this class.
  67. */
  68. public static function get_instance() {
  69. // If the single instance hasn't been set, set it now.
  70. if ( null == self::$instance ) {
  71. self::$instance = new self;
  72. }
  73. return self::$instance;
  74. }
  75. /**
  76. * Fired when the plugin is activated.
  77. *
  78. *
  79. * @param boolean $network_wide True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog.
  80. */
  81. public static function activate( $network_wide ) {
  82. if ( function_exists( 'is_multisite' ) && is_multisite() ) {
  83. if ( $network_wide ) {
  84. // Get all blog ids
  85. $blog_ids = self::get_blog_ids();
  86. foreach ( $blog_ids as $blog_id ) {
  87. switch_to_blog( $blog_id );
  88. self::single_activate();
  89. }
  90. restore_current_blog();
  91. } else {
  92. self::single_activate();
  93. }
  94. } else {
  95. self::single_activate();
  96. }
  97. }
  98. /**
  99. * Fired when the plugin is deactivated.
  100. *
  101. *
  102. * @param boolean $network_wide True if WPMU superadmin uses "Network Deactivate" action, false if WPMU is disabled or plugin is deactivated on an individual blog.
  103. */
  104. public static function deactivate( $network_wide ) {
  105. if ( function_exists( 'is_multisite' ) && is_multisite() ) {
  106. if ( $network_wide ) {
  107. // Get all blog ids
  108. $blog_ids = self::get_blog_ids();
  109. foreach ( $blog_ids as $blog_id ) {
  110. switch_to_blog( $blog_id );
  111. self::single_deactivate();
  112. }
  113. restore_current_blog();
  114. } else {
  115. self::single_deactivate();
  116. }
  117. } else {
  118. self::single_deactivate();
  119. }
  120. }
  121. /**
  122. * Fired when a new site is activated with a WPMU environment.
  123. *
  124. *
  125. * @param int $blog_id ID of the new blog.
  126. */
  127. public function activate_new_site( $blog_id ) {
  128. if ( 1 !== did_action( 'wpmu_new_blog' ) )
  129. return;
  130. switch_to_blog( $blog_id );
  131. self::single_activate();
  132. restore_current_blog();
  133. }
  134. /**
  135. * Get all blog ids of blogs in the current network that are:
  136. * - not archived
  137. * - not spam
  138. * - not deleted
  139. *
  140. *
  141. * @return array|false The blog ids, false if no matches.
  142. */
  143. private static function get_blog_ids() {
  144. global $wpdb;
  145. // get an array of blog ids
  146. $sql = "SELECT blog_id FROM $wpdb->blogs
  147. WHERE archived = '0' AND spam = '0'
  148. AND deleted = '0'";
  149. return $wpdb->get_col( $sql );
  150. }
  151. /**
  152. * Fired for each blog when the plugin is activated.
  153. *
  154. */
  155. private static function single_activate() {
  156. // TODO: Define activation functionality here if needed
  157. }
  158. /**
  159. * Fired for each blog when the plugin is deactivated.
  160. *
  161. */
  162. private static function single_deactivate() {
  163. // TODO: Define deactivation functionality here needed
  164. }
  165. /**
  166. * Load the plugin text domain for translation.
  167. *
  168. */
  169. public function load_plugin_textdomain() {
  170. // TODO: Add translations as need in /languages
  171. $domain = $this->plugin_slug;
  172. $locale = apply_filters( 'plugin_locale', get_locale(), $domain );
  173. load_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . $domain . '/' . $domain . '-' . $locale . '.mo' );
  174. load_plugin_textdomain( $domain, FALSE, basename( dirname( __FILE__ ) ) . '/languages' );
  175. }
  176. /**
  177. * Register and enqueue admin-specific style sheet.
  178. *
  179. *
  180. * @return null
  181. */
  182. public function enqueue_admin_stylescripts() {
  183. $screen = get_current_screen();
  184. if( false !== strpos( $screen->base, 'user_meta_display' ) ){
  185. }
  186. if ( ! isset( $this->plugin_screen_hook_suffix ) ) {
  187. return;
  188. }
  189. if ( in_array( $screen->id, $this->plugin_screen_hook_suffix ) ) {
  190. $slug = array_search( $screen->id, $this->plugin_screen_hook_suffix );
  191. //$configfiles = glob( self::get_path( __FILE__ ) .'configs/'.$slug.'-*.php' );
  192. if(file_exists(self::get_path( __FILE__ ) .'configs/fieldgroups-'.$slug.'.php')){
  193. include self::get_path( __FILE__ ) .'configs/fieldgroups-'.$slug.'.php';
  194. }else{
  195. return;
  196. }
  197. if( !empty( $configfiles ) ) {
  198. // Always good to have.
  199. wp_enqueue_media();
  200. wp_enqueue_script('media-upload');
  201. foreach ($configfiles as $key=>$fieldfile) {
  202. include $fieldfile;
  203. if(!empty($group['scripts'])){
  204. foreach($group['scripts'] as $script){
  205. if( is_array( $script ) ){
  206. foreach($script as $remote=>$location){
  207. $infoot = false;
  208. if($location == 'footer'){
  209. $infoot = true;
  210. }
  211. if( false !== strpos($remote, '.')){
  212. wp_enqueue_script( $this->plugin_slug . '-' . strtok(basename($remote), '.'), $remote , array('jquery'), false, $infoot );
  213. }else{
  214. wp_enqueue_script( $remote, false , array(), false, $infoot );
  215. }
  216. }
  217. }else{
  218. if( false !== strpos($script, '.')){
  219. wp_enqueue_script( $this->plugin_slug . '-' . strtok($script, '.'), self::get_url( 'assets/js/'.$script , __FILE__ ) , array('jquery'), false, false );
  220. }else{
  221. wp_enqueue_script( $script );
  222. }
  223. }
  224. }
  225. }
  226. if(!empty($group['styles'])){
  227. foreach($group['styles'] as $style){
  228. if( is_array( $style ) ){
  229. foreach($style as $remote){
  230. wp_enqueue_style( $this->plugin_slug . '-' . strtok(basename($remote), '.'), $remote );
  231. }
  232. }else{
  233. wp_enqueue_style( $this->plugin_slug . '-' . strtok($style, '.'), self::get_url( 'assets/css/'.$style , __FILE__ ) );
  234. }
  235. }
  236. }
  237. }
  238. }
  239. wp_enqueue_style( $this->plugin_slug .'-admin-styles', self::get_url( 'assets/css/panel.css', __FILE__ ), array(), self::VERSION );
  240. wp_enqueue_script( $this->plugin_slug .'-admin-scripts', self::get_url( 'assets/js/panel.js', __FILE__ ), array(), self::VERSION );
  241. }
  242. }
  243. /**
  244. * Process a field value
  245. *
  246. */
  247. public function process_value($type, $value){
  248. switch ($type){
  249. default:
  250. return $value;
  251. break;
  252. }
  253. return $value;
  254. }
  255. /**
  256. * setup meta boxes.
  257. *
  258. *
  259. * @return null
  260. */
  261. public function get_post_meta($id, $key = null, $single = false){
  262. if(!empty($key)){
  263. //$configfiles = glob(self::get_path( __FILE__ ) .'configs/*.php');
  264. if(file_exists(self::get_path( __FILE__ ) .'configs/fieldgroups-user_meta_display.php')){
  265. include self::get_path( __FILE__ ) .'configs/fieldgroups-user_meta_display.php';
  266. }else{
  267. return;
  268. }
  269. $field_type = 'text';
  270. foreach( $configfiles as $config=>$file ){
  271. include $file;
  272. if(isset($group['fields'][$key]['type'])){
  273. $field_type = $group['fields'][$key]['type'];
  274. break;
  275. }
  276. }
  277. $key = 'user_meta_display_' . $key;
  278. }
  279. if( false === $single){
  280. $metas = get_post_meta( $id, $key );
  281. foreach ($metas as $key => &$value) {
  282. $value = $this->process_value( $field_type, $value );
  283. }
  284. return $metas;
  285. }
  286. return $this->process_value( $field_type, get_post_meta( $id, $key, $single ) );
  287. }
  288. /**
  289. * save metabox data
  290. *
  291. *
  292. */
  293. function save_post_metaboxes($pid, $post){
  294. if(!isset($_POST['user_meta_display_metabox']) || !isset($_POST['user_meta_display_metabox_prefix'])){return;}
  295. if(!wp_verify_nonce($_POST['user_meta_display_metabox'], plugin_basename(__FILE__))){
  296. return $post->ID;
  297. }
  298. if(!current_user_can( 'edit_post', $post->ID)){
  299. return $post->ID;
  300. }
  301. if($post->post_type == 'revision' ){return;}
  302. foreach( $_POST['user_meta_display_metabox_prefix'] as $prefix ){
  303. if(!isset($_POST[$prefix])){continue;}
  304. delete_post_meta($post->ID, $prefix);
  305. add_post_meta($post->ID, $prefix, $_POST[$prefix]);
  306. //foreach($_POST['user_meta_display'] as $field=>$data){
  307. //if(is_array($data)){
  308. // foreach($data as $item){
  309. // add_post_meta($post->ID, 'user_meta_display_'.$field, $item);
  310. // }
  311. //}else{
  312. //}
  313. //}
  314. }
  315. }
  316. /**
  317. * create and register an instance ID
  318. *
  319. */
  320. public function element_instance_id($id, $process){
  321. $this->element_instances[$id][$process][] = true;
  322. $count = count($this->element_instances[$id][$process]);
  323. if($count > 1){
  324. return $id.($count-1);
  325. }
  326. return $id;
  327. }
  328. /**
  329. * Render the element
  330. *
  331. */
  332. public function render_element($atts, $content, $slug, $head = false) {
  333. $raw_atts = $atts;
  334. if(!empty($head)){
  335. $instanceID = $this->element_instance_id('user_meta_display'.$slug, 'header');
  336. }else{
  337. $instanceID = $this->element_instance_id('user_meta_display'.$slug, 'footer');
  338. }
  339. //$configfiles = glob(self::get_path( __FILE__ ) .'configs/'.$slug.'-*.php');
  340. if(file_exists(self::get_path( __FILE__ ) .'configs/fieldgroups-'.$slug.'.php')){
  341. include self::get_path( __FILE__ ) .'configs/fieldgroups-'.$slug.'.php';
  342. $defaults = array();
  343. foreach($configfiles as $file){
  344. include $file;
  345. foreach($group['fields'] as $variable=>$conf){
  346. if(!empty($group['multiple'])){
  347. $value = array($this->process_value($conf['type'],$conf['default']));
  348. }else{
  349. $value = $this->process_value($conf['type'],$conf['default']);
  350. }
  351. if(!empty($group['multiple'])){
  352. if(isset($atts[$variable.'_1'])){
  353. $index = 1;
  354. $value=array();
  355. while(isset($atts[$variable.'_'.$index])){
  356. $value[] = $this->process_value($conf['type'],$atts[$variable.'_'.$index]);
  357. $index++;
  358. }
  359. }elseif (isset($atts[$variable])) {
  360. if(is_array($atts[$variable])){
  361. foreach($atts[$variable] as &$varval){
  362. $varval = $this->process_value($conf['type'],$varval);
  363. }
  364. $value = $atts[$variable];
  365. }else{
  366. $value[] = $this->process_value($conf['type'],$atts[$variable]);
  367. }
  368. }
  369. }else{
  370. if(isset($atts[$variable])){
  371. $value = $this->process_value($conf['type'],$atts[$variable]);
  372. }
  373. }
  374. if(!empty($group['multiple']) && !empty($value)){
  375. foreach($value as $key=>$val){
  376. //if(is_array($val)){
  377. $groups[$group['master']][$key][$variable] = $val;
  378. //}elseif(strlen($val) > 0){
  379. // $groups[$group['master']][$key][$variable] = $val;
  380. //}
  381. }
  382. }
  383. $defaults[$variable] = $value;
  384. /*if(is_array($value)){
  385. foreach($value as $varkey=>&$varval){
  386. if(is_array($val)){
  387. if(!empty($val)){
  388. unset($value[$varkey]);
  389. }
  390. }elseif(strlen($varval) === 0){
  391. unset($value[$varkey]);
  392. }
  393. }
  394. if(!empty($value)){
  395. $defaults[$variable] = implode(', ', $value);
  396. }
  397. }else{
  398. if(strlen($value) > 0){
  399. $defaults[$variable] = $value;
  400. }
  401. }*/
  402. }
  403. }
  404. //dump($atts,0);
  405. //dump($defaults,0);
  406. $atts = $defaults;
  407. }
  408. // pull in the assets
  409. $assets = array();
  410. if(file_exists(self::get_path( __FILE__ ) . 'assets/assets-'.$slug.'.php')){
  411. include self::get_path( __FILE__ ) . 'assets/assets-'.$slug.'.php';
  412. }
  413. ob_start();
  414. if(file_exists(self::get_path( __FILE__ ) . 'includes/element-'.$slug.'.php')){
  415. include self::get_path( __FILE__ ) . 'includes/element-'.$slug.'.php';
  416. }else if( file_exists(self::get_path( __FILE__ ) . 'includes/element-'.$slug.'.html')){
  417. include self::get_path( __FILE__ ) . 'includes/element-'.$slug.'.html';
  418. }
  419. $out = ob_get_clean();
  420. if(!empty($head)){
  421. // process headers - CSS
  422. if(file_exists(self::get_path( __FILE__ ) . 'assets/css/styles-'.$slug.'.php')){
  423. ob_start();
  424. include self::get_path( __FILE__ ) . 'assets/css/styles-'.$slug.'.php';
  425. $this->element_header_styles[] = ob_get_clean();
  426. add_action('wp_head', array( $this, 'header_styles' ) );
  427. }else if( file_exists(self::get_path( __FILE__ ) . 'assets/css/styles-'.$slug.'.css')){
  428. wp_enqueue_style( $this->plugin_slug . '-'.$slug.'-styles', self::get_url( 'assets/css/styles-'.$slug.'.css', __FILE__ ), array(), self::VERSION );
  429. }
  430. // process headers - JS
  431. if(file_exists(self::get_path( __FILE__ ) . 'assets/js/scripts-'.$slug.'.php')){
  432. ob_start();
  433. include self::get_path( __FILE__ ) . 'assets/js/scripts-'.$slug.'.php';
  434. $this->element_footer_scripts[] = ob_get_clean();
  435. }else if( file_exists(self::get_path( __FILE__ ) . 'assets/js/scripts-'.$slug.'.js')){
  436. wp_enqueue_script( $this->plugin_slug . '-'.$slug.'-script', self::get_url( 'assets/js/scripts-'.$slug.'.js', __FILE__ ), array( 'jquery' ), self::VERSION , true );
  437. }
  438. // get clean do shortcode for header checking
  439. ob_start();
  440. do_shortcode( $out );
  441. ob_get_clean();
  442. return;
  443. }
  444. // CHECK FOR EMBEDED ELEMENTS
  445. /*foreach($elements as $subelement){
  446. if(empty($subelement['state']) || $subelement['shortcode'] == $element['_shortcode']){continue;}
  447. if(false !== strpos($out, '{{:'.$subelement['shortcode'].':}}')){
  448. $out = str_replace('{{:'.$subelement['shortcode'].':}}', caldera_doShortcode(array(), $out, $subelement['shortcode']), $out);
  449. }
  450. }*/
  451. /*if(!empty($element['_removelinebreaks'])){
  452. add_filter( 'the_content', 'wpautop' );
  453. }*/
  454. return do_shortcode($out);
  455. }
  456. /**
  457. * Detect elements used on the page to allow us to enqueue needed styles and scripts
  458. *
  459. */
  460. public function detect_elements() {
  461. global $wp_query;
  462. $this->render_element(get_option( "_user_meta_display_options" ), false, 'user_meta_display', true);
  463. }
  464. /**
  465. * Render any header styles
  466. *
  467. */
  468. public function header_styles() {
  469. if(!empty($this->element_header_styles)){
  470. echo "<style type=\"text/css\">\r\n";
  471. foreach($this->element_header_styles as $styles){
  472. echo $styles."\r\n";
  473. }
  474. echo "</style>\r\n";
  475. }
  476. }
  477. /**
  478. * Render any footer scripts
  479. *
  480. */
  481. public function footer_scripts() {
  482. if(!empty($this->element_footer_scripts)){
  483. echo "<script type=\"text/javascript\">\r\n";
  484. foreach($this->element_footer_scripts as $script){
  485. echo $script."\r\n";
  486. }
  487. echo "</script>\r\n";
  488. }
  489. }
  490. /***
  491. * Get the current URL
  492. *
  493. */
  494. static function get_url($src = null, $path = null) {
  495. if(!empty($path)){
  496. return plugins_url( $src, $path);
  497. }
  498. return trailingslashit( plugins_url( $path , __FILE__ ) );
  499. }
  500. /***
  501. * Get the current URL
  502. *
  503. */
  504. static function get_path($src = null) {
  505. return plugin_dir_path( $src );
  506. }
  507. }