This repository was archived by the owner on Dec 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathacf-relationship-create-pro.php
More file actions
391 lines (330 loc) · 14 KB
/
acf-relationship-create-pro.php
File metadata and controls
391 lines (330 loc) · 14 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
/*
Plugin Name: Quick and easy Post creation for ACF Relationship Fields PRO
Description: Quick & Easy post creation on your Advanced Custom Fields (ACF) Relationship Fields (PRO version)
Author: Bazalt
Version: 3.2
Author URI: http://bazalt.fr/
Text Domain: acf-relationship-create
Domain Path: /languages/
*/
if ( ! class_exists( 'ACF_Relationship_Create_Pro' ) ) :
class ACF_Relationship_Create_Pro
{
private static $_instance;
/**
* Singleton pattern
* @return ACF_Relationship_Create_Pro
*/
public static function getInstance() {
if( self::$_instance instanceof self ) return self::$_instance;
self::$_instance = new self();
return self::$_instance;
}
/**
* Avoid creation of an instance from outside
*/
private function __clone() {}
/**
* Private constructor (part of singleton pattern)
* Declare WordPress Hooks
*/
private function __construct() {
// Load the plugin's translated strings
add_action( 'plugins_loaded', array( $this, 'load_text_domain' ) );
// Init
add_action(
'init',
array($this, 'init'),
6 // Right after ACF
);
}
/**
* Load the plugin's translated strings
*
* @hook action plugins_loaded
*/
public function load_text_domain() {
load_plugin_textdomain( 'acf-relationship-create', false, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
}
/**
* Check if ACF is installed
*
* @return bool
*/
public static function is_acf_installed() {
return ( class_exists( 'acf' ) || class_exists( 'ACF' ) );
}
/**
* Check if ACF version is PRO
*
* @return bool
*/
public static function is_acf_pro_version() {
return class_exists( 'acf_pro' );
}
/**
* Admin notice if ACF version isn't PRO
*
* @hook action admin_notices
* @see ACF_Relationship_Create_Free::register_assets
*/
public function admin_notice_bad_ACF_version() {
?>
<div class="notice notice-error is-dismissible">
<p>
<?php
printf(
__( 'You are using the free version of Advanced Custom Fields plugin. You have to downgrade `Advanced Custom Fields Relationship Create` plugin <a href="%s" target="_blank">to the FREE version</a> too!', 'acf-relationship-create' ),
'https://wordpress.org/plugins/quick-and-easy-post-creation-for-acf-relationship-fields/'
);
?>
</p>
</div>
<?php
}
/**
* Init method, called right after ACF
*
* @hook action init
*/
public function init() {
// Stop here if ACF isn't installed
if( !self::is_acf_installed() )
return;
// Bail early with an error notice if ACF version isn't PRO
if( !self::is_acf_pro_version() ) {
add_action( 'admin_notices', array( $this, 'admin_notice_bad_ACF_version' ) );
return;
}
/**
* Register scripts
*/
// Tools
wp_register_script(
'acf-relationship-create-pro',
plugins_url('assets/js/acf-relationship-create' . ( ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min' ) . '.js', __FILE__),
array( 'jquery' ),
'3.2'
);
// Relationship field script
wp_register_script(
'acf-relationship-create-pro-field',
plugins_url('assets/js/acf-relationship-create-field' . ( ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min' ) . '.js', __FILE__),
array( 'acf-relationship-create-pro', 'thickbox', 'acf-input' ),
'3.2'
);
wp_register_script(
'acf-relationship-create-pro-field-5.7',
plugins_url('assets/js/acf-relationship-create-field-5.7' . ( ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min' ) . '.js', __FILE__),
array( 'acf-relationship-create-pro', 'thickbox', 'acf-input' ),
'3.2'
);
// iframe script
wp_register_script(
'acf-relationship-create-pro-iframe',
plugins_url('assets/js/acf-relationship-create-iframe' . ( ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min' ) . '.js', __FILE__),
array( 'acf-relationship-create-pro' ),
'3.2'
);
wp_register_style(
'acf-relationship-create-pro',
plugins_url( 'assets/css/acf-relationship-create.css', __FILE__ ),
array( 'acf-input', 'thickbox' ),
'3.2'
);
/**
* Admin enqueue scripts
*/
add_action(
'admin_enqueue_scripts',
array( $this, 'admin_scripts'),
11 // Right after ACF
);
/**
* ACF Hooks
*/
// Enqueue assets for ACF fields
add_action( 'acf/input/admin_enqueue_scripts', array( $this, 'enqueue_acf_assets' ), 11 ); // Just after ACF scripts
// Alter query params for AJAX calls on ACF Relationship fields
add_filter( 'acf/fields/relationship/query', array( $this, 'acf_field_alter_ajax' ), 10, 3 );
// Add new setting for ACF relationship fields
add_action( 'acf/render_field_settings/type=relationship', array( $this, 'acf_relationship_settings' ), 50);
add_action( 'acf/render_field_settings/type=post_object', array( $this, 'acf_relationship_settings' ), 50);
// Alter markup of ACF relationship fields
add_action( 'acf/render_field/type=relationship', array( $this, 'acf_render_relationship_field' ), 10, 1 );
add_action( 'acf/render_field/type=post_object', array( $this, 'acf_render_relationship_field' ), 10, 1 );
}
public function isGutenbergEnabledOnCPT( $post_type ) {
$post_type_object = get_post_type_object( $post_type );
if( empty( $post_type_object ) )
return false;
if( !isset( $post_type_object->show_in_rest ) || $post_type_object->show_in_rest !== true )
return false;
if( !is_array( $post_type_object->supports ) || !in_array( 'editor', $post_type_object->supports ) )
return false;
return true;
}
/**
* Include scripts
*
* @hook action admin_enqueue_scripts
*
* @param $hook
*/
public function admin_scripts( $hook ) {
$include_in = array( 'post.php' );
// Check if Gutenberg is enabled on this post type.
// In this only case, post-new.php hook must be included.
$current_screen = get_current_screen();
if( !empty( $current_screen->post_type ) && $this->isGutenbergEnabledOnCPT( $current_screen->post_type ) ) {
$include_in[] = 'post-new.php';
}
if( in_array( $hook, $include_in ) ) {
wp_enqueue_script( 'acf-relationship-create-pro-iframe' );
}
}
/**
* Enqueue assets for ACF fields
*
* @hook action acf/input/admin_enqueue_scripts
*/
public function enqueue_acf_assets() {
$script_name = 'acf-relationship-create-pro-field';
if( defined('ACF_VERSION') && (float) ACF_VERSION >= 5.7 )
$script_name.= '-5.7';
include_once( ABSPATH . WPINC . '/version.php' );
global $wp_version;
wp_enqueue_script( $script_name );
wp_localize_script(
$script_name,
'acf_relationship_create_pro_field',
array(
'i18n' => array(
'no_title' => __( '(No title)', 'acf-relationship-create' )
),
'ACF' => array(
'version' => ( defined('ACF_VERSION') ? ACF_VERSION : 0 )
),
'wp' => array(
'version' => $wp_version
)
)
);
wp_enqueue_style( 'acf-relationship-create-pro' );
}
/**
* Alter query params for AJAX calls on ACF Relationship fields
*
* @hook filter acf/fields/relationship/query
*
* @param $args
* @param $field
* @param $post_id
* @return mixed
*/
public function acf_field_alter_ajax( $args, $field, $post_id ) {
if( empty( $_POST['acf_relationship_created_post_id'] ) ) return $args;
$post_params = explode( '-', $_POST['acf_relationship_created_post_id'] );
$created_post_id = absint( $post_params[0] );
if( empty( $created_post_id ) ) return $args;
if( !empty( $args['post_type'] ) ) {
// We're only looking for this particular post ID
$args['p'] = $created_post_id;
unset($args['s']);
unset($args['tax_query']);
}
return $args;
}
/**
* Alter markup of ACF relationship fields
*
* @hook action acf/render_field/type=relationship
*
* @param $field
*/
public function acf_render_relationship_field( $field ) {
if( empty( $field['acf_relationship_create'] ) ) return;
$post_types = empty( $field['post_type'] ) ? acf_get_post_types() : $field['post_type'];
if( empty( $post_types ) ) return;
$tooltip_links = array();
foreach( $post_types as $post_type ) {
if( $post_type == 'attachment' ) continue;
$post_type_obj = get_post_type_object( $post_type );
if( !user_can( get_current_user_id(), $post_type_obj->cap->create_posts ) )
continue;
$tooltip_links[ $post_type ] = array(
'label' => $post_type_obj->labels->singular_name,
'url' => admin_url(
add_query_arg(
array(
'acf_rc_original_field_uniqid' => '__acf_rc_original_field_uniqid__', // token that will be replaced dynamically in JS
'acf_rc_from_content_type' => '__acf_rc_from_content_type__',
'acf_rc_from_content_ID' => '__acf_rc_from_content_ID__',
'TB_iframe' => 1 // Force loading as iframe
),
'post-new.php?post_type=' . $post_type
)
)
);
}
$tooltip_links = apply_filters( 'acf-relationship-create/tooltip_links', $tooltip_links, $field );
if( empty( $tooltip_links ) ) return;
?>
<a href="#" class="acf-relationship-create-link">
<span class="dashicons dashicons-plus"></span>
<span class="screen-reader-text"><?php esc_html_e( 'Create', 'acf-relationship-create' ); ?></span>
</a>
<div>
<input type="hidden"
name="acf-relationship-created_post_id"
data-field-type="<?php echo esc_attr( $field['type'] ); ?>"
data-filter="acf_relationship_created_post_id" />
</div>
<script type="text-html" class="acf-rc-popup-wrapper">
<div id="acf-rc-popup"
data-field-type="<?php echo esc_attr( $field['type'] ); ?>">
<ul>
<?php foreach( $tooltip_links as $post_type => $post_type_data ) : ?>
<li>
<a href="#"
data-create-url="<?php echo esc_attr( $post_type_data['url'] ); ?>"
title="<?php printf( esc_attr__( 'Create new %s', 'acf-relationship-create' ), $post_type_data['label'] ); ?>">
<?php echo $post_type_data['label']; ?>
<span class="status"></span>
</a>
</li>
<?php endforeach; ?>
</ul>
<a href="#" class="focus"></a>
</div>
</script>
<?php
}
/**
* Add new setting for ACF relationship fields
*
* @hook action acf/render_field_settings/type=relationship
* @hook action acf/render_field_settings/type=post_object
*
* @param $field
*/
public function acf_relationship_settings( $field ) {
acf_render_field_wrap( array(
'label' => __( 'Display a link to create content on the fly?', 'acf-relationship-create' ),
'instructions' => '',
'type' => 'radio',
'name' => 'acf_relationship_create',
'prefix' => $field['prefix'],
'value' => $field['acf_relationship_create'],
'choices' => array(
0 => __("No",'acf'),
1 => __("Yes",'acf'),
),
'layout' => 'horizontal',
'class' => 'field-acf_relationship_create'
), 'tr');
}
}
ACF_Relationship_Create_Pro::getInstance();
endif;