Completed
Push — develop ( 152e25...139b37 )
by Zack
21:27 queued 11:24
created

GravityView_GFFormsModel::get_database_version()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 4
cp 0
crap 6
rs 9.4285
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
	public static function get_database_version() {
18
19
		if ( is_callable( 'parent::get_database_version' ) ) {
20
			return parent::get_database_version();
21
		}
22
23
		return get_option( 'gf_db_version' );
24
	}
25
26
	/**
27
	 * Determines if the field value matches the conditional logic rule value.
28
	 *
29
	 * @since 1.22.3
30
	 *
31
	 * @param mixed         $field_value  The field value to be checked.
32
	 * @param mixed         $target_value The conditional logic rule value.
33
	 * @param string        $operation    The conditional logic rule operator.
34
	 * @param null|GF_Field|string $source_field The field the rule is based on, or the entry meta
35
	 * @param null|array    $rule         The conditional logic rule properties.
36
	 * @param null|array    $form         The current form.
37
	 *
38
	 * @return bool
39
	 */
40 1
	public static function is_value_match( $field_value, $target_value, $operation = 'is', $source_field = null, $rule = null, $form = null ) {
41
42 1
		if ( 'date_created' === $source_field ) {
43
			$field_value = is_int( $field_value )? $field_value : strtotime( $field_value );
44
			$target_value = is_int( $target_value )? $target_value : strtotime( $target_value );
45
		}
46
47 1
		return parent::is_value_match( $field_value, $target_value, $operation, $source_field, $rule, $form ); // TODO: Change the autogenerated stub
48
	}
49
50
	/**
51
     * Given information provided in an entry, get array of media IDs
52
     *
53
     * This is necessary because GF doesn't expect to need to update post images, only to create them.
54
     *
55
     * @see GFFormsModel::create_post()
56
     *
57
     * @since 1.17
58
     *
59
     * @param array $form Gravity Forms form array
60
     * @param array $entry Gravity Forms entry array
61
     *
62
     * @return array Array of "Field ID" => "Media IDs"
63
     */
64
    public static function get_post_field_images( $form, $entry ) {
65
66
        $post_data = self::get_post_fields( $form, $entry );
67
68
        $media = get_attached_media( 'image', $entry['post_id'] );
69
70
        $post_images = array();
71
72
        foreach ( $media as $media_item ) {
73
            foreach( (array) $post_data['images'] as $post_data_item ) {
74
                if(
75
                    \GV\Utils::get( $post_data_item, 'title' ) === $media_item->post_title &&
76
                    \GV\Utils::get( $post_data_item, 'description' ) === $media_item->post_content &&
77
                    \GV\Utils::get( $post_data_item, 'caption' ) === $media_item->post_excerpt
78
                ) {
79
                    $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...
80
                }
81
            }
82
        }
83
84
        return $post_images;
85
    }
86
87
    /**
88
     * Alias of GFFormsModel::get_post_fields(); just making it public
89
     *
90
     * @see GFFormsModel::get_post_fields()
91
     *
92
     * @since 1.17
93
     *
94
     * @param array $form Gravity Forms form array
95
     * @param array $entry Gravity Forms entry array
96
     *
97
     * @return array
98
     */
99
    public static function get_post_fields( $form, $entry ) {
100
101
        $reflection = new ReflectionMethod( 'GFFormsModel', 'get_post_fields' );
102
103
        /**
104
         * If the method changes to public, use Gravity Forms' method
105
         * @todo: If/when the method is public, remove the unneeded copied code.
106
         */
107
        if( $reflection->isPublic() ) {
108
            return parent::get_post_fields( $form, $entry );
109
        }
110
111
        // It was private; let's make it public
112
        $reflection->setAccessible( true );
113
114
        return $reflection->invoke( new GFFormsModel, $form, $entry );
115
    }
116
117
    /**
118
     * Copied function from Gravity Forms plugin \GFFormsModel::copy_post_image since the method is private.
119
     *
120
     * @since 1.16.2
121
     *
122
     * @param string $url URL of the post image to update
123
     * @param int $post_id ID of the post image to update
124
     * @return array|bool Array with `file`, `url` and `type` keys. False: failed to copy file to final directory path.
125
     */
126
    public static function copy_post_image( $url, $post_id ) {
127
128
        $reflection = new ReflectionMethod( 'GFFormsModel', 'copy_post_image' );
129
130
        /**
131
         * If the method changes to public, use Gravity Forms' method
132
         * @todo: If/when the method is public, remove the unneeded copied code.
133
         */
134
        if( $reflection->isPublic() ) {
135
            return parent::copy_post_image( $url, $post_id );
136
        }
137
138
        // It was private; let's make it public
139
        $reflection->setAccessible( true );
140
141
        return $reflection->invoke( new GFFormsModel, $url, $post_id );
142
    }
143
144
    /**
145
     * Copied function from Gravity Forms plugin \GFFormsModel::media_handle_upload since the method is private.
146
     *
147
     * Note: The method became public in GF 1.9.17.7
148
     *
149
     * @see GFFormsModel::media_handle_upload
150
     * @see GravityView_Edit_Entry_Render::maybe_update_post_fields
151
     *
152
     * @uses copy_post_image
153
     * @uses wp_insert_attachment
154
     * @uses wp_update_attachment_metadata
155
     *
156
     * @param string $url URL of the post image to update
157
     * @param int $post_id ID of the post image to update
158
     * @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.
159
     * @return bool|int ID of attachment Post created. Returns false if file not created by copy_post_image
160
     */
161
    public static function media_handle_upload( $url, $post_id, $post_data = array() ) {
162
163
        $reflection = new ReflectionMethod( 'GFFormsModel', 'media_handle_upload' );
164
165
        /**
166
         * If the method changes to public, use Gravity Forms' method
167
         * @todo: If/when the method is public, remove the unneeded copied code.
168
         */
169
        if( $reflection->isPublic() ) {
170
            return parent::media_handle_upload( $url, $post_id, $post_data );
171
        }
172
173
        // It was private; let's make it public
174
        $reflection->setAccessible( true );
175
176
        return $reflection->invoke( new GFFormsModel, $url, $post_id, $post_data );
177
    }
178
179
}