Completed
Push — develop ( 4e47eb...d09b3b )
by Zack
14:47
created

GravityView_GFFormsModel::is_value_match()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 6.9683

Importance

Changes 0
Metric Value
cc 5
nc 10
nop 6
dl 0
loc 13
ccs 4
cts 7
cp 0.5714
crap 6.9683
rs 9.5222
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
	 * 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
		if ( in_array( $operation, array( 'in', 'not_in' ) ) ) {
48
			return GVCommon::matches_operation( (array) $field_value, (array) $target_value, $operation );
49
		}
50
51 1
		return parent::is_value_match( (array) $field_value, $target_value, $operation, $source_field, $rule, $form );
52
	}
53
54
	/**
55
     * Given information provided in an entry, get array of media IDs
56
     *
57
     * This is necessary because GF doesn't expect to need to update post images, only to create them.
58
     *
59
     * @see GFFormsModel::create_post()
60
     *
61
     * @since 1.17
62
     *
63
     * @param array $form Gravity Forms form array
64
     * @param array $entry Gravity Forms entry array
65
     *
66
     * @return array Array of "Field ID" => "Media IDs"
67
     */
68
    public static function get_post_field_images( $form, $entry ) {
69
70
        $post_data = self::get_post_fields( $form, $entry );
71
72
        $media = get_attached_media( 'image', $entry['post_id'] );
73
74
        $post_images = array();
75
76
        foreach ( $media as $media_item ) {
77
            foreach( (array) $post_data['images'] as $post_data_item ) {
78
                if(
79
                    \GV\Utils::get( $post_data_item, 'title' ) === $media_item->post_title &&
80
                    \GV\Utils::get( $post_data_item, 'description' ) === $media_item->post_content &&
81
                    \GV\Utils::get( $post_data_item, 'caption' ) === $media_item->post_excerpt
82
                ) {
83
                    $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...
84
                }
85
            }
86
        }
87
88
        return $post_images;
89
    }
90
91
    /**
92
     * Alias of GFFormsModel::get_post_fields(); just making it public
93
     *
94
     * @see GFFormsModel::get_post_fields()
95
     *
96
     * @since 1.17
97
     *
98
     * @param array $form Gravity Forms form array
99
     * @param array $entry Gravity Forms entry array
100
     *
101
     * @return array
102
     */
103
    public static function get_post_fields( $form, $entry ) {
104
105
        $reflection = new ReflectionMethod( 'GFFormsModel', 'get_post_fields' );
106
107
        /**
108
         * If the method changes to public, use Gravity Forms' method
109
         * @todo: If/when the method is public, remove the unneeded copied code.
110
         */
111
        if( $reflection->isPublic() ) {
112
            return parent::get_post_fields( $form, $entry );
113
        }
114
115
        // It was private; let's make it public
116
        $reflection->setAccessible( true );
117
118
        return $reflection->invoke( new GFFormsModel, $form, $entry );
119
    }
120
121
    /**
122
     * Copied function from Gravity Forms plugin \GFFormsModel::copy_post_image since the method is private.
123
     *
124
     * @since 1.16.2
125
     *
126
     * @param string $url URL of the post image to update
127
     * @param int $post_id ID of the post image to update
128
     * @return array|bool Array with `file`, `url` and `type` keys. False: failed to copy file to final directory path.
129
     */
130
    public static function copy_post_image( $url, $post_id ) {
131
132
        $reflection = new ReflectionMethod( 'GFFormsModel', 'copy_post_image' );
133
134
        /**
135
         * If the method changes to public, use Gravity Forms' method
136
         * @todo: If/when the method is public, remove the unneeded copied code.
137
         */
138
        if( $reflection->isPublic() ) {
139
            return parent::copy_post_image( $url, $post_id );
140
        }
141
142
        // It was private; let's make it public
143
        $reflection->setAccessible( true );
144
145
        return $reflection->invoke( new GFFormsModel, $url, $post_id );
146
    }
147
148
    /**
149
     * Copied function from Gravity Forms plugin \GFFormsModel::media_handle_upload since the method is private.
150
     *
151
     * Note: The method became public in GF 1.9.17.7
152
     *
153
     * @see GFFormsModel::media_handle_upload
154
     * @see GravityView_Edit_Entry_Render::maybe_update_post_fields
155
     *
156
     * @uses copy_post_image
157
     * @uses wp_insert_attachment
158
     * @uses wp_update_attachment_metadata
159
     *
160
     * @param string $url URL of the post image to update
161
     * @param int $post_id ID of the post image to update
162
     * @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.
163
     * @return bool|int ID of attachment Post created. Returns false if file not created by copy_post_image
164
     */
165
    public static function media_handle_upload( $url, $post_id, $post_data = array() ) {
166
167
        $reflection = new ReflectionMethod( 'GFFormsModel', 'media_handle_upload' );
168
169
        /**
170
         * If the method changes to public, use Gravity Forms' method
171
         * @todo: If/when the method is public, remove the unneeded copied code.
172
         */
173
        if( $reflection->isPublic() ) {
174
            return parent::media_handle_upload( $url, $post_id, $post_data );
175
        }
176
177
        // It was private; let's make it public
178
        $reflection->setAccessible( true );
179
180
        return $reflection->invoke( new GFFormsModel, $url, $post_id, $post_data );
181
    }
182
183
}