-
Notifications
You must be signed in to change notification settings - Fork 0
/
mv-category-media.php
142 lines (105 loc) · 3.79 KB
/
mv-category-media.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/*
Plugin Name: MV Category Media
Plugin URI: https://github.com/jar-laks/mv-category-media
Description: Creates additional media fields for default category
Version: 0.1
Author: Maks Viter
Author URI: https://github.com/jar-laks
*/
define( 'MV_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
require_once( MV_PLUGIN_PATH . '/db/MVCategoryFieldsDB.php' );
require_once( MV_PLUGIN_PATH . '/fields/MVCategoryFields.php' );
require_once( MV_PLUGIN_PATH . '/fields/MVImageField.php' );
require_once( MV_PLUGIN_PATH . '/fields/MVVideoField.php' );
require_once( MV_PLUGIN_PATH . '/fields/integration/MVVideoIntegrations.php' );
register_activation_hook( __FILE__, array( 'MVCategoryFieldsDB', 'create_db_table' ) );
register_uninstall_hook( __FILE__, array( 'MVCategoryFieldsDB', 'drop_table' ) );
class MVCategoryMedia {
protected static $instance = null;
protected function __construct() {
add_action( 'admin_menu', array( $this, 'create_menu' ) );
}
public function create_menu() {
add_menu_page( 'MV Plugin Settings', 'MV Settings', 'administrator', __FILE__, array(
$this,
'settings_page'
), 'dashicons-playlist-video' );
add_action( 'admin_init', array( $this, 'register_settings' ) );
}
public static function get_instance() {
if ( static::$instance == null ) {
static::$instance = new static;
}
return static::$instance;
}
function register_settings() {
register_setting( 'mv-settings-group', 'mv_youtube_api_key' );
}
function settings_page() {
?>
<div class="wrap">
<h2>MV Category Media</h2>
<form method="post" action="options.php">
<?php settings_fields( 'mv-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?= __( 'Youtube API key' ) ?></th>
<td><p id="status_key">Key status</p>
<input type="text" name="mv_youtube_api_key"
value="<?php echo get_option( 'mv_youtube_api_key' ); ?>"/></td>
<a class="button-primary" id="check-api-key">Check Api Key</a>
<script>
jQuery(function () {
var $check_btn = jQuery('#check-api-key');
var check_status = function () {
var $key = jQuery('[name="mv_youtube_api_key"]').val();
var status_key = jQuery('#status_key');
jQuery.ajax({
method: "GET",
url: "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q=video&key=" + $key,
error: function (responce) {
status_key.css('color', 'red').text('Incorrect Key');
},
success: function (response) {
status_key.css('color', 'green').text('Key OK');
}
})
};
check_status();
$check_btn.on('click', function(e){
e.preventDefault();
check_status();
});
});
</script>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e( 'Save Changes' ) ?>"/>
</p>
</form>
</div>
<?php }
}
MVCategoryMedia::get_instance();
MVCategoryFields::get_instance();
add_filter( 'mv_additional_cat_meta', function ( $cat_id ) {
$category_img = MVImageField::get_value( $cat_id );
$category_video = MVVideoField::get_value( $cat_id );
$video_html = '';
if ( $category_video ) {
$video_html = MVVideoIntegrations::get_video_embed_url_not_auto( $category_video );
}
$meta_html = apply_filters( 'mv_additional_cat_video_html', $video_html );
$img_html = wp_get_attachment_image( $category_img, 'large' );
$meta_html .= apply_filters( 'mv_additional_cat_img_html', $img_html );
return $meta_html;
}, 1 );
add_filter( 'category_description', function ( $content, $cat_id ) {
$meta_content = '';
if ( ! is_admin() ) {
$meta_content = apply_filters( 'mv_additional_cat_meta', $cat_id );
}
return $meta_content . $content;
}, 1, 2 );