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