PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wp-css3-button-creator-plugin/libs/class.plugin-base.php

https://gitlab.com/endomorphosis/jeffersonsmithmayor
PHP | 171 lines | 169 code | 2 blank | 0 comment | 20 complexity | d80211d6cf462a3169f8b2ac95f79c2c MD5 | raw file
  1. <?php
  2. require( ABSPATH . WPINC . '/pluggable.php' );
  3. class WPCss3ButtonCreatorBase{
  4. var $admin_js;
  5. var $admin_css;
  6. var $front_css;
  7. var $front_js;
  8. var $message;
  9. function __construct($config){
  10. $this->admin_js = self::array_union($this->admin_js,array('jquery'));
  11. $this->front_js = self::array_union($this->front_js,array('jquery'));
  12. $this->front_css = self::array_union($this->front_css,array());
  13. $this->admin_css = self::array_union($this->admin_css,array());
  14. $this->config = $config;//json_decode(file_get_contents($configfile));
  15. $this->plugin_folder = strtok(plugin_basename(__FILE__),'/');
  16. $this->plugin_path = WP_PLUGIN_DIR . '/'.$this->plugin_folder;
  17. $this->plugin_url = WP_PLUGIN_URL . '/'.$this->plugin_folder;
  18. $this->plugin_id = self::escapize($this->config->plugin_name);
  19. $this->options = get_option($this->plugin_id);
  20. add_action('admin_menu', array(&$this, 'add_menu'));
  21. add_action('init',array(&$this, 'load_front_libraries'));
  22. register_activation_hook( $this->plugin_path . '/' . $this->config->plugin_base_file, array(&$this,'activate') );
  23. register_deactivation_hook( $this->plugin_path . '/' . $this->config->plugin_base_file, array(&$this,'deactivate') );
  24. $methods = get_class_methods($this);
  25. foreach($methods as $method){
  26. $p = explode('__',$method);
  27. $jackpot = array('filter','action','shortcode');
  28. if(isset($p[1])&&in_array($p[1], $jackpot)){
  29. $a = sprintf("add_%s", $p[1]);
  30. $a($p[0], array(&$this, $method));
  31. }
  32. }
  33. }
  34. function add_menu(){
  35. if(!isset($this->config->pages)) return;
  36. if(isset($this->addon)){
  37. foreach($this->config->pages as $key=>$value){
  38. $page = add_submenu_page($this->addon, $value->title,$value->title, 'add_users',
  39. $this->plugin_id.'-'.$key, array(&$this, 'dispatch_page'));
  40. add_action('admin_print_styles-' . $page, array(&$this, 'load_admin_libraries'));
  41. }
  42. }
  43. $default_page = $this->config->default_page;
  44. if($this->config->menu_type == "option_page"){
  45. $page = add_options_page($this->config->plugin_name, $this->config->plugin_name,
  46. 'manage_options', $this->plugin_id.'-'.$default_page,
  47. array(&$this, 'dispatch_page'));
  48. add_action('admin_print_styles-' . $page, array(&$this, 'load_admin_libraries'));
  49. }else if($this->config->menu_type == 'menu_page'){
  50. $page = add_menu_page($this->config->plugin_name, $this->config->plugin_name,'add_users',
  51. $this->plugin_id.'-'.$default_page, array(&$this, 'dispatch_page'),$this->plugin_url.'/images/'.'menu_logo.png');
  52. add_action('admin_print_styles-' . $page, array(&$this, 'load_admin_libraries'));
  53. $default_page_title = $this->config->pages->{$default_page}->title;
  54. $page = add_submenu_page($this->plugin_id.'-'.$default_page, $default_page_title, $default_page_title, 'add_users',
  55. $this->plugin_id.'-'.$default_page, array(&$this, 'dispatch_page'));
  56. add_action('admin_print_styles-' . $page, array(&$this, 'load_admin_libraries'));
  57. foreach($this->config->pages as $key=>$value){
  58. if($key == $default_page) continue;
  59. $page = add_submenu_page($this->plugin_id.'-'.$default_page, $value->title,$value->title, 'add_users',
  60. $this->plugin_id.'-'.$key, array(&$this, 'dispatch_page'));
  61. add_action('admin_print_styles-' . $page, array(&$this, 'load_admin_libraries'));
  62. }
  63. }
  64. }
  65. function dispatch_page(){
  66. $screen = $this->get_current_screen();
  67. echo '<h2>'.$this->config->plugin_name .' v'.$this->config->version .'</h2>';
  68. $this->draw_tabs();
  69. if(method_exists($this, $screen->page)){
  70. $this->{$screen->page}();
  71. }else{
  72. $method = $screen->page. '_tab_'. $screen->tab;
  73. if(method_exists($this, $method)){
  74. $this->$method();
  75. }else{
  76. echo 'Define <b>'.$screen->page .'</b> or <b>'. $method . '</b>' . ' in <b>'. get_class($this).'</b> class.';
  77. }
  78. }
  79. }
  80. function load_front_libraries(){
  81. if(!is_admin()){
  82. wp_enqueue_scripts(array('jquery'));
  83. foreach($this->front_js as $js){
  84. wp_enqueue_script($js, $this->plugin_url. '/js/'.$js.'.js');
  85. }
  86. foreach($this->front_css as $css){
  87. wp_enqueue_style($css, $this->plugin_url. '/css/'.$css.'.css');
  88. }
  89. }
  90. }
  91. function load_admin_libraries(){
  92. $pages = array_keys((array)$this->config->pages);
  93. $screen = $this->get_current_screen();
  94. if(!in_array($screen->page, $pages)) return;
  95. wp_enqueue_scripts(array('jquery'));
  96. foreach($this->admin_js as $js){
  97. wp_enqueue_script($js, $this->plugin_url. '/js/'.$js.'.js');
  98. }
  99. foreach($this->admin_css as $css){
  100. wp_enqueue_style($css, $this->plugin_url. '/css/'.$css.'.css');
  101. }
  102. }
  103. function set_options($new_options = array()){
  104. foreach($new_options as $key=>$value)
  105. $this->options[$key] = $value;
  106. update_option($this->plugin_id , $this->options);
  107. }
  108. function setting_page(){
  109. $screen = $this->get_current_screen();
  110. $tab = $this->config->pages->setting_page->tabs[$screen->tab];
  111. if(isset($_POST[$this->plugin_id.'_submit'])){
  112. $updated_options = $_POST[$this->plugin_id];
  113. $whole_new_options = array();
  114. foreach($tab->blocks as $key=>$block){
  115. foreach($block->fields as $field){
  116. if (isset($field->name))
  117. $name = $field->name;
  118. else
  119. $name = self::escapize($field->level);
  120. if(isset($updated_options[$key][$name]))
  121. $whole_new_options[$key][$name] = $updated_options[$key][$name];
  122. else
  123. $whole_new_options[$key][$name] = '';
  124. }
  125. }
  126. $this->set_options($whole_new_options);
  127. if(empty($this->message))$this->message = 'Options updated!';
  128. }
  129. $options = $this->options;
  130. include_once($this->plugin_path.'/views/settings.php');
  131. }
  132. function draw_tabs(){
  133. $screen = $this->get_current_screen();
  134. $current_page = $this->config->pages->{$screen->page};
  135. if(!isset($current_page->tabs)) return;
  136. $tabs = $this->config->pages->{$screen->page}->tabs;
  137. echo '<h2 class="nav-tab-wrapper">';
  138. foreach($tabs as $key=>$value){
  139. $active = (($key== $screen->tab)?'nav-tab-active':'');
  140. $url = '?page='. $screen->plugin.'-'.$screen->page.'&tab='.$key;
  141. echo '<a href="' . $url. '" class="nav-tab '. $active. '" >' .$value->title . '</a>';
  142. }
  143. echo '</h2>';
  144. }
  145. function activate(){
  146. $this->set_options(array('setting'=>$_POST['setting']));
  147. }
  148. function deactivate(){
  149. }
  150. function get_current_screen(){
  151. $default_page = $this->config->default_page;
  152. $page = explode('-',$_GET['page']);
  153. $screen = new stdClass();
  154. $screen->tab = isset($_GET['tab'])?$_GET['tab'] : 0;
  155. $screen->page = isset($page[1])?$page[1] : $default_page;
  156. $screen->menuid = 0;
  157. $screen->plugin = $page[0];
  158. return $screen;
  159. }
  160. static function escapize($str){
  161. return str_replace(' ', '_', strtolower($str));
  162. }
  163. static function array_union($a1,$a2){
  164. if(empty($a1)) $a1 = array();
  165. if(empty($a2)) $a2 = array();
  166. foreach( $a2 as $a)array_push($a1, $a);
  167. return array_unique($a1);
  168. }
  169. }