GPR_Plugin_Framework

Class GPR_Plugin_Framework.

Defined (1)

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

/classes/core.php  
  1. class GPR_Plugin_Framework { 
  2.  
  3. /** @var string Plugin meta */ 
  4. var $meta; 
  5.  
  6. /** @var string Plugin base name */ 
  7. var $basename; 
  8.  
  9. /** @var string Short plugin slug */ 
  10. var $slug; 
  11.  
  12. /** @var string Plugin version */ 
  13. var $version; 
  14.  
  15. /** @var string Plugin textdomain */ 
  16. var $textdomain; 
  17.  
  18. /** @var string Full plugin name */ 
  19. var $name; 
  20.  
  21. /** @var string Plugin directory URL - http://example.com/wp-content/plugins/plugin-slug */ 
  22. var $url; 
  23.  
  24. /** @var string Relative path to includes directory */ 
  25. var $includes; 
  26.  
  27. /** @var string Relative path to views directory */ 
  28. var $views; 
  29.  
  30. /** @var string Relative path to assets directory */ 
  31. var $assets; 
  32.  
  33. /** @var string Plugin control panel URL */ 
  34. var $admin_url; 
  35.  
  36. /** @var string Plugin option name. This option contains all plugin settings */ 
  37. var $option; 
  38.  
  39. /** @var array Set of fields for options page */ 
  40. var $options; 
  41.  
  42. /** @var string Options page config */ 
  43. var $settings; 
  44.  
  45. /** 
  46. * Constructor 
  47. * @param $file 
  48. * @param array $args 
  49. */ 
  50. function __construct( $file, $args = array() ) { 
  51. // Default args 
  52. $defaults = array( 'includes' => 'inc', 'views' => 'inc/views', 'assets' => 'assets' ); 
  53. // Prepare initial data 
  54. $this->file = $file; 
  55. $this->args = wp_parse_args( $args, $defaults ); 
  56. // Check that function get_plugin_data exists 
  57. if ( ! function_exists( 'get_plugin_data' ) ) { 
  58. require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); 
  59.  
  60. // Read plugin meta 
  61. $this->meta = get_plugin_data( $this->file, false ); 
  62. // Init plugin data 
  63. $this->basename = plugin_basename( $this->file ); 
  64. $this->slug = sanitize_key( $this->meta['Name'] ); 
  65. $this->version = sanitize_text_field( $this->meta['Version'] ); 
  66. $this->name = $this->meta['Name']; 
  67. $this->url = plugins_url( '', $this->file ); 
  68. $this->option = $this->slug . '_options'; 
  69. $this->includes = trailingslashit( path_join( plugin_dir_path( $this->file ), trim( $this->args['includes'], '/' ) ) ); 
  70. $this->views = trailingslashit( path_join( plugin_dir_path( $this->file ), trim( $this->args['views'], '/' ) ) ); 
  71. $this->assets = trim( $this->args['assets'], '/' ); 
  72.  
  73. //define a version constant 
  74. define( 'GPR_VERSION', $this->version ); 
  75.  
  76.  
  77. /** 
  78. * Debug 
  79. * description: Helper function 
  80. */ 
  81. function debug() { 
  82. die( '<pre>' . print_r( $this, true ) . '</pre>' ); 
  83.  
  84. /** 
  85. * Is Settings 
  86. * @description: Conditional tag to check there is settings page 
  87. */ 
  88. function is_settings() { 
  89. global $pagenow; 
  90.  
  91. return is_admin() && $pagenow == $this->settings['parent'] && isset( $_GET['page'] ) && $_GET['page'] == $this->slug; 
  92.  
  93. /** 
  94. * Register assets 
  95. */ 
  96. function register_assets() { 
  97. wp_register_style( 'core-plugin-framework', plugins_url( 'assets/css/core.css', dirname( __FILE__ ) ), false, $this->version, 'all' ); 
  98. wp_register_script( 'core-plugin-framework-form', $this->assets( 'js', 'form.js' ), array( 'jquery' ), $this->version, false ); 
  99. wp_register_script( 'core-plugin-fitvids', $this->assets( 'js', 'jquery.fitvids.js' ), array( 'core-plugin-framework-form' ), $this->version, false ); 
  100.  
  101. wp_register_script( 'core-plugin-framework', $this->assets( 'js', 'core.js' ), array( 
  102. 'core-plugin-framework-form',  
  103. 'core-plugin-fitvids' 
  104. ), $this->version, false ); 
  105.  
  106. /** 
  107. * Enqueue assets 
  108. */ 
  109. function enqueue_assets() { 
  110. if ( ! $this->is_settings() ) { 
  111. return; 
  112. foreach ( array( 'thickbox', 'farbtastic', 'core-plugin-framework' ) as $style ) { 
  113. wp_enqueue_style( $style ); 
  114. foreach ( 
  115. array( 
  116. 'jquery',  
  117. 'media-upload',  
  118. 'thickbox',  
  119. 'farbtastic',  
  120. 'core-plugin-framework-form',  
  121. 'core-plugin-fitvids',  
  122. 'core-plugin-framework' 
  123. ) as $script 
  124. ) { 
  125. wp_enqueue_script( $script ); 
  126.  
  127. /** 
  128. * Helper function to get assets url by type 
  129. */ 
  130. function assets( $type = 'css', $file = 'core.css' ) { 
  131. return implode( 
  132. '/', array_filter( 
  133. array( 
  134. trim( $this->url, '/' ),  
  135. trim( $this->assets, '/' ),  
  136. trim( $type, '/' ),  
  137. trim( $file, '/' ) 
  138. ); 
  139.  
  140. /** 
  141. * Set plugin settings to default 
  142. */ 
  143. function default_settings( $manual = false ) { 
  144. // Settings page is created 
  145. if ( $manual || ! get_option( $this->option ) ) { 
  146. // Create array with default options 
  147. $defaults = array(); 
  148. // Loop through available options 
  149. foreach ( (array) $this->options as $value ) { 
  150. if ( isset( $value['id'] ) ) { 
  151. $defaults[ $value['id'] ] = empty( $value['std'] ) ? '' : $value['std']; 
  152. // Insert default options 
  153. update_option( $this->option, $defaults ); 
  154.  
  155. /** 
  156. * Get single option value 
  157. * @param mixed $option Option ID to return. If false, all options will be returned 
  158. * @return mixed $option Returns option by specified key 
  159. */ 
  160. function get_option( $option = false ) { 
  161. // Get options from database 
  162. $options = get_option( $this->option ); 
  163. // Check option is specified 
  164. $value = ( $option ) ? $options[ $option ] : $options; 
  165.  
  166. // Return result 
  167. return ( is_array( $value ) ) ? array_filter( $value, 'esc_attr' ) : esc_attr( stripslashes( $value ) ); 
  168.  
  169. /** 
  170. * Update single option value 
  171. * @param mixed $key Option ID to update 
  172. * @param mixed $value New value 
  173. * @return mixed $option Returns option by specified key 
  174. */ 
  175. function update_option( $key = false, $value = false ) { 
  176. // Prepare variables 
  177. $settings = get_option( $this->option ); 
  178. $new_settings = array(); 
  179. // Prepare data 
  180. foreach ( $settings as $id => $val ) { 
  181. $new_settings[ $id ] = ( $id == $key ) ? $value : $val; 
  182.  
  183. // Update option and return operation result 
  184. return update_option( $this->option, $new_settings ); 
  185.  
  186. /** 
  187. * Action to save/reset options 
  188. */ 
  189. function manage_options() { 
  190. // Check this is settings page 
  191. if ( ! $this->is_settings() ) { 
  192. return; 
  193. // ACTION: RESET 
  194. if ( isset( $_GET['action'] ) && $_GET['action'] == 'reset' ) { 
  195. // Prepare variables 
  196. $new_options = array(); 
  197. // Prepare data 
  198. foreach ( $this->options as $value ) { 
  199. if ( isset( $value['id'] ) && $value['std'] ) { 
  200. $new_options[ $value['id'] ] = $value['std']; 
  201. // Save new options 
  202. if ( update_option( $this->option, $new_options ) ) { 
  203. // Redirect 
  204. wp_redirect( $this->admin_url . '&message=1' ); 
  205. exit; 
  206. } // Option doesn't updated 
  207. else { 
  208. // Redirect 
  209. wp_redirect( $this->admin_url . '&message=2' ); 
  210. exit; 
  211. } // ACTION: SAVE 
  212. elseif ( isset( $_POST['action'] ) && $_POST['action'] == 'save' ) { 
  213. // Prepare vars 
  214. $new_options = array(); 
  215. // Prepare data 
  216. foreach ( $this->options as $value ) { 
  217. $new_options[ $value['id'] ] = ( is_array( $_POST[ $value['id'] ] ) ) ? $_POST[ $value['id'] ] 
  218. : htmlspecialchars( $_POST[ $value['id'] ] ); 
  219. // Save new options 
  220. if ( update_option( $this->option, $new_options ) ) { 
  221. // Redirect 
  222. wp_redirect( $this->admin_url . '&message=3' ); 
  223. exit; 
  224. } // Options not saved 
  225. else { 
  226. // Redirect 
  227. wp_redirect( $this->admin_url . '&message=4' ); 
  228. exit; 
  229.  
  230. /** 
  231. * Register options page 
  232. * @param array $args Options page config 
  233. * @param array $options Set of fields for options page 
  234. */ 
  235. function add_options_page( $args, $options = array() ) { 
  236. // Save options 
  237. $this->options = $options; 
  238. // Prepare defaults 
  239. $defaults = array( 
  240. 'parent' => 'options-general.php',  
  241. 'menu_title' => 'Google Reviews',  
  242. 'page_title' => $this->name,  
  243. 'capability' => 'manage_options',  
  244. 'link' => true 
  245. ); 
  246. // Parse args 
  247. $this->settings = wp_parse_args( $args, $defaults ); 
  248. // Define admin url 
  249. $this->admin_url = admin_url( $this->settings['parent'] . '?page=' . $this->slug ); 
  250. // Register and enqueue assets 
  251. add_action( 'admin_head', array( &$this, 'register_assets' ) ); 
  252. add_action( 'admin_footer', array( &$this, 'enqueue_assets' ) ); 
  253. // Insert default settings if it's doesn't exists 
  254. add_action( 'admin_init', array( &$this, 'default_settings' ) ); 
  255. // Manage options 
  256. add_action( 'admin_menu', array( &$this, 'manage_options' ) ); 
  257. // Add settings page 
  258. add_action( 'admin_menu', array( &$this, 'options_page' ) ); 
  259. // Add settings link to plugins dashboard 
  260. if ( $this->settings['link'] ) { 
  261. add_filter( 
  262. 'plugin_action_links_' . $this->basename, array( 
  263. &$this,  
  264. 'add_settings_link' 
  265. ); 
  266.  
  267. /** 
  268. * Register settings page 
  269. */ 
  270. function options_page() { 
  271. add_submenu_page( 
  272. $this->settings['parent'], __( $this->settings['page_title'], $this->textdomain ), __( $this->settings['menu_title'], $this->textdomain ), $this->settings['capability'], $this->slug, array( 
  273. &$this,  
  274. 'render_options_page' 
  275. ); 
  276.  
  277. /** 
  278. * Display settings page 
  279. */ 
  280. function render_options_page() { 
  281. $backend_file = $this->views . 'settings.php'; 
  282. if ( file_exists( $backend_file ) ) { 
  283. require_once $backend_file; 
  284.  
  285. /** 
  286. * Add settings link to plugins dashboard 
  287. */ 
  288. function add_settings_link( $links ) { 
  289. $links[] = '<a href="' . $this->admin_url . '">' . __( 'Settings', 'google-places-reviews' ) . '</a>'; 
  290.  
  291. return $links; 
  292.  
  293. /** 
  294. * Display settings panes 
  295. */ 
  296. function render_panes() { 
  297. // Get current settings 
  298. $settings = get_option( $this->option ); 
  299. // Options loop 
  300. foreach ( $this->options as $option ) { 
  301. // Get option file path 
  302. $option_file = $this->views . $option['type'] . '.php'; 
  303. // Check that file exists and include it 
  304. if ( file_exists( $option_file ) ) { 
  305. include( $option_file ); 
  306. } else { 
  307. trigger_error( 'Option file <strong>' . $option_file . '</strong> not found!', E_USER_NOTICE ); 
  308.  
  309. /** 
  310. * Display settings tabs 
  311. */ 
  312. function render_tabs() { 
  313. foreach ( $this->options as $option ) { 
  314. if ( $option['type'] == 'opentab' ) { 
  315. $active = ( isset( $active ) ) ? ' sunrise-plugin-tab-inactive' 
  316. : ' nav-tab-active sunrise-plugin-tab-active'; 
  317. echo '<span class="nav-tab' . $active . '">' . $option['name'] . '</span>'; 
  318.  
  319. /** 
  320. * Show notifications 
  321. */ 
  322. function notifications( $notifications ) { 
  323. $file = $this->views . 'notifications.php'; 
  324. if ( file_exists( $file ) ) { 
  325. include $file; 
  326.