Completed
Push — develop ( 5b5c67...50c7f0 )
by Gennady
06:16
created

GravityView_GFFormsModel   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 6.98%

Importance

Changes 0
Metric Value
dl 0
loc 181
ccs 3
cts 43
cp 0.0698
rs 10
c 0
b 0
f 0
wmc 23
lcom 0
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get_database_version() 0 8 2
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
B is_value_match() 0 18 9
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 21
	public static function get_database_version() {
18
19 21
		if ( is_callable( 'parent::get_database_version' ) ) {
20 21
			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
	public static function is_value_match( $field_value, $target_value, $operation = 'is', $source_field = null, $rule = null, $form = null ) {
41
42
		if ( in_array( $source_field, array( 'date_created', 'date_updated', 'payment_date' ), true ) ) {
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
		if ( $source_field instanceof GF_Field && $source_field->type == 'date' ) {
0 ignored issues
show
Bug introduced by
The class GF_Field does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

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