Completed
Pull Request — master (#716)
by Zack
09:51 queued 04:52
created

GravityView_GFFormsModel   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 132
rs 10
wmc 12
lcom 0
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B get_post_field_images() 0 22 6
A get_post_fields() 0 17 2
A copy_post_image() 0 17 2
A media_handle_upload() 0 17 2
1
<?php
2
/**
3
 * Make some GFFormsModel public available.
4
 * @since 1.16.2
5
 */
6
7
8
class GravityView_GFFormsModel extends GFFormsModel {
9
10
    /**
11
     * Given information provided in an entry, get array of media IDs
12
     *
13
     * This is necessary because GF doesn't expect to need to update post images, only to create them.
14
     *
15
     * @see GFFormsModel::create_post()
16
     *
17
     * @since 1.17
18
     *
19
     * @param array $form Gravity Forms form array
20
     * @param array $entry Gravity Forms entry array
21
     *
22
     * @return array Array of "Field ID" => "Media IDs"
23
     */
24
    public static function get_post_field_images( $form, $entry ) {
25
26
        $post_data = self::get_post_fields( $form, $entry );
27
28
        $media = get_attached_media( 'image', $entry['post_id'] );
29
30
        $post_images = array();
31
32
        foreach ( $media as $media_item ) {
33
            foreach( (array) $post_data['images'] as $post_data_item ) {
34
                if(
35
                    rgar( $post_data_item, 'title' ) === $media_item->post_title &&
36
                    rgar( $post_data_item, 'description' ) === $media_item->post_content &&
37
                    rgar( $post_data_item, 'caption' ) === $media_item->post_excerpt
38
                ) {
39
                    $post_images["{$post_data_item['field_id']}"] = $media_item->ID;
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
40
                }
41
            }
42
        }
43
44
        return $post_images;
45
    }
46
47
    /**
48
     * Alias of GFFormsModel::get_post_fields(); just making it public
49
     *
50
     * @see GFFormsModel::get_post_fields()
51
     *
52
     * @since 1.17
53
     *
54
     * @param array $form Gravity Forms form array
55
     * @param array $entry Gravity Forms entry array
56
     *
57
     * @return array
58
     */
59
    public static function get_post_fields( $form, $entry ) {
60
61
        $reflection = new ReflectionMethod( 'GFFormsModel', 'get_post_fields' );
62
63
        /**
64
         * If the method changes to public, use Gravity Forms' method
65
         * @todo: If/when the method is public, remove the unneeded copied code.
66
         */
67
        if( $reflection->isPublic() ) {
68
            return parent::get_post_fields( $form, $entry );
69
        }
70
71
        // It was private; let's make it public
72
        $reflection->setAccessible( true );
73
74
        return $reflection->invoke( new GFFormsModel, $form, $entry );
75
    }
76
77
    /**
78
     * Copied function from Gravity Forms plugin \GFFormsModel::copy_post_image since the method is private.
79
     *
80
     * @since 1.16.2
81
     *
82
     * @param string $url URL of the post image to update
83
     * @param int $post_id ID of the post image to update
84
     * @return array|bool Array with `file`, `url` and `type` keys. False: failed to copy file to final directory path.
85
     */
86
    public static function copy_post_image( $url, $post_id ) {
87
88
        $reflection = new ReflectionMethod( 'GFFormsModel', 'copy_post_image' );
89
90
        /**
91
         * If the method changes to public, use Gravity Forms' method
92
         * @todo: If/when the method is public, remove the unneeded copied code.
93
         */
94
        if( $reflection->isPublic() ) {
95
            return parent::copy_post_image( $url, $post_id );
96
        }
97
98
        // It was private; let's make it public
99
        $reflection->setAccessible( true );
100
101
        return $reflection->invoke( new GFFormsModel, $url, $post_id );
102
    }
103
104
    /**
105
     * Copied function from Gravity Forms plugin \GFFormsModel::media_handle_upload since the method is private.
106
     *
107
     * Note: The method became public in GF 1.9.17.7
108
     *
109
     * @see GFFormsModel::media_handle_upload
110
     * @see GravityView_Edit_Entry_Render::maybe_update_post_fields
111
     *
112
     * @uses copy_post_image
113
     * @uses wp_insert_attachment
114
     * @uses wp_update_attachment_metadata
115
     *
116
     * @param string $url URL of the post image to update
117
     * @param int $post_id ID of the post image to update
118
     * @param array $post_data Array of data for the eventual attachment post type that is created using {@see wp_insert_attachment}. Supports `post_mime_type`, `guid`, `post_parent`, `post_title`, `post_content` keys.
119
     * @return bool|int ID of attachment Post created. Returns false if file not created by copy_post_image
120
     */
121
    public static function media_handle_upload( $url, $post_id, $post_data = array() ) {
122
123
        $reflection = new ReflectionMethod( 'GFFormsModel', 'media_handle_upload' );
124
125
        /**
126
         * If the method changes to public, use Gravity Forms' method
127
         * @todo: If/when the method is public, remove the unneeded copied code.
128
         */
129
        if( $reflection->isPublic() ) {
130
            return parent::media_handle_upload( $url, $post_id, $post_data );
131
        }
132
133
        // It was private; let's make it public
134
        $reflection->setAccessible( true );
135
136
        return $reflection->invoke( new GFFormsModel, $url, $post_id, $post_data );
137
    }
138
139
}