| [ Index ] |
PHP Cross Reference of GlotPress |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * GlotPress plugin base class. It is supposed to be inherited. 5 */ 6 class GP_Plugin { 7 8 /** 9 * @var $id unique id of the plugin. It will be used as a plugin slug in the plugin repository 10 */ 11 var $id; 12 13 /** 14 * If you override the constructor, always call the parent one. 15 */ 16 function __construct() { 17 $this->_object_type = 'plugin_'.$this->id; 18 } 19 20 21 /** 22 * Retrieve an option, specific to your plugin. 23 * 24 * You don't have to prefix the key or to tie its name to your plugin. 25 * 26 * @param string $key 27 * @return mixed the value of the option, or null if it wasn't found 28 */ 29 function get_option( $key ) { 30 global $gpdb; 31 // TODO: add caching (see gp_get_option_from_db()) 32 $row = $gpdb->get_row( $gpdb->prepare( "SELECT `meta_value` FROM `$gpdb->meta` WHERE `object_type` = %s AND `meta_key` = %s", $this->_object_type, $key ) ); 33 if ( is_object( $row ) ) { 34 $r = maybe_unserialize( $row->meta_value ); 35 } else { 36 $r = null; 37 } 38 return $r; 39 } 40 41 /** 42 * Update an option, specific to your plugin. 43 * 44 * You don't have to prefix the key or to tie its name to your plugin. 45 * 46 * @param string $key 47 * @param mixed $value Can be anything serializable 48 * @return bool 49 */ 50 function update_option( $key, $value ) { 51 return gp_update_meta( 0, $key, $value, $this->_object_type, true ); 52 } 53 54 /** 55 * Adds a method in this class as an action with the same name. 56 * 57 * For example $this->add_action( 'init', ... ) will add $this->init() as an init action 58 * 59 * @param string $action_name The name of the action and the method. 60 * @param array $args Two keys are supported: 61 * - priority => priority of the action. Positive integer. Lower priority means earlier execution. Default is 10. 62 * - args => how many arguments the action accepts. Default is 1. 63 * 64 * @see add_action() 65 */ 66 function add_action( $action_name, $args = array() ) { 67 return $this->add_filter( $action_name, $args ); 68 } 69 70 /** 71 * Adds a method in this class as a filter with the same name. 72 * 73 * For example $this->add_filter( 'the_content', ... ) will add $this->the_content() as a the_content filter 74 * 75 * @param string $filter_name The name of the filter and the method. 76 * @param array $args Two keys are supported: 77 * - priority => priority of the filter. Positive integer. Lower priority means earlier execution. Default is 10. 78 * - args => how many arguments the filter accepts. Default is 1. 79 * 80 * @see add_action() 81 */ 82 function add_filter( $filter_name, $args = array() ) { 83 return $this->_call_wp_plugin_api_function( 'add_filter', $filter_name, $args ); 84 } 85 86 function _call_wp_plugin_api_function( $wp_function, $tag, $args = array() ) { 87 $args['tag'] = $tag; 88 $defaults = array( 'priority' => 10, 'args' => 1 ); 89 $args = array_merge( $defaults, $args ); 90 return $wp_function( $args['tag'], array( &$this, $args['tag'] ), $args['priority'], $args['args'] ); 91 } 92 93 function remove_action( $action_name, $args = array() ) { 94 return $this->remove_filter( $action_name, $args ); 95 } 96 97 function remove_filter( $filter_name, $args = array() ) { 98 return $this->_call_wp_plugin_api_function( 'remove_filter', $filter_name, $args ); 99 } 100 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu May 24 03:59:35 2012 | Hosted by follow the white rabbit. |