PluginProbe
Powerkit – Supercharge your WordPress Site / 2.2.9
Powerkit – Supercharge your WordPress Site v2.2.9
3.1.1 3.1.0 3.0.9 3.0.8 trunk 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 All 87 releases
powerkit / modules / featured-categories / admin / class-powerkit-featured-categories-admin.php

class-powerkit-featured-categories-admin.php in Powerkit – Supercharge your WordPress Site 2.2.9, at modules/featured-categories/admin/class-powerkit-featured-categories-admin.php

210 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The admin-specific functionality of the module.
4 *
5 * @link https://codesupply.co
6 * @since 1.0.0
7 *
8 * @package Powerkit
9 * @subpackage Modules/Admin
10 */
11
12 /**
13 * The admin-specific functionality of the module.
14 */
15 class Powerkit_Featured_Categories_Admin extends Powerkit_Module_Admin {
16
17 /**
18 * Initialize
19 */
20 public function initialize() {
21 add_action( 'category_add_form_fields', array( $this, 'category_add_form_fields' ), 10 );
22 add_action( 'category_edit_form_fields', array( $this, 'category_edit_form_fields' ), 10, 2 );
23 add_action( 'created_category', array( $this, 'save_category' ), 10, 2 );
24 add_action( 'edited_category', array( $this, 'save_category' ), 10, 2 );
25 }
26
27 /**
28 * Add fields to Category
29 *
30 * @param string $taxonomy The taxonomy slug.
31 */
32 public function category_add_form_fields( $taxonomy ) {
33 wp_nonce_field( 'category_options', 'powerkit_category' );
34 ?>
35 <div class="form-field">
36 <label><?php esc_html_e( 'Featured Image', 'powerkit' ); ?></label>
37
38 <div class="pk-featured-image" data-frame-title="<?php esc_html_e( 'Select or upload image', 'powerkit' ); ?>" data-frame-btn-text="<?php esc_html_e( 'Set image', 'powerkit' ); ?>">
39 <p class="uploaded-img-box">
40 <span class="uploaded-image"></span>
41 <input id="powerkit_featured_image" class="uploaded-img-id" name="powerkit_featured_image" type="hidden"/>
42 </p>
43 <p class="hide-if-no-js">
44 <a class="upload-img-link button button-primary" href="#"><?php esc_html_e( 'Upload image', 'powerkit' ); ?></a>
45 <a class="delete-img-link button button-secondary hidden" href="#"><?php esc_html_e( 'Remove image', 'powerkit' ); ?></a>
46 </p>
47 </div>
48
49 <p><?php esc_html_e( 'This image is used in the category blocks.', 'powerkit' ); ?></p>
50 </div>
51 <br>
52 <?php
53 }
54
55 /**
56 * Edit fields from Category
57 *
58 * @param object $tag Current taxonomy term object.
59 * @param string $taxonomy Current taxonomy slug.
60 */
61 public function category_edit_form_fields( $tag, $taxonomy ) {
62 wp_nonce_field( 'category_options', 'powerkit_category' );
63
64 $powerkit_featured_image = get_term_meta( $tag->term_id, 'powerkit_featured_image', true );
65 ?>
66 <tr class="form-field">
67 <th scope="row" valign="top">
68 <label for="powerkit_featured_image"><?php esc_html_e( 'Featured Image', 'powerkit' ); ?></label>
69 </th>
70 <td>
71 <div class="pk-featured-image" data-frame-title="<?php esc_html_e( 'Select or upload image', 'powerkit' ); ?>" data-frame-btn-text="<?php esc_html_e( 'Set image', 'powerkit' ); ?>">
72 <p class="uploaded-img-box">
73 <span class="uploaded-image">
74 <?php if ( $powerkit_featured_image ) : ?>
75 <?php
76 echo wp_get_attachment_image( $powerkit_featured_image, 'large', false, array(
77 'style' => 'max-width:100%; height: auto;',
78 ) );
79 ?>
80 <?php endif; ?>
81 </span>
82
83 <input id="powerkit_featured_image" class="uploaded-img-id" name="powerkit_featured_image" type="hidden" value="<?php echo esc_attr( $powerkit_featured_image ); ?>" />
84 </p>
85 <p class="hide-if-no-js">
86 <a class="upload-img-link button button-primary <?php echo esc_attr( $powerkit_featured_image ? 'hidden' : '' ); ?>" href="#"><?php esc_html_e( 'Upload image', 'powerkit' ); ?></a>
87 <a class="delete-img-link button button-secondary <?php echo esc_attr( ! $powerkit_featured_image ? 'hidden' : '' ); ?>" href="#"><?php esc_html_e( 'Remove image', 'powerkit' ); ?></a>
88 </p>
89 </div>
90
91 <p class="description"><?php esc_html_e( 'This image is used in the category blocks.', 'powerkit' ); ?></p>
92 </td>
93 </tr>
94 <?php
95 }
96
97 /**
98 * Save category fields
99 *
100 * @param int $term_id ID of the term about to be edited.
101 * @param string $taxonomy Taxonomy slug of the related term.
102 */
103 public function save_category( $term_id, $taxonomy ) {
104
105 // Bail if we're doing an auto save.
106 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
107 return;
108 }
109
110 // if our nonce isn't there, or we can't verify it, bail.
111 if ( ! isset( $_POST['powerkit_category'] ) || ! wp_verify_nonce( $_POST['powerkit_category'], 'category_options' ) ) { // Input var ok; sanitization ok.
112 return;
113 }
114
115 if ( isset( $_POST['powerkit_featured_image'] ) ) { // Input var ok; sanitization ok.
116 $powerkit_featured_image = sanitize_text_field( $_POST['powerkit_featured_image'] ); // Input var ok; sanitization ok.
117
118 update_term_meta( $term_id, 'powerkit_featured_image', $powerkit_featured_image );
119 }
120 }
121
122 /**
123 * Register the stylesheets and JavaScript for the admin area.
124 *
125 * @param string $page Current page.
126 */
127 public function admin_enqueue_scripts( $page ) {
128 if ( 'edit-tags.php' === $page || 'term.php' === $page ) {
129 wp_enqueue_media();
130
131 ob_start();
132 ?>
133 <script>
134 ( function() {
135
136 var powerkitFeaturedContainer = '.pk-featured-image';
137
138 var powerkitFeaturedFrame;
139
140
141 jQuery( document ).ready( function( $ ) {
142
143 /* Add Image Link */
144 jQuery( powerkitFeaturedContainer ).find( '.upload-img-link' ).live( 'click', function( event ){
145 event.preventDefault();
146
147 var parentContainer = $( this ).parents( powerkitFeaturedContainer );
148
149 // Options.
150 var options = {
151 title: parentContainer.data( 'frame-title' ) ? parentContainer.data( 'frame-title' ) : 'Select or Upload Media',
152 button: {
153 text: parentContainer.data( 'frame-btn-text' ) ? parentContainer.data( 'frame-btn-text' ) : 'Use this media',
154 },
155 library : { type : 'image' },
156 multiple: false // Set to true to allow multiple files to be selected.
157 };
158
159 // Create a new media frame
160 powerkitFeaturedFrame = wp.media( options );
161
162 // When an image is selected in the media frame...
163 powerkitFeaturedFrame.on( 'select', function() {
164
165 // Get media attachment details from the frame state.
166 var attachment = powerkitFeaturedFrame.state().get('selection').first().toJSON();
167
168 // Send the attachment URL to our custom image input field.
169 parentContainer.find( '.uploaded-image' ).html( '<img src="' + attachment.url + '" style="max-width:100%;"/>' );
170 parentContainer.find( '.uploaded-img-id' ).val( attachment.id ).change();
171 parentContainer.find( '.upload-img-link' ).addClass( 'hidden' );
172 parentContainer.find( '.delete-img-link' ).removeClass( 'hidden' );
173
174 powerkitFeaturedFrame.close();
175 });
176
177 // Finally, open the modal on click.
178 powerkitFeaturedFrame.open();
179 });
180
181
182 /* Delete Image Link */
183 $( powerkitFeaturedContainer ).find( '.delete-img-link' ).live( 'click', function( event ){
184 event.preventDefault();
185
186 $( this ).parents( powerkitFeaturedContainer ).find( '.uploaded-image' ).html( '' );
187 $( this ).parents( powerkitFeaturedContainer ).find( '.upload-img-link' ).removeClass( 'hidden' );
188 $( this ).parents( powerkitFeaturedContainer ).find( '.delete-img-link' ).addClass( 'hidden' );
189 $( this ).parents( powerkitFeaturedContainer ).find( '.uploaded-img-id' ).val( '' ).change();
190 });
191 });
192
193 jQuery( document ).ajaxSuccess(function(e, request, settings){
194 let action = settings.data.indexOf( 'action=add-tag' );
195 let screen = settings.data.indexOf( 'screen=edit-category' );
196 let taxonomy = settings.data.indexOf( 'taxonomy=category' );
197
198 if( action > -1 && screen > -1 && taxonomy > -1 ){
199 $( powerkitFeaturedContainer ).find( '.delete-img-link' ).click();
200 }
201 });
202
203 } )();
204 </script>
205 <?php
206 wp_add_inline_script( 'jquery-core', str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
207 }
208 }
209 }
210