-
Notifications
You must be signed in to change notification settings - Fork 13
/
gravityforms-update-post.php
1335 lines (1185 loc) · 38.6 KB
/
gravityforms-update-post.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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @link https://github.com/jupitercow/gravity-forms-post-updates
* @since 1.2.18
* @package gform_update_post
*
* @wordpress-plugin
* Plugin Name: Gravity Forms: Post Updates
* Plugin URI: https://wordpress.org/plugins/gravity-forms-post-updates/
* Description: Allow Gravity Forms to update post content and the meta data associated with a post.
* Version: 1.2.23
* Author: Jupitercow
* Author URI: http://Jupitercow.com/
* Contributer: ekaj
* Contributer: jr00ck
* Contributer: p51labs
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: gform_update_post
* Domain Path: /languages
*/
if (! class_exists('gform_update_post') ) :
add_action( 'init', array('gform_update_post', 'init') );
class gform_update_post
{
/**
* Class prefix
*
* @since 1.2
* @var string
*/
const PREFIX = __CLASS__;
/**
* Current version of plugin
*
* @since 1.2
* @var string
*/
const VERSION = '1.2.23';
/**
* Settings
*
* @since 0.6.1
* @var string
*/
public static $settings = array();
/**
* Holds the post to update
*
* @since 0.6.1
* @var string
*/
private static $post;
/**
* Holds the form info
*
* @since 0.6.1
* @var string
*/
private static $form;
/**
* Initialize the Class
*
* Add filters and actions and set up the options.
*
* @author ekaj
*/
public static function init()
{
self::setup();
// actions
add_action( 'admin_init', array(__CLASS__, 'admin_init') );
// filters
add_filter( 'shortcode_atts_gravityforms', array(__CLASS__, 'gf_shortcode_atts'), 10, 3 );
}
/**
* Admin init
*
* @author ekaj
* @since 1.2
*/
public static function admin_init()
{
if (! self::test_requirements() )
{
$plugin_data = get_plugin_data(__FILE__);
self::$settings['name'] = $plugin_data['Name'];
add_action( 'admin_notices', array(__CLASS__, 'admin_warnings'), 20);
}
}
/**
* Add support for the new update attribute in the shortcode
*
* @author ekaj
* @author jr00ck
* @since 1.2
*/
public static function gf_shortcode_atts( $out, $pairs, $atts )
{
$require_link = ( isset($atts['require_link']) || in_array('require_link', $atts) ) ? true : false;
$post_id = false;
if ( $require_link )
{
$request_key = apply_filters(self::PREFIX . '/request_id', self::$settings['request_id']);
if (! empty($_REQUEST[$request_key]) ) {
$post_id = $_REQUEST[$request_key];
}
}
if (! $require_link || ($require_link && $post_id) )
{
if ( isset($atts['update']) )
{
if ( is_numeric($atts['update']) )
{
if (! $post_id ) {
$post_id = $atts['update'];
}
if ( self::current_user_can( $post_id ) ) {
do_action( self::PREFIX . '/setup_form', array('form_id'=>$atts['id'], 'post_id'=>$post_id) );
}
}
elseif ( 'false' == $atts['update'] )
{
remove_filter( 'gform_form_tag', array(__CLASS__, 'gform_form_tag') );
remove_filter( 'gform_pre_render_' . $atts['id'], array(__CLASS__, 'gform_pre_render') );
remove_filter( 'gform_pre_render', array(__CLASS__, 'gform_pre_render') );
}
}
elseif ( in_array('update', $atts) )
{
// Get the current post id, if none is provided
if (! $post_id ) {
$post_id = (! empty($GLOBALS['post']->ID) ) ? $GLOBALS['post']->ID : false;
}
if ( self::current_user_can( $post_id ) ) {
do_action( self::PREFIX . '/setup_form', array('form_id'=>$atts['id'], 'post_id'=>$post_id) );
}
}
}
if ( (isset($atts['update']) || in_array('update', $atts)) && ! self::current_user_can($post_id) ) {
$out['action'] = 0;
}
return $out;
}
/**
* Set up the Class
*
* Set up options and check if a URL variable is sent.
*
* @author ekaj
*/
public static function setup()
{
if ( self::test_requirements() )
{
add_filter( self::PREFIX . '/settings/get_path', array(__CLASS__, 'helpers_get_path'), 1 );
add_filter( self::PREFIX . '/settings/get_dir', array(__CLASS__, 'helpers_get_dir'), 1 );
self::$settings = array(
'request_id' => apply_filters( self::PREFIX . '/request_id', 'gform_post_id' ),
'nonce_delete' => self::PREFIX . '_delete_upload',
'nonce_update' => self::PREFIX . '_update_post',
'file_width' => 46,
'file_height' => 60,
'path' => apply_filters( self::PREFIX . '/settings/get_path', __FILE__ ),
'dir' => apply_filters( self::PREFIX . '/settings/get_dir', __FILE__ ),
'unique_field' => 'field_unique_custom_meta_value'
);
self::$settings = array_merge( self::$settings, apply_filters( self::PREFIX . '/settings', self::$settings ) );
// Adds support for unique custom fields
add_action( 'gform_field_standard_settings', array(__CLASS__, 'gform_field_standard_settings'), 10, 2 );
add_action( 'gform_editor_js', array(__CLASS__, 'gform_editor_js') );
add_filter( 'gform_tooltips', array(__CLASS__, 'gform_tooltips') );
// Custom post types plugin doesn't update taxonomies, it just adds to them, so you have to delete first
add_filter( 'gform_after_submission', array(__CLASS__, 'delete_custom_taxonomy_save'), 1, 2 );
// Update validation for file/image upload
add_filter( 'gform_field_validation', array(__CLASS__, 'required_upload_field_validation'), 10, 4 );
// Adds a really basic shortcode to set the plugin in action
add_shortcode( self::PREFIX, array(__CLASS__, 'shortcode') );
// Add an action to set up the form
add_action( self::PREFIX . '/setup_form', array(__CLASS__, 'setup_form') );
// Add a filter to get an edit url
add_filter( self::PREFIX . '/edit_url', array(__CLASS__, 'get_edit_url'), 10, 2 );
// Add a filter to get an edit link
add_filter( self::PREFIX . '/get_edit_link', array(__CLASS__, 'get_edit_link'), 99 );
// Adds a really basic shortcode to set the plugin in action
add_shortcode( self::PREFIX . '_edit_link', array(__CLASS__, 'shortcode_edit_link') );
// Add an action to create a link
add_action( self::PREFIX . '/edit_link', array(__CLASS__, 'edit_link') );
// Ajax file delete
add_action( 'wp_ajax_' . self::PREFIX . '_delete_upload', array(__CLASS__, 'ajax_delete_upload') );
if ( apply_filters( self::PREFIX . '/public_file_delete', true ) ) {
add_action( 'wp_ajax_nopriv_' . self::PREFIX . '_delete_upload', array(__CLASS__, 'ajax_delete_upload') );
}
// Set up from url query vars and process submitted forms.
self::process_request();
}
}
/**
* Get the plugin path
*
* Calculates the path (works for plugin / theme folders). These functions are from Elliot Condon's ACF plugin.
*
* @since: 0.6
*/
public static function helpers_get_path( $file )
{
return trailingslashit( dirname($file) );
}
/**
* Get the plugin directory
*
* Calculates the directory (works for plugin / theme folders). These functions are from Elliot Condon's ACF plugin.
*
* @since: 0.6
*/
public static function helpers_get_dir( $file )
{
$dir = trailingslashit( dirname($file) );
$count = 0;
// sanitize for Win32 installs
$dir = str_replace('\\' ,'/', $dir);
// if file is in plugins folder
$wp_plugin_dir = str_replace('\\' ,'/', WP_PLUGIN_DIR);
$dir = str_replace($wp_plugin_dir, plugins_url(), $dir, $count);
if ( $count < 1 )
{
// if file is in wp-content folder
$wp_content_dir = str_replace('\\' ,'/', WP_CONTENT_DIR);
$dir = str_replace($wp_content_dir, content_url(), $dir, $count);
}
if ( $count < 1 )
{
// if file is in ??? folder
$wp_dir = str_replace('\\' ,'/', ABSPATH);
$dir = str_replace($wp_dir, site_url('/'), $dir);
}
return $dir;
}
/**
* Just returns the request_id
*
* @author ekaj
*/
public static function request_id()
{
return apply_filters( self::PREFIX . '/request_id', self::$settings['request_id'] );
}
/**
* Make sure that any neccessary dependancies exist
*
* @author ekaj
* @return bool
*/
public static function test_requirements()
{
// Look for GF
if (! class_exists('RGForms') ) {
return false;
}
// Make sure the Form Model is there also
if (! class_exists('GFFormsModel') ) {
return false;
}
// Look for the GFCommon object
if (! class_exists('GFCommon') ) {
return false;
}
return true;
}
/**
* If Gravity Forms isn't installed, add an error to let user know this won't be useable.
*
* @author ekaj
* @return void
*/
public static function admin_warnings()
{
$message = sprintf( __('<strong>%s</strong> requires Gravity Forms to be installed. Please <a href="http://www.gravityforms.com/">download the latest version</a> to use this plugin.', self::PREFIX), self::$settings['name'] );
?>
<div class="error">
<p>
<?php echo $message; ?>
</p>
</div>
<?php
}
public static function scripts_and_styles()
{
// register acf scripts
wp_register_script( self::PREFIX, plugins_url( 'js/scripts.js', __FILE__ ), array('jquery'), self::VERSION );
$args = array(
'url' => admin_url( 'admin-ajax.php' ),
'action' => self::PREFIX . '_delete_upload',
'prefix' => self::PREFIX,
'spinner' => admin_url( 'images/loading.gif' ),
'nonce' => wp_create_nonce( self::$settings['nonce_delete'] )
);
wp_localize_script( self::PREFIX, 'gform_up', $args );
wp_enqueue_script( array(
self::PREFIX
) );
}
/**
* Manage URL Query
*
* Check if a url variable has been submitted correctly, and then trigger.
*
* @author ekaj
* @return void
*/
public static function process_request()
{
$post_id = false;
$request_key = apply_filters(self::PREFIX . '/request_id', self::$settings['request_id']);
if (! empty($_REQUEST[$request_key]) ) {
$post_id = $_REQUEST[$request_key];
}
if ( $post_id && is_numeric($post_id) )
{
if ( 'POST' == $_SERVER['REQUEST_METHOD'] || (! empty($_REQUEST['nonce']) && wp_verify_nonce($_REQUEST['nonce'], self::$settings['nonce_update'])) ) {
do_action( self::PREFIX . '/setup_form', $post_id );
}
}
}
/**
* Get the Post Object
*
* Used to get the post from id along with taxonomies.
*
* @author p51labs
* @author ekaj
* @param int $post_id
* @return void
*/
public static function get_post_object( $post_id )
{
self::$post = get_post($post_id);
self::$post->taxonomies = array();
self::get_post_taxonomies();
}
/**
* Add taxonomies to post object
*
* @author ekaj
* @return void
*/
public static function get_post_taxonomies()
{
if (! is_object(self::$post) ) return;
$taxonomies = get_object_taxonomies(self::$post->post_type);
foreach ( $taxonomies as $taxonomy )
{
$key = $taxonomy;
if ( 'post_tag' == $taxonomy ) {
$key = 'post_tags';
}
if ( 'category' == $taxonomy ) {
$key = 'post_category';
}
self::$post->taxonomies[$key] = wp_get_object_terms(self::$post->ID, $taxonomy);
}
}
/**
* Build Edit URI
*
* Create a url with GET variables to the form for editing.
*
* @author ekaj
* @param int $post_id ID of the post you want to edit
* @param string $url By default the permalink of the post that you want to edit is used, use this to send to a different page to edit the post whose id is provided
* @return void
*/
public static function get_edit_url( $post_id=false, $url=false )
{
if (! $post_id && ! empty($GLOBALS['post']) ) {
$post_id = $GLOBALS['post']->ID;
}
// If the url parameter is a post_id, get the url to that post
if ( is_numeric($url) ) {
$url = get_permalink($url);
}
// If no url, use the post_id to get the url to the post being edited
if (! $url ) {
$url = get_permalink($post_id);
}
$request_id = apply_filters(self::PREFIX . '/request_id', self::$settings['request_id']);
return add_query_arg( array($request_id => $post_id, 'nonce' => wp_create_nonce(self::$settings['nonce_update'])), $url );
}
/**
* Build Edit Link and return
*
* Create anchor link with the edit URI. Uses self::edit_url to create the URI.
*
* Arguments:
* post_id (int) is the id of the post you want to edit
* url (string|int) is either the full url of the page where your edit form resides, or an id for the page where the edit form resides
* text (string) is the link text
* title (string) is the title attribute of the anchor tag
*
* @author ekaj
* @since 1.2.7
* @param array|string $args The arguments to use when creating a link
* @return void
*/
public static function get_edit_link( $args=array() )
{
$defaults = array(
'post_id' => false,
'url' => false,
'text' => __("Edit Post", self::PREFIX),
'title' => false,
'class' => '',
);
$args = wp_parse_args( $args, $defaults );
$output = '';
// Get the current post id, if none is provided
if (! $args['post_id'] && ! empty($GLOBALS['post']->ID) ) {
$args['post_id'] = $GLOBALS['post']->ID;
}
$request_key = apply_filters(self::PREFIX . '/request_id', self::$settings['request_id']);
if ( self::current_user_can( $args['post_id'] ) && empty($_REQUEST[$request_key]) )
{
// Add the link text to the title if no link title is specified
if (! $args['title'] ) {
$args['title'] = $args['text'];
}
$output .= '<a class="' . esc_attr(self::PREFIX) . '_link' . ($args['class'] ? ' ' . esc_attr($args['class']) : '') . '" href="' . esc_attr( apply_filters(self::PREFIX.'/edit_url', $args['post_id'], $args['url']) ) . '" title="' . esc_attr($args['title']) . '">' . esc_html($args['text']) . '</a>';
}
return $output;
}
/**
* Build Edit Link
*
* Create anchor link with the edit URI. Uses self::edit_url to create the URI.
*
* @author ekaj
* @param array|string $args The arguments to use when creating a link
* @return void
*/
public static function edit_link( $args=array() )
{
echo apply_filters( self::PREFIX . '/get_edit_link', $args );
}
/**
* Create a link to edit a post
*
* @author ekaj
* @since 1.2.6
* @type shortcode
* @return void
*/
public static function shortcode_edit_link( $atts )
{
$args = shortcode_atts( array(
'post_id' => false,
'url' => false
), $atts );
return apply_filters( self::PREFIX . '/get_edit_link', $args );
}
/**
* Create a simple short code to setup a form
*
* Set up a form on a post or page to be editable.
*
* @author ekaj
* @type shortcode
* @return void
*/
public static function shortcode( $atts )
{
extract( shortcode_atts( array(
'post_id' => false,
), $atts ) );
do_action( self::PREFIX . '/setup_form', $post_id );
}
/**
* Set Up Form
*
* Sets up a form from post id for editing.
*
* @author ekaj
* @param int $post_id id of the post you want to edit
* @return void
*/
public static function setup_form( $args=array() )
{
if ( is_numeric($args) )
{
$post_id = $args;
$form_id = false;
}
elseif ( is_array($args) )
{
$defaults = array(
'post_id' => 0,
'form_id' => 0,
);
$args = wp_parse_args( $args, $defaults );
extract($args);
}
else
{
return false;
}
if (! $post_id && ! empty($GLOBALS['post']->ID) ) {
$post_id = $GLOBALS['post']->ID;
}
self::get_post_object($post_id);
if ( is_object(self::$post) )
{
// Make sure taxonomies get set up
add_action( 'wp', array(__CLASS__, 'get_post_taxonomies') );
// Load scripts and styles
add_action( 'gform_enqueue_scripts', array(__CLASS__, 'scripts_and_styles') );
// Add the request_id to the form as a hidden field. This triggers our post data addition that will update the post
add_filter( 'gform_form_tag', array(__CLASS__, 'gform_form_tag'), 50, 2 );
if ( $form_id ) {
// Add the existing information to the form
add_filter( 'gform_pre_render_' . $form_id, array(__CLASS__, 'gform_pre_render') );
} else {
// Add the existing information to the form
add_filter( 'gform_pre_render', array(__CLASS__, 'gform_pre_render') );
}
// Updates the post data with post id, so post gets updated instead of creating a new one
add_action( 'gform_post_data', array(__CLASS__, 'gform_post_data'), 10, 2 );
// Update file field
add_filter( 'gform_field_content', array(__CLASS__, 'gform_field_content'), 10, 5 );
}
}
/**
* AJAX Delete Upload Wrapper
*
* @author ekaj
* @return void
*/
public static function ajax_delete_upload()
{
// vars
$options = array(
'nonce' => '',
'post_id' => 0,
'form_id' => 0,
'file' => false,
'featured' => 0,
'meta' => ''
);
// load post options
$options = array_merge($options, $_POST);
// test options
if (! $options['post_id'] || ! $options['form_id'] || (! $options['featured'] && ! $options['meta']) ) { die('Missing information'); }
// verify nonce
if (! wp_verify_nonce($options['nonce'], self::$settings['nonce_delete']) ) { die('Are you sure?'); }
if ( $options['featured'] ) {
// Delete the attachment, if it works, remove the featured meta from post
if ( wp_delete_attachment( $options['featured'] ) ) {
delete_post_meta( $options['post_id'], '_thumbnail_id' );
}
} elseif ( $options['meta'] ) {
self::delete_upload( $options );
} else {
die(0);
}
die('1');
}
/**
* Delete Upload
*
* @author ekaj
* @return void
*/
public static function delete_upload( $options )
{
$file = ( $options['file'] ) ? $options['file'] : get_post_meta( $options['post_id'], $options['meta'], true );
$filetype = wp_check_filetype( $file );
// get the thumbnail name
$path_to_file = GFFormsModel::get_upload_path($options['form_id']);
$url_to_file = GFFormsModel::get_upload_url($options['form_id']);
$file_path = str_replace($url_to_file, $path_to_file, $file);
// Delete the file
@unlink($file_path);
// Attempt to delete the thumbnail if an image
if ( 'image/' == substr($filetype['type'], 0, 6) )
{
$width = apply_filters( self::PREFIX . '/image/width', apply_filters(self::PREFIX . '/file/width', self::$settings['file_width']) );
$height = apply_filters( self::PREFIX . '/image/height', apply_filters(self::PREFIX . '/file/height', self::$settings['file_height']) );
// get the thumbnail name
$old_ext = '.' . $filetype['ext'];
$new_ext = '-thumb.' . $filetype['ext'];
$resized = str_replace($old_ext, $new_ext, $file_path);
@unlink($resized);
}
// Remove the meta from the post
if ( $options['file'] ) {
return delete_post_meta( $options['post_id'], $options['meta'], $options['file'] );
} else {
return delete_post_meta( $options['post_id'], $options['meta'] );
}
}
/**
* Update File Fields
*
* Add the existing file to file field in form.
*
* @author ekaj
* @type filter
*/
public static function gform_field_content( $content, $field, $value, $lead_id, $form_id )
{
if (! empty($field['type']) && 'post_image' == $field['type'] && ! empty($field['postFeaturedImage']) )
{
$thumb_id = get_post_thumbnail_id(self::$post->ID);
if ( is_numeric($thumb_id) )
{
$thumb_url = wp_get_attachment_image_src($thumb_id, 'thumbnail', true);
$full_url = wp_get_attachment_image_src($thumb_id, 'full', true);
$file = $full_url[0];
$filename = basename($file);
$image = '<span style="display:inline-block; width:' . esc_attr($thumb_url[1]) . 'px; height:' . esc_attr($thumb_url[2]) . 'px; overflow:hidden;"><img src="' . esc_url($thumb_url[0]) . '" /></span>';
ob_start();
?>
<div class="<?php echo esc_attr(self::PREFIX); ?>_upload_container">
<p class="<?php echo esc_attr(self::PREFIX); ?>_upload_link" style="margin:1em 0 0 0;">
<a target="_blank" href="<?php echo esc_url($file); ?>" style="border:none; margin:0 1em 0 0;">
<?php echo $image; ?>
</a>
<a target="_blank" href="<?php echo esc_url($file); ?>">
<strong><?php echo esc_html( apply_filters(self::PREFIX . '/file/name', $filename) ); ?></strong>
</a>
</p>
<?php if ( apply_filters( self::PREFIX . '/public_file_delete', true ) && apply_filters( self::PREFIX . '/public_file_delete/featured', true ) ) : ?>
<a class="<?php echo esc_attr(self::PREFIX); ?>_delete_link" data-post_id="<?php echo esc_attr(self::$post->ID); ?>" data-form_id="<?php echo esc_attr($form_id); ?>" data-meta="0" data-featured="<?php echo $thumb_id; ?>" href="#<?php _e("delete_requires_javascript", self::PREFIX); ?>" title="<?php _e("Delete Upload", self::PREFIX); ?>">
<?php _e("Delete", self::PREFIX); ?>
</a>
<?php endif; ?>
</div>
<?php
$content .= ob_get_clean();
}
}
elseif (! empty($field['inputType']) && 'fileupload' == $field['inputType'] && ! empty($field['defaultValue']) )
{
if (! empty($field['multipleFiles']) )
{
$content .= '<a class="' . esc_attr(self::PREFIX) . '_addmore_link" href="#' . __("requires_javascript", self::PREFIX) . '" title="' . __("Add more uploads", self::PREFIX) . '">' . __("Add more", self::PREFIX) . '</a>';
$file_array = explode(', ', $field['defaultValue']);
if ( $file_array ) {
foreach ( $file_array as $file ) {
$content .= self::create_uploaded_file( $file, $field, $form_id );
}
}
}
else
{
$content .= self::create_uploaded_file( $field['defaultValue'], $field, $form_id );
}
}
return $content;
}
/**
* Create an upload.
*
* @author ekaj
* @return void
*/
public static function create_uploaded_file( $file, $field, $form_id )
{
$basename = basename($file);
$filetype = wp_check_filetype( $basename );
$mime = $filetype['type'];
$width = apply_filters(self::PREFIX . '/file/width', self::$settings['file_width']);
$height = apply_filters(self::PREFIX . '/file/height', self::$settings['file_height']);
// If this is an image, set up and create a thumbnail
if ( 'image/' == substr($mime, 0, 6) )
{
$image_url = '';
if ( apply_filters(self::PREFIX . '/image/resize', true) )
{
// Get settings for image thumb
$width = apply_filters(self::PREFIX . '/image/width', $width);
$height = apply_filters(self::PREFIX . '/image/height', $height);
$crop = apply_filters(self::PREFIX . '/image/crop', true);
// Create the file local path
$basedir = GFFormsModel::get_upload_path($form_id);
$baseurl = GFFormsModel::get_upload_url($form_id);
$filename = str_replace($baseurl, $basedir, $file);
if ( is_file($filename) )
{
// Make sure the server supports resize and save
$img_editor_test = wp_image_editor_supports( array(
'methods' => array(
'resize',
'save'
)
) );
if ( true === $img_editor_test && is_writable($basedir) )
{
// Get the image editor
$image_editor = wp_get_image_editor( $filename );
if (! is_wp_error($image_editor) )
{
// Create thumbnail filename
$thumbname = $image_editor->generate_filename( 'thumb' );
// Test if thumbnail exists
$thumb_exists = file_exists($thumbname);
if ( $thumb_exists ) {
$thumbsize = getimagesize( $thumbname );
}
// If no thumbnail, or the size has changed, generate a new one
if (! $thumb_exists || $thumbsize[0] != $width || $thumbsize[1] != $height )
{
$image_editor->resize( $width, $height, $crop );
$resized = $image_editor->save($thumbname);
if (! is_wp_error($resized) ) {
$pathinfo = pathinfo($file);
$image_url = $pathinfo['dirname'] . '/' . $resized['file'];
}
}
// Otherwise use the existing file
else
{
$image_url = str_replace($basedir, $baseurl, $thumbname);
}
}
}
}
}
// If there is no thumbnail at this point, use the file itself
if (! $image_url ) {
$image_url = $file;
}
}
// Not a file then get a mimetype icon from WP
else
{
$image_url = wp_mime_type_icon( wp_ext2type($filetype['ext']) );
}
$image = '<span style="display:inline-block; width:' . esc_attr($width) . 'px; height:' . esc_attr($height) . 'px; overflow:hidden;"><img src="' . esc_url($image_url) . '" /></span>';
ob_start();
?>
<div class="<?php echo esc_attr(self::PREFIX); ?>_upload_container">
<p class="<?php echo esc_attr(self::PREFIX); ?>_upload_link" style="margin:1em 0 0 0;">
<a target="_blank" href="<?php echo esc_url($file); ?>" style="border:none; margin:0 1em 0 0;">
<?php echo $image; ?>
</a>
<a target="_blank" href="<?php echo esc_url($file); ?>">
<strong><?php echo esc_html( apply_filters(self::PREFIX . '/file/name', $basename) ); ?></strong>
</a>
</p>
<?php if ( apply_filters( self::PREFIX . '/public_file_delete', true ) ) : ?>
<a class="<?php echo esc_attr(self::PREFIX); ?>_delete_link" data-post_id="<?php echo esc_attr(self::$post->ID); ?>" data-form_id="<?php echo esc_attr($form_id); ?>" data-meta="<?php echo esc_attr($field['postCustomFieldName']); ?>" data-featured="0" href="#<?php _e("delete_requires_javascript", self::PREFIX); ?>" title="<?php _e("Delete Upload", self::PREFIX); ?>">
<?php _e("Delete", self::PREFIX); ?>
</a>
<?php endif; ?>
</div>
<?php
return ob_get_clean();
}
/**
* Add Request Field to Form
*
* This field will trigger the post data updates when their isn't a GET variable.
*
* @author p51labs
* @author ekaj
* @type filter
*/
public static function gform_form_tag( $form_tag, $form )
{
$form_tag .= '<input type="hidden" name="' . apply_filters(self::PREFIX . '/request_id', self::$settings['request_id']) . '" value="' . self::$post->ID . '" class="gform_hidden" />';
return $form_tag;
}
/**
* Populate Form Fields
*
* Add any exisiting info to the form fields from our edit post.
*
* @author p51labs
* @type filter
*/
public static function gform_pre_render( $form )
{
if ( self::current_user_can() )
{
$meta = get_post_custom( self::$post->ID );
foreach ( $form['fields'] as &$field )
{
$field_type = $field['type'];
if ( isset(self::$post->$field_type) && is_string(self::$post->$field_type) )
{
$field = self::populate_element($field, $field_type, self::$post->$field_type);
}
elseif ( 'post_custom_field' == $field_type && isset($meta[$field['postCustomFieldName']]) )
{
$multi_fields = array('multiselect', 'checkbox', 'list');
if ( in_array($field['inputType'], $multi_fields) || ! empty($field['multipleFiles']) ) {
$value = apply_filters( self::PREFIX . '_multi_fields', $meta[ $field['postCustomFieldName'] ] );
} else {
$value = end($meta[ $field['postCustomFieldName'] ]);
}
$field = self::populate_element($field, $field['inputType'], $value);
}
elseif ( isset(self::$post->taxonomies[$field_type]) )
{
$value = array();
foreach ( self::$post->taxonomies[$field_type] as $object )
{
if ( 'post_tags' == $field_type ) {
$value[] = $object->name;
} else {
$value[] = $object->term_id;
}
}
$field = self::populate_element($field, $field_type, $value);
}
elseif (! empty($field['populateTaxonomy']) && isset(self::$post->taxonomies[$field['populateTaxonomy']]) )
{
$value = array();
if ( self::$post->taxonomies[$field['populateTaxonomy']] )
{
foreach ( self::$post->taxonomies[$field['populateTaxonomy']] as $object ) {
$value[] = $object->term_id;
}
}
$field = self::populate_element($field, 'populateTaxonomy', $value);
}
if (! empty($field['defaultValue']) && ! empty($field['conditionalLogic']) && is_array($field['conditionalLogic']) )
{
foreach ( $field['conditionalLogic']['rules'] as $rule )
{
if ( empty($form['conditional']) ) {
$form['conditional'] = array();
}
$form['conditional'][] = $rule['fieldId'];
}
}
}
}
return $form;
}
/**
* Populate Field Elements
*
* Populate specific form fields based on type.
*
* @author p51labs
* @author ekaj
* @param array $field
* @param string $field_type
* @param mixed $value
* @return array $field Modified $field array
*/
public static function populate_element( $field, $field_type, $value )
{
$value = maybe_unserialize($value);
switch ( $field_type )
{
case 'post_category':
$field['allowsPrepopulate'] = true;
$field['inputName'] = $field_type;
self::$settings['cat_value'] = $value;
add_filter( 'gform_field_value_' . $field['inputName'], array(__CLASS__, 'return_category_field_value'), 10, 2 );
#add_filter( 'gform_field_value_' . $field['inputName'], function($value) use($value) { return $value; } );
break;
case 'populateTaxonomy':
$field['allowsPrepopulate'] = true;
$field['inputName'] = $field['populateTaxonomy'];
self::$settings['tax_value'][$field['inputName']] = $value;
add_filter( 'gform_field_value_' . $field['inputName'], array(__CLASS__, 'return_taxonomy_field_value') , 10, 2 );
$value = (! is_array($value) ) ? array($value) : $value;
if ( version_compare(GFCommon::$version, '1.9') >= 0 )
{
if ( isset($field->choices) )
{
foreach ( $field->choices as &$choice ) {
$choice['isSelected'] = ( in_array($choice['value'], $value) ) ? true : '';
}
}
}
else
{
if ( isset($field['choices']) )
{
foreach ( $field['choices'] as &$choice ) {
$choice['isSelected'] = ( in_array($choice['value'], $value) ) ? true : '';
}