This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
acf-google_font_selector.php
executable file
·165 lines (141 loc) · 4.53 KB
/
acf-google_font_selector.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/*
Plugin Name: Advanced Custom Fields: Google Font Selector
Plugin URI: https://github.com/danielpataki/ACF-Google-Font-Selector
Description: A field for Advanced Custom Fields which allows users to select Google fonts with advanced options
Version: 3.0.1
Author: Daniel Pataki
Author URI: http://danielpataki.com
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
// Include Common Functions
include_once('functions.php');
add_action('plugins_loaded', 'acfgfs_load_textdomain');
/**
* Load Text Domain
*
* Loads the textdomain for translations
*
* @author Daniel Pataki
* @since 3.0.0
*
*/
function acfgfs_load_textdomain() {
load_plugin_textdomain( 'acf-google-font-selector-field', false, dirname( plugin_basename(__FILE__) ) . '/lang/' );
}
add_action('acf/include_field_types', 'include_field_types_google_font_selector');
/**
* ACF 5 Field
*
* Loads the field for ACF 5
*
* @author Daniel Pataki
* @since 3.0.0
*
*/
function include_field_types_google_font_selector( $version ) {
include_once('acf-google_font_selector-v5.php');
}
add_action('acf/register_fields', 'register_fields_google_font_selector');
/**
* ACF 4 Field
*
* Loads the field for ACF 4
*
* @author Daniel Pataki
* @since 3.0.0
*
*/
function register_fields_google_font_selector() {
include_once('acf-google_font_selector-v4.php');
}
add_action( 'admin_notices', 'acfgfs_setup_nag' );
/**
* API Key Nag
*
* Displays a message promting users to supply the Google API key. It can be
* added in the settings page, or it can be defined as a constant.
*
* @author Daniel Pataki
* @since 3.0.0
*
*/
function acfgfs_setup_nag() {
if( !defined( 'ACFGFS_API_KEY' ) && !get_option('acfgfs_api_key') ) :
?>
<div class="update-nag">
<p><?php echo sprintf( __( 'The Google Font Selector Field requires an Google API key to work. You can set your API key on the <a href="%s">options page</a>. If you need help getting an API key <a href="%s">click here</a>', 'acf-google-font-selector-field' ), admin_url( 'options-general.php?page=acfgfs-settings' ), 'https://wordpress.org/plugins/acf-google-font-selector-field/' ); ?></p>
</div>
<?php
endif;
}
add_action('admin_menu', 'acfgfs_settings_page');
/**
* Add Setting Page
*
* Adds the settings page which contains the field for the Google API Key
* Also initializes the settings that holds the value.
*
* @author Daniel Pataki
* @since 3.0.0
*
*/
function acfgfs_settings_page() {
if( !defined('ACFGFS_API_KEY') ) {
add_options_page( _x( 'Google Font Selector', 'In the title tag of the page', 'acf-google-font-selector-field' ), _x( 'Google Font Selector', 'Menu title', 'acf-google-font-selector-field' ), 'manage_options', 'acfgfs-settings', 'acfgfs_settings_page_content');
add_action( 'admin_init', 'acfgfs_register_settings' );
}
}
/**
* Register Settings
*
* Registers plugin-wide settings, we use this for the Google API key
*
* @author Daniel Pataki
* @since 1.0.0
*
*/
function acfgfs_register_settings() {
register_setting( 'acfgfs', 'acfgfs_api_key' );
}
/**
* Settings Page Content
*
* The UI for the settings page. It contains the form, as well as
* a quick check to make sure the given credentials work.
*
* @author Daniel Pataki
* @since 1.0.0
*
*/
function acfgfs_settings_page_content() {
?>
<div class="wrap">
<h2><?php _e( 'Google Font Selector', 'acf-google-font-selector-field' ) ?></h2>
<form method="post" action="options.php">
<?php settings_fields( 'acfgfs' ); ?>
<?php do_settings_sections( 'acfgfs' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php _e( 'Google API Key', 'acf-google-font-selector-field' ) ?></th>
<td><input type="text" name="acfgfs_api_key" value="<?php echo esc_attr( get_option('acfgfs_api_key') ); ?>" /></td>
</tr>
</table>
<?php
$api_key = esc_attr( get_option('acfgfs_api_key') );
if( !empty( $_GET['settings-updated'] ) && !empty( $api_key ) ) {
$request = wp_remote_get( 'https://www.googleapis.com/webfonts/v1/webfonts?key=' . $api_key );
$response = json_decode( $request['body'], true );
if( !empty( $response['error']['errors'] ) ) {
echo '<div id="acfgfs-api-key-error" class="error acfgfs-error">
<p><strong>' . __( 'We tried to verify your API key but it seems it is incorrect. Make sure to copy-paste it from Google exactly.', 'acf-google-font-selector-field' ) . '</strong></p></div>';
}
}
?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
?>