'http://wordpress.org/extend/plugins/flexidx-home-search/', //plugin page on wp.org
'official_url' => 'http://www.phoenixhomes.com/tech/flexidx-home-search', //plugin page on author's website
'author_url' => 'http://wordpress.org/extend/plugins/profile/maxchirkov',
'sponsored_by' => 'PhoenixHomes.com',
'forums_url' => 'http://wordpress.org/tags/flexidx-home-search?forum_id=10',
);
function __construct() {
// register_activation_hook( __FILE__, array($this, 'plugin_activate'));
add_action('admin_init', array(&$this,'options_init'));
if(!empty($this->submenu_pages)){
add_action('admin_menu', array(&$this, 'register_menu'));
}else{
add_action('admin_menu', array(&$this, 'register_settings_page'));
add_filter('plugin_action_links', array(&$this, 'add_action_link'), 10, 2);
}
add_filter('ozh_adminmenu_icon', array(&$this, 'add_ozh_adminmenu_icon'));
add_action('admin_print_scripts', array(&$this,'config_page_scripts'));
add_action('admin_print_styles', array(&$this,'config_page_styles'));
add_action('admin_head', array(&$this, 'admin_head'));
//add_action('wp_dashboard_setup', array(&$this,'widget_setup'));
$this->options = get_option($this->optionname);
//Localization
add_action( 'init', array(&$this, 'localize'), 1 );
add_action('init', array(&$this, 'load_locale'), 10 );
}
//empty functions to be used in class extension to localize strings
function localize(){
}
function set_var($name, $value){
$this->$name = $value;
}
function set_longname($longname){
$this->longname = $longname;
}
function set_shortname($shortname){
$this->shortname = $shortname;
}
function plugin_activate(){
if(!$this->options){
if($this->default_options){
add_option($this->optionname, $this->default_options);
}else{
$settings = $this->settings();
if(is_array($settings)){
foreach($settings as $field){
$default = null;
if(in_array($field['type'], array('text', 'textarea', 'select'))){
if($field['default']){
$default = $field['default'];
}
}elseif($field['type'] == 'checkbox'){
if($field['attr']['checked']){
if(!$default = $field['default']){
$default = true;
}
}
}elseif($field['attr']['checked'] && $field['default']){
$default = $field['default'];
}
if($default){
if(is_array($field['id'])){
$opt[$field['id'][0]][$field['id'][1]] = $default;
}else{
$opt[$field['id']] = $default;
}
}
}
}
add_option($this->optionname, $opt);
}
$this->options = get_option($this->optionname);
}
}
function add_ozh_adminmenu_icon( $hook ) {
if ($hook == $this->hook)
return WP_CONTENT_URL . '/plugins/' . plugin_basename(dirname($filename)). '/'.$this->ozhicon;
return $hook;
}
/**
* Hook called to change the locale directory
* @return void
**/
function load_locale() {
// Here we manually fudge the plugin locale as WP doesnt allow many options
$locale = get_locale();
if( empty( $locale ) )
$locale = 'en_US';
$mofile = dirname( __FILE__ )."/../languages/{$this->hook}-{$locale}.mo";
load_textdomain( $this->hook, $mofile );
}
//Returns the help context
function contextual_help (){}
//using add_contextual_help hook
function add_contextual_help(){
if( '' != $this->screen_name && $this->contextual_help() )
{
// add_contextual_help( $this->screen_name, $this->contextual_help() );
$screen = get_current_screen();
$screen->add_help_tab( array(
'id' => 'atw-help', //unique id for the tab
'title' => 'ATW Help', //unique visible title for the tab
'content' => $this->contextual_help(), //actual help text
// 'callback' => $callback //optional function to callback
) );
}
}
/**
* Config Page Scripts
*/
function config_page_styles() {
if ($page = @$_GET['page'])
{
if ($page && ($page == $this->filename || $page == $this->hook)) {
wp_enqueue_style('dashboard');
wp_enqueue_style('thickbox');
wp_enqueue_style('global');
wp_enqueue_style('wp-admin');
wp_enqueue_style('blogicons-admin-css', WP_CONTENT_URL . '/plugins/' . plugin_basename(dirname(__FILE__)). '/wp_plugin_admin.css');
}
}
}
function config_page_scripts() {
if ($page = @$_GET['page'])
{
if ($page && ($page == $this->filename || $page == $this->hook)) {
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-ui');
wp_enqueue_script('jquery-ui-core');
wp_enqueue_script('jquery-ui-tabs');
wp_enqueue_script('jquery-ui-sortable');
wp_enqueue_script('postbox');
wp_enqueue_script('dashboard');
wp_enqueue_script('thickbox');
wp_enqueue_script('media-upload');
}
}
}
function admin_head(){
//this condition is important, otherwise, if loads on other pages - breaks collapsible sidebar navigation.
if ($page = @$_GET['page'])
{
if ($page && ($page == $this->filename || $page == $this->hook)) {
wp_enqueue_script('jquery-ui-sortable');
wp_enqueue_style('jquery-ui', '//jquery-ui.googlecode.com/svn/tags/latest/themes/base/jquery.ui.all.css', array());
// echo '';
// echo '';
echo '
';
}
}
}
/**
* Register Settings Page
*/
function register_settings_page() {
$this->screen_name = add_options_page($this->longname, $this->shortname, $this->accesslevel, $this->hook, array(&$this,'config_page'));
add_action('load-'.$this->screen_name, array(&$this, 'add_contextual_help'));
}
function plugin_options_url() {
return admin_url( 'options-general.php?page='.$this->hook );
}
/**
* Add a link to the settings page to the plugins list
*/
function add_action_link( $links, $file ) {
static $this_plugin;
if( empty($this_plugin) ) $this_plugin = $this->filename;
if ( $file == $this_plugin ) {
$settings_link = '' . __('Settings') . '';
array_unshift( $links, $settings_link );
}
return $links;
}
/**
* Register Menu
*/
function options_init(){
register_setting($this->optionname . '-option-group', $this->optionname, array(&$this, 'validate_input'));
}
function register_menu(){
add_menu_page($this->longname, $this->shortname, 8, $this->filename, array(&$this,'show_menu'));
foreach($this->submenu_pages as $submenu_slug => $submenu_title){
add_submenu_page($this->filename, $submenu_title, $submenu_title, 8, $submenu_slug, array(&$this,'show_menu'));
}
}
function show_menu(){
$submenu_slugs = array_keys($this->submenu_pages);
$page = esc_attr($_GET['page']);
if($page && in_array($page, $submenu_slugs)){
//submenu slug contains prefix, but submenu files don't, so remove prefix
$submenu_file_name = str_replace($this->prefix, '', $page) . '.php';
$submenu_file_path = dirname (__FILE__) . '/' . $submenu_file_name;
//options funtion should be $submenu_slug + _options_page()
$func = $page . '_options_page';
if(function_exists($func)){
call_user_func($func);
}elseif(file_exists($submenu_file_path)){
include_once ($submenu_file_path);
call_user_func($func);
}
}else{
$this->config_page();
}
}
/*
* Applies input_callback functions form the settings array as well as encodes html entitites for strings.
* This function can be overwritten with custom validation from the class extension.
*/
function validate_input($input){
//overwrite and do something here
$settings = $this->settings();
foreach($input as $k1 => $fields){
if(is_array($fields)){
foreach($fields as $k2 => $field){
foreach($settings as $item){
if($item['id'] == array($k1, $k2) && $item['input_callback']){
if(is_array( $input[$k1][$k2])){
$input[$k1][$k2] = call_user_func_array($item['input_callback'], $input[$k1][$k2]);
}else{
$input[$k1][$k2] = call_user_func($item['input_callback'], htmlentities($input[$k1][$k2], ENT_QUOTES));
}
}elseif($item['id'] == array($k1, $k2)){
$input[$k1][$k2] = htmlentities($input[$k1][$k2], ENT_QUOTES);
}
}
}
}else{
foreach($settings as $item){
if($item['id'] == $k1 && $item['input_callback']){
if(is_array($input[$k1])){
$input[$k1] = call_user_func_array($item['input_callback'], $input[$k1]);
}else{
$input[$k1] = call_user_func($item['input_callback'], htmlentities($input[$k1], ENT_QUOTES));
}
}elseif($item['id'] == $k1){
$input[$k1] = htmlentities($input[$k1], ENT_QUOTES);
}
}
}
}
//since tabs have separate forms, we need to murge the incomplete array of new settings into the existing settings array
//$input = array_replace_recursive($this->options, $input);
$new_input = $this->options;
foreach($input as $k => $v){
$new_input[$k] = $v;
}
return $new_input;
}
function config_page() {
echo 'Function config_page() needs to be declaired within the extended Class.';
}
/**
* Create a Checkbox input field
*/
function checkbox($id, $label) {
$options = $this->options;
return '
';
}
/**
* Create a Text input field
*/
function textinput($id, $label) {
$options = $this->options;
return '
';
}
/**
* Creates a name for a form field which is in a form of an array optiongroup[option]
* @param string $option_id
* @return string
*/
function _name($field_params){
//$parts = explode('###', str_replace(']', '', str_replace('[', '###', $field_params['id'])));
if(is_string($field_params['id']))
$field_params['id'] = array($field_params['id']);
$i=0;
$output = false;
if(is_array($field_params['id'])){
foreach($field_params['id'] as $part){
if($i>0){
$output .= "[$part]";
}else{
$output .= $this->optionname . "[$part]";
}
$i++;
}
if(!is_array($output))
return $output;
}else{
if(!is_array($parts))
return $parts;
}
}
/**
* Creates an id for a form field reflecting option's hierarchy with hyphans
* @param string $option_id
* @return string
*/
function _id($field_params){
//$output = str_replace(']', '', str_replace('[', '-', $field_params['id']));
if( !isset($field_params['id']) )
return;
if(!is_array($field_params['id']))
return $field_params['id'];
return implode('-', $field_params['id']);
}
/**
* Grabs a value from a multidimentional array by a string field id
* @param string $option_id
* @return mixed
*/
function _val($field_params){
$options = $this->options;
if(!$options)
return;
//$parts = explode('###', str_replace(']', '', str_replace('[', '###', $field_params['id'])));
if(!is_array($field_params['id']) && is_string($field_params['id'])){
$val = $options[$field_params['id']];
if($field_params['output_callback']){
$val = call_user_func($field_params['output_callback'], $val);
}
//pa($val);
return $val;
}elseif(is_array($field_params['id'])){
//$val = $options[$field_params['id'][0]][$field_params['id'][1]];
$tmp = $options;
foreach($field_params['id'] as $item){
$tmp = @$tmp[$item];
}
$val = $tmp;
if( isset($field_params['output_callback']) ){
$val = call_user_func($field_params['output_callback'], $val);
}
return $val;
}
}
function _label($field_params){
if(!empty($field_params['label'])){
return '' . "\n";
}else{
return '';
}
}
/**
* Parces array of field parameters and their options and reterns a string or an array
* of complete input fields with their values and labels.
* @param array $field_params
* @param bool $array
* @return string|array
*/
function inputfield($field_params, $array = false) {
$options = $this->options;
//explode array into additioanal input fields parameters
$attributes = false;
if(isset($field_params['attr']) && !empty($field_params['attr'])){
foreach($field_params['attr'] as $k => $v){
if($k != 'checked'){
$attributes .= $k . '="' . $v . '" ';
//for selection list
if($k == 'multiple'){
$name_suffix = '[]';
}
}
}
}
(isset($field_params['desc'])) ? $desc = "{$field_params['desc']}\n" : $desc = '';
switch($field_params['type']){
case 'html':
$output = array(
$this->_label($field_params),
$field_params['default'] ."\n",
);
break;
case 'text':
$output = array(
$this->_label($field_params),
'' . "\n" . $desc
);
break;
case 'textarea':
$output = array(
$this->_label($field_params),
'' . "\n" . $desc
);
break;
case 'radio':
$output = array(
$this->_label($field_params),
'_val($field_params), $field_params['default'], false) . '/>' . "\n" . $desc
);
break;
case 'checkbox':
$output = array(
$this->_label($field_params),
'_val($field_params), true, false) . '/>' . "\n" . $desc
);
break;
case 'select':
if(!empty($field_params['options'])){
if($name_suffix){
if($this->_val($field_params)){
$keys = $this->_val($field_params);
}elseif($field_params['default']){
$keys = $field_params['default'];
}
}else{
if($this->_val($field_params)){
$keys = $this->_val($field_params);
}else{
$keys = $field_params['default'];
}
}
foreach($field_params['options'] as $key => $value){
$selected = '';
if(is_array($keys)){
if(in_array($key, $keys)){
$selected = ' selected="selected"';
}
}elseif($key == $keys){
$selected = ' selected="selected"';
}
$select_options .= '' . "\n";
}
}
$output = array(
$this->_label($field_params),
'' . "\n"
. $desc
);
break;
case 'input':
$output = array(
$this->_label($field_params),
'' . "\n" . $desc
);
break;
}
if($array){
//retrun array
return $output;
}else{
//return string
return implode('', $output);
}
}
/**
* Create a form table from an array of rows
*/
function form_table($rows) {
$content = '
| ' . "\n";
if (isset($row['id']) && $row['id'] != '')
$content .= '' . "\n";
else
$content .= $row['label'];
if (isset($row['desc']) && $row['desc'] != '')
$content .= ' '.$row['desc'].'' . "\n"; $content .= ' | ' . "\n"; $content .= $row['content']; $content .= ' |
|---|