| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Admin Category class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Registers fields on Categories and saves its settings when the Category |
| 11 |
* is saved in the WordPress Administration interface. |
| 12 |
* |
| 13 |
* @package ConvertKit |
| 14 |
* @author ConvertKit |
| 15 |
*/ |
| 16 |
class ConvertKit_Admin_Category { |
| 17 |
|
| 18 |
/** |
| 19 |
* Registers action and filter hooks. |
| 20 |
* |
| 21 |
* @since 1.9.6 |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
|
| 25 |
// Load CSS and JS when adding/editing Categories. |
| 26 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 27 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) ); |
| 28 |
|
| 29 |
// Add Category. |
| 30 |
add_action( 'category_add_form_fields', array( $this, 'add_category_form_fields' ), 10 ); |
| 31 |
add_action( 'created_category', array( $this, 'save_category_fields' ), 20 ); |
| 32 |
|
| 33 |
// Edit Category. |
| 34 |
add_action( 'category_edit_form_fields', array( $this, 'edit_category_form_fields' ), 20 ); |
| 35 |
add_action( 'edited_category', array( $this, 'save_category_fields' ), 20 ); |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Enqueue JavaScript when editing a Category that outputs |
| 41 |
* ConvertKit Plugin settings. |
| 42 |
* |
| 43 |
* @since 1.9.6.4 |
| 44 |
* |
| 45 |
* @param string $hook Hook. |
| 46 |
*/ |
| 47 |
public function enqueue_scripts( $hook ) { |
| 48 |
|
| 49 |
// Don't load scripts if not editing a Category. |
| 50 |
if ( ! $this->is_category_edit_screen( $hook ) ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
// Enqueue Select2 JS. |
| 55 |
convertkit_select2_enqueue_scripts(); |
| 56 |
|
| 57 |
/** |
| 58 |
* Enqueue JavaScript when editing a Category that outputs |
| 59 |
* ConvertKit Plugin settings. |
| 60 |
* |
| 61 |
* @since 1.9.6.4 |
| 62 |
*/ |
| 63 |
do_action( 'convertkit_admin_category_enqueue_scripts' ); |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Enqueue CSS when editing a Category that outputs |
| 69 |
* ConvertKit Plugin settings. |
| 70 |
* |
| 71 |
* @since 1.9.6.4 |
| 72 |
* |
| 73 |
* @param string $hook Hook. |
| 74 |
*/ |
| 75 |
public function enqueue_styles( $hook ) { |
| 76 |
|
| 77 |
// Don't load scripts if not editing a Category. |
| 78 |
if ( ! $this->is_category_edit_screen( $hook ) ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
// Enqueue Select2 CSS. |
| 83 |
convertkit_select2_enqueue_styles(); |
| 84 |
|
| 85 |
// Enqueue Category CSS. |
| 86 |
wp_enqueue_style( 'convertkit-category', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/category.css', array(), CONVERTKIT_PLUGIN_VERSION ); |
| 87 |
|
| 88 |
/** |
| 89 |
* Enqueue CSS when editing a Category that outputs |
| 90 |
* ConvertKit Plugin settings. |
| 91 |
* |
| 92 |
* @since 1.9.6.4 |
| 93 |
*/ |
| 94 |
do_action( 'convertkit_admin_category_enqueue_styles' ); |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Determine if the current request is for editing a Category. |
| 100 |
* |
| 101 |
* @since 2.0.3 |
| 102 |
* |
| 103 |
* @param string $hook Hook. |
| 104 |
* @return bool Is category edit screen request |
| 105 |
*/ |
| 106 |
private function is_category_edit_screen( $hook ) { |
| 107 |
|
| 108 |
// Bail if we are not editing a Term. |
| 109 |
if ( $hook !== 'term.php' && $hook !== 'edit-tags.php' ) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
// Bail if we are not editing a Category. |
| 114 |
if ( convertkit_get_current_screen( 'id' ) !== 'edit-category' ) { |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
// Bail if the API hasn't been configured. |
| 119 |
$settings = new ConvertKit_Settings(); |
| 120 |
if ( ! $settings->has_access_and_refresh_token() ) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
return true; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Display the ConvertKit Forms dropdown when adding a Category |
| 129 |
* |
| 130 |
* @since 2.0.3 |
| 131 |
*/ |
| 132 |
public function add_category_form_fields() { |
| 133 |
|
| 134 |
// Don't show the form fields if the API hasn't been configured. |
| 135 |
$settings = new ConvertKit_Settings(); |
| 136 |
if ( ! $settings->has_access_and_refresh_token() ) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
// Fetch Forms. |
| 141 |
$convertkit_forms = new ConvertKit_Resource_Forms(); |
| 142 |
|
| 143 |
// Load view. |
| 144 |
include CONVERTKIT_PLUGIN_PATH . '/views/backend/term/fields-add.php'; |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Display the ConvertKit Forms dropdown when editing a Category |
| 150 |
* |
| 151 |
* @since 1.9.6 |
| 152 |
* |
| 153 |
* @param WP_Term $term Category. |
| 154 |
*/ |
| 155 |
public function edit_category_form_fields( $term ) { |
| 156 |
|
| 157 |
// Don't show the form fields if the API hasn't been configured. |
| 158 |
$settings = new ConvertKit_Settings(); |
| 159 |
if ( ! $settings->has_access_and_refresh_token() ) { |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
// Fetch Category Settings and Forms. |
| 164 |
$convertkit_term = new ConvertKit_Term( $term->term_id ); |
| 165 |
$convertkit_forms = new ConvertKit_Resource_Forms(); |
| 166 |
|
| 167 |
// Load view. |
| 168 |
include CONVERTKIT_PLUGIN_PATH . '/views/backend/term/fields-edit.php'; |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Save Term Settings. |
| 174 |
* |
| 175 |
* @since 1.9.6 |
| 176 |
* |
| 177 |
* @param int $term_id Term ID. |
| 178 |
*/ |
| 179 |
public function save_category_fields( $term_id ) { |
| 180 |
|
| 181 |
// Bail if no nonce field exists. |
| 182 |
if ( ! isset( $_POST['wp-convertkit-save-meta-nonce'] ) ) { |
| 183 |
return; |
| 184 |
} |
| 185 |
|
| 186 |
// Bail if the nonce verification fails. |
| 187 |
if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['wp-convertkit-save-meta-nonce'] ) ), 'wp-convertkit-save-meta' ) ) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
// Bail if no ConvertKit settings were posted. |
| 192 |
if ( ! isset( $_POST['wp-convertkit'] ) ) { |
| 193 |
return; |
| 194 |
} |
| 195 |
|
| 196 |
// Build metadata. |
| 197 |
$meta = array( |
| 198 |
'form' => ( isset( $_POST['wp-convertkit']['form'] ) ? intval( $_POST['wp-convertkit']['form'] ) : '' ), |
| 199 |
'form_position' => ( isset( $_POST['wp-convertkit']['form_position'] ) ? sanitize_text_field( wp_unslash( $_POST['wp-convertkit']['form_position'] ) ) : '' ), |
| 200 |
); |
| 201 |
|
| 202 |
// Save metadata. |
| 203 |
$convertkit_term = new ConvertKit_Term( $term_id ); |
| 204 |
return $convertkit_term->save( $meta ); |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
} |
| 209 |
|