| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Charitable Core Admin Functions |
| 5 |
* |
| 6 |
* General core functions available only within the admin area. |
| 7 |
* |
| 8 |
* @package Charitable/Functions/Admin |
| 9 |
* @version 1.0.0 |
| 10 |
* @author Eric Daams |
| 11 |
* @copyright Copyright (c) 2015, Studio 164a |
| 12 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 13 |
*/ |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 16 |
|
| 17 |
/** |
| 18 |
* Load a view from the admin/views folder. |
| 19 |
* |
| 20 |
* If the view is not found, an Exception will be thrown. |
| 21 |
* |
| 22 |
* Example usage: charitable_admin_view('metaboxes/cause-metabox'); |
| 23 |
* |
| 24 |
* @param string $view The view to display. |
| 25 |
* @param array $view_args Optional. Arguments to pass through to the view itself |
| 26 |
* @return void |
| 27 |
* @since 1.0.0 |
| 28 |
*/ |
| 29 |
function charitable_admin_view( $view, $view_args = array() ) { |
| 30 |
$filename = apply_filters( 'charitable_admin_view_path', charitable()->get_path( 'admin' ) . 'views/' . $view . '.php', $view, $view_args ); |
| 31 |
|
| 32 |
if ( ! is_readable( $filename ) ) { |
| 33 |
_doing_it_wrong( __FUNCTION__, __( 'Passed view (' . $filename . ') not found or is not readable.', 'charitable' ), '1.0.0' ); |
| 34 |
} |
| 35 |
|
| 36 |
ob_start(); |
| 37 |
|
| 38 |
include( $filename ); |
| 39 |
|
| 40 |
ob_end_flush(); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Returns the Charitable_Admin_Settings helper. |
| 45 |
* |
| 46 |
* @return Charitable_Admin_Settings |
| 47 |
* @since 1.0.0 |
| 48 |
*/ |
| 49 |
function charitable_get_admin_settings() { |
| 50 |
return Charitable_Settings::get_instance(); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Returns whether we are currently viewing the Charitable settings area. |
| 55 |
* |
| 56 |
* @param string $tab Optional. If passed, the function will also check that we are on the given tab. |
| 57 |
* @return boolean |
| 58 |
* @since 1.2.0 |
| 59 |
*/ |
| 60 |
function charitable_is_settings_view( $tab = "" ) { |
| 61 |
if ( ! empty( $_POST ) ) { |
| 62 |
|
| 63 |
$is_settings = isset( $_POST[ 'charitable_settings' ] ); |
| 64 |
|
| 65 |
if ( ! $is_settings || empty( $tab ) ) { |
| 66 |
return $is_settings; |
| 67 |
} |
| 68 |
|
| 69 |
return array_key_exists( $tab, $_POST[ 'charitable_settings' ] ); |
| 70 |
} |
| 71 |
|
| 72 |
$is_settings = isset( $_GET[ 'page' ] ) && 'charitable-settings' == $_GET[ 'page' ]; |
| 73 |
|
| 74 |
if ( ! $is_settings || empty( $tab ) ) { |
| 75 |
return $is_settings; |
| 76 |
} |
| 77 |
|
| 78 |
/* The general tab can be loaded when tab is not set. */ |
| 79 |
if ( 'general' == $tab ) { |
| 80 |
return ! isset( $_GET[ 'tab' ] ) || 'general' == $_GET[ 'tab' ]; |
| 81 |
} |
| 82 |
|
| 83 |
return isset( $_GET[ 'tab' ] ) && $tab == $_GET[ 'tab' ]; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Print out the settings fields for a particular settings section. |
| 88 |
* |
| 89 |
* This is based on WordPress' do_settings_fields but allows the possibility |
| 90 |
* of leaving out a field lable/title, for fullwidth fields. |
| 91 |
* |
| 92 |
* @see do_settings_fields |
| 93 |
* |
| 94 |
* @global $wp_settings_fields Storage array of settings fields and their pages/sections |
| 95 |
* |
| 96 |
* @param string $page Slug title of the admin page who's settings fields you want to show. |
| 97 |
* @param string $section Slug title of the settings section who's fields you want to show. |
| 98 |
* @return string |
| 99 |
* @since 1.0.0 |
| 100 |
*/ |
| 101 |
function charitable_do_settings_fields( $page, $section ) { |
| 102 |
global $wp_settings_fields; |
| 103 |
|
| 104 |
if ( ! isset( $wp_settings_fields[ $page ][ $section ] ) ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
foreach ( (array) $wp_settings_fields[ $page ][ $section ] as $field ) { |
| 109 |
$class = ''; |
| 110 |
|
| 111 |
if ( ! empty( $field['args']['class'] ) ) { |
| 112 |
$class = ' class="' . esc_attr( $field['args']['class'] ) . '"'; |
| 113 |
} |
| 114 |
|
| 115 |
echo "<tr{$class}>"; |
| 116 |
|
| 117 |
if ( ! empty( $field['args']['label_for'] ) ) { |
| 118 |
echo '<th scope="row"><label for="' . esc_attr( $field['args']['label_for'] ) . '">' . $field['title'] . '</label></th>'; |
| 119 |
echo '<td>'; |
| 120 |
call_user_func($field['callback'], $field['args']); |
| 121 |
echo '</td>'; |
| 122 |
} |
| 123 |
elseif ( ! empty( $field[ 'title' ] ) ) { |
| 124 |
echo '<th scope="row">' . $field['title'] . '</th>'; |
| 125 |
echo '<td>'; |
| 126 |
call_user_func($field['callback'], $field['args']); |
| 127 |
echo '</td>'; |
| 128 |
} |
| 129 |
else { |
| 130 |
echo '<td colspan="2" class="charitable-fullwidth">'; |
| 131 |
call_user_func($field['callback'], $field['args']); |
| 132 |
echo '</td>'; |
| 133 |
} |
| 134 |
|
| 135 |
echo '</tr>'; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Add new tab to the Charitable settings area. |
| 141 |
* |
| 142 |
* @param string[] $tabs |
| 143 |
* @param string $key |
| 144 |
* @param string $name |
| 145 |
* @param mixed[] $args |
| 146 |
* @return string[] |
| 147 |
* @since 1.3.0 |
| 148 |
*/ |
| 149 |
function charitable_add_settings_tab( $tabs, $key, $name, $args = array() ) { |
| 150 |
$defaults = array( |
| 151 |
'index' => 3 |
| 152 |
); |
| 153 |
|
| 154 |
$args = wp_parse_args( $args, $defaults ); |
| 155 |
|
| 156 |
$keys = array_keys( $tabs ); |
| 157 |
$values = array_values( $tabs ); |
| 158 |
|
| 159 |
array_splice( $keys, $args[ 'index' ], 0, $key ); |
| 160 |
array_splice( $values, $args[ 'index' ], 0, $name ); |
| 161 |
|
| 162 |
return array_combine( $keys, $values ); |
| 163 |
} |