Completed
Push — master ( 1fd6be...155036 )
by Zack
13s
created

GravityView_GFFormsModel::get_post_field_images()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 4
nop 2
dl 0
loc 22
ccs 0
cts 11
cp 0
crap 42
rs 8.6737
c 0
b 0
f 0
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
	 * Make sure the method exists, regardless of GF version
12
	 *
13
	 * @since 1.22.2
14
	 *
15
	 * @return string|false False if not set, version string otherwise
16
	 */
17 3
	public static function get_database_version() {
18
19 3
		if ( is_callable( 'parent::get_database_version' ) ) {
20 3
			return parent::get_database_version();
21
		}
22
23
		return get_option( 'gf_db_version' );
24
	}
25
26
	/**
27
     * Given information provided in an entry, get array of media IDs
28
     *
29
     * This is necessary because GF doesn't expect to need to update post images, only to create them.
30
     *
31
     * @see GFFormsModel::create_post()
32
     *
33
     * @since 1.17
34
     *
35
     * @param array $form Gravity Forms form array
36
     * @param array $entry Gravity Forms entry array
37
     *
38
     * @return array Array of "Field ID" => "Media IDs"
39
     */
40
    public static function get_post_field_images( $form, $entry ) {
41
42
        $post_data = self::get_post_fields( $form, $entry );
43
44
        $media = get_attached_media( 'image', $entry['post_id'] );
45
46
        $post_images = array();
47
48
        foreach ( $media as $media_item ) {
49
            foreach( (array) $post_data['images'] as $post_data_item ) {
50
                if(
51
                    rgar( $post_data_item, 'title' ) === $media_item->post_title &&
52
                    rgar( $post_data_item, 'description' ) === $media_item->post_content &&
53
                    rgar( $post_data_item, 'caption' ) === $media_item->post_excerpt
54
                ) {
55
                    $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...
56
                }
57
            }
58
        }
59
60
        return $post_images;
61
    }
62
63
    /**
64
     * Alias of GFFormsModel::get_post_fields(); just making it public
65
     *
66
     * @see GFFormsModel::get_post_fields()
67
     *
68
     * @since 1.17
69
     *
70
     * @param array $form Gravity Forms form array
71
     * @param array $entry Gravity Forms entry array
72
     *
73
     * @return array
74
     */
75
    public static function get_post_fields( $form, $entry ) {
76
77
        $reflection = new ReflectionMethod( 'GFFormsModel', 'get_post_fields' );
78
79
        /**
80
         * If the method changes to public, use Gravity Forms' method
81
         * @todo: If/when the method is public, remove the unneeded copied code.
82
         */
83
        if( $reflection->isPublic() ) {
84
            return parent::get_post_fields( $form, $entry );
85
        }
86
87
        // It was private; let's make it public
88
        $reflection->setAccessible( true );
89
90
        return $reflection->invoke( new GFFormsModel, $form, $entry );
91
    }
92
93
    /**
94
     * Copied function from Gravity Forms plugin \GFFormsModel::copy_post_image since the method is private.
95
     *
96
     * @since 1.16.2
97
     *
98
     * @param string $url URL of the post image to update
99
     * @param int $post_id ID of the post image to update
100
     * @return array|bool Array with `file`, `url` and `type` keys. False: failed to copy file to final directory path.
101
     */
102
    public static function copy_post_image( $url, $post_id ) {
103
104
        $reflection = new ReflectionMethod( 'GFFormsModel', 'copy_post_image' );
105
106
        /**
107
         * If the method changes to public, use Gravity Forms' method
108
         * @todo: If/when the method is public, remove the unneeded copied code.
109
         */
110
        if( $reflection->isPublic() ) {
111
            return parent::copy_post_image( $url, $post_id );
112
        }
113
114
        // It was private; let's make it public
115
        $reflection->setAccessible( true );
116
117
        return $reflection->invoke( new GFFormsModel, $url, $post_id );
118
    }
119
120
    /**
121
     * Copied function from Gravity Forms plugin \GFFormsModel::media_handle_upload since the method is private.
122
     *
123
     * Note: The method became public in GF 1.9.17.7
124
     *
125
     * @see GFFormsModel::media_handle_upload
126
     * @see GravityView_Edit_Entry_Render::maybe_update_post_fields
127
     *
128
     * @uses copy_post_image
129
     * @uses wp_insert_attachment
130
     * @uses wp_update_attachment_metadata
131
     *
132
     * @param string $url URL of the post image to update
133
     * @param int $post_id ID of the post image to update
134
     * @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.
135
     * @return bool|int ID of attachment Post created. Returns false if file not created by copy_post_image
136
     */
137
    public static function media_handle_upload( $url, $post_id, $post_data = array() ) {
138
139
        $reflection = new ReflectionMethod( 'GFFormsModel', 'media_handle_upload' );
140
141
        /**
142
         * If the method changes to public, use Gravity Forms' method
143
         * @todo: If/when the method is public, remove the unneeded copied code.
144
         */
145
        if( $reflection->isPublic() ) {
146
            return parent::media_handle_upload( $url, $post_id, $post_data );
147
        }
148
149
        // It was private; let's make it public
150
        $reflection->setAccessible( true );
151
152
        return $reflection->invoke( new GFFormsModel, $url, $post_id, $post_data );
153
    }
154
155
}