| Total Complexity | 308 |
| Total Lines | 2677 |
| Duplicated Lines | 0 % |
| Changes | 14 | ||
| Bugs | 7 | Features | 1 |
Complex classes like Object_Sync_Sf_WordPress often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Object_Sync_Sf_WordPress, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Object_Sync_Sf_WordPress { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Current version of the plugin |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | public $version; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The main plugin file |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | public $file; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Global object of `$wpdb`, the WordPress database |
||
| 32 | * |
||
| 33 | * @var object |
||
| 34 | */ |
||
| 35 | public $wpdb; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The plugin's slug so we can include it when necessary |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | public $slug; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The plugin's prefix when saving options to the database |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | public $option_prefix; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Object_Sync_Sf_Logging class |
||
| 53 | * |
||
| 54 | * @var object |
||
| 55 | */ |
||
| 56 | public $logging; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Object_Sync_Sf_Mapping class |
||
| 60 | * |
||
| 61 | * @var object |
||
| 62 | */ |
||
| 63 | public $mappings; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Supported WordPress objects |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | public $wordpress_objects; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Method call options |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | public $options; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Object_Sync_Sf_WordPress_Transient class |
||
| 81 | * |
||
| 82 | * @var object |
||
| 83 | */ |
||
| 84 | public $sfwp_transients; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Whether the plugin is in debug mode |
||
| 88 | * |
||
| 89 | * @var bool |
||
| 90 | */ |
||
| 91 | public $debug; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Constructor for WordPress class |
||
| 95 | */ |
||
| 96 | public function __construct() { |
||
| 97 | $this->version = object_sync_for_salesforce()->version; |
||
| 98 | $this->file = object_sync_for_salesforce()->file; |
||
| 99 | $this->wpdb = object_sync_for_salesforce()->wpdb; |
||
| 100 | $this->slug = object_sync_for_salesforce()->slug; |
||
| 101 | $this->option_prefix = object_sync_for_salesforce()->option_prefix; |
||
| 102 | |||
| 103 | $this->logging = object_sync_for_salesforce()->logging; |
||
| 104 | $this->mappings = object_sync_for_salesforce()->mappings; |
||
| 105 | |||
| 106 | add_action( |
||
|
|
|||
| 107 | 'admin_init', |
||
| 108 | function() { |
||
| 109 | $this->wordpress_objects = $this->get_object_types(); |
||
| 110 | } |
||
| 111 | ); |
||
| 112 | |||
| 113 | $this->options = array( |
||
| 114 | 'cache' => true, |
||
| 115 | 'cache_expiration' => $this->cache_expiration( 'wordpress_data_cache', 86400 ), |
||
| 116 | 'type' => 'read', |
||
| 117 | ); |
||
| 118 | |||
| 119 | $this->sfwp_transients = new Object_Sync_Sf_WordPress_Transient( 'sfwp_transients' ); |
||
| 120 | |||
| 121 | // use the option value for whether we're in debug mode. |
||
| 122 | $this->debug = filter_var( get_option( $this->option_prefix . 'debug_mode', false ), FILTER_VALIDATE_BOOLEAN ); |
||
| 123 | |||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get WordPress object types |
||
| 128 | * |
||
| 129 | * @return array $wordpress_objects |
||
| 130 | */ |
||
| 131 | public function get_object_types() { |
||
| 132 | /* |
||
| 133 | * Allow developers to specify, especially non-post, content types that should be included or ignored. |
||
| 134 | * Here's an example of filters to add/remove types: |
||
| 135 | * |
||
| 136 | add_filter( 'object_sync_for_salesforce_add_more_wordpress_types', 'add_more_types', 10, 1 ); |
||
| 137 | function add_more_types( $wordpress_object_types ) { |
||
| 138 | $wordpress_object_types[] = 'foo'; // this will add to the existing types. |
||
| 139 | return $wordpress_object_types; |
||
| 140 | } |
||
| 141 | |||
| 142 | add_filter( 'object_sync_for_salesforce_remove_wordpress_types', 'wordpress_object_types', 10, 1 ); |
||
| 143 | function remove_types( $types_to_remove ) { |
||
| 144 | $types_to_remove[] = 'revision'; // this adds to the array of types to ignore |
||
| 145 | return $types_to_remove; |
||
| 146 | } |
||
| 147 | */ |
||
| 148 | |||
| 149 | // this should include the available object types and send them to the hook. |
||
| 150 | $wordpress_types_not_posts_include = array( 'user', 'comment', 'category', 'tag' ); |
||
| 151 | $wordpress_objects = array_merge( get_post_types(), $wordpress_types_not_posts_include ); |
||
| 152 | // this should be all the objects. |
||
| 153 | $wordpress_objects = apply_filters( $this->option_prefix . 'add_more_wordpress_types', $wordpress_objects ); |
||
| 154 | |||
| 155 | // by default, only remove the revision, log, and scheduled-action types that we use in this plugin. |
||
| 156 | $types_to_remove = apply_filters( $this->option_prefix . 'remove_wordpress_types', array( 'wp_log', 'scheduled-action', 'revision' ) ); |
||
| 157 | |||
| 158 | // if the hook filters out any types, remove them from the visible list. |
||
| 159 | if ( ! empty( $types_to_remove ) ) { |
||
| 160 | $wordpress_objects = array_diff( $wordpress_objects, $types_to_remove ); |
||
| 161 | } |
||
| 162 | |||
| 163 | sort( $wordpress_objects ); |
||
| 164 | return $wordpress_objects; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get WordPress table structure for an object |
||
| 169 | * |
||
| 170 | * @param string $object_type The type of object. |
||
| 171 | * @return array $object_table_structure The table structure. |
||
| 172 | */ |
||
| 173 | public function get_wordpress_table_structure( $object_type ) { |
||
| 174 | if ( 'attachment' === $object_type ) { |
||
| 175 | $object_table_structure = array( |
||
| 176 | 'object_name' => 'post', |
||
| 177 | 'content_methods' => array( |
||
| 178 | 'create' => 'wp_insert_attachment', |
||
| 179 | 'read' => 'get_posts', |
||
| 180 | 'update' => 'wp_insert_attachment', |
||
| 181 | 'delete' => 'wp_delete_attachment', |
||
| 182 | 'match' => 'get_posts', |
||
| 183 | ), |
||
| 184 | 'meta_methods' => array( |
||
| 185 | 'create' => 'wp_generate_attachment_metadata', |
||
| 186 | 'read' => 'wp_get_attachment_metadata', |
||
| 187 | 'update' => 'wp_update_attachment_metadata', |
||
| 188 | 'delete' => '', |
||
| 189 | 'match' => 'WP_Query', |
||
| 190 | ), |
||
| 191 | 'content_table' => $this->wpdb->prefix . 'posts', |
||
| 192 | 'id_field' => 'ID', |
||
| 193 | 'meta_table' => $this->wpdb->prefix . 'postmeta', |
||
| 194 | 'meta_join_field' => 'post_id', |
||
| 195 | 'where' => 'AND ' . $this->wpdb->prefix . 'posts.post_type = "' . $object_type . '"', |
||
| 196 | 'ignore_keys' => array(), |
||
| 197 | ); |
||
| 198 | } elseif ( 'user' === $object_type ) { |
||
| 199 | // User meta fields need to use update_user_meta for create as well, otherwise it'll just get created twice because apparently when the post is created it's already there. |
||
| 200 | |||
| 201 | $user_meta_methods = array( |
||
| 202 | 'create' => 'update_user_meta', |
||
| 203 | 'read' => 'get_user_meta', |
||
| 204 | 'update' => 'update_user_meta', |
||
| 205 | 'delete' => 'delete_user_meta', |
||
| 206 | ); |
||
| 207 | |||
| 208 | $object_table_structure = array( |
||
| 209 | 'object_name' => 'user', |
||
| 210 | 'content_methods' => array( |
||
| 211 | 'create' => 'wp_insert_user', |
||
| 212 | 'read' => 'get_user_by', |
||
| 213 | 'update' => 'wp_update_user', |
||
| 214 | 'delete' => 'wp_delete_user', |
||
| 215 | 'match' => 'get_user_by', |
||
| 216 | ), |
||
| 217 | 'meta_methods' => $user_meta_methods, |
||
| 218 | 'content_table' => $this->wpdb->prefix . 'users', |
||
| 219 | 'id_field' => 'ID', |
||
| 220 | 'meta_table' => $this->wpdb->prefix . 'usermeta', |
||
| 221 | 'meta_join_field' => 'user_id', |
||
| 222 | 'where' => '', |
||
| 223 | 'ignore_keys' => array( // Keep it simple and avoid security risks. |
||
| 224 | 'user_pass', |
||
| 225 | 'user_activation_key', |
||
| 226 | 'session_tokens', |
||
| 227 | ), |
||
| 228 | ); |
||
| 229 | // Check for Multisite installation. Sitewide User table uses base site prefix across all sites. |
||
| 230 | if ( is_multisite() ) { |
||
| 231 | $object_table_structure['content_table'] = $this->wpdb->base_prefix . 'users'; |
||
| 232 | $object_table_structure['meta_table'] = $this->wpdb->base_prefix . 'usermeta'; |
||
| 233 | } |
||
| 234 | } elseif ( 'post' === $object_type ) { |
||
| 235 | $object_table_structure = array( |
||
| 236 | 'object_name' => 'post', |
||
| 237 | 'content_methods' => array( |
||
| 238 | 'create' => 'wp_insert_post', |
||
| 239 | 'read' => 'get_posts', |
||
| 240 | 'update' => 'wp_update_post', |
||
| 241 | 'delete' => 'wp_delete_post', |
||
| 242 | 'match' => 'get_posts', |
||
| 243 | ), |
||
| 244 | 'meta_methods' => array( |
||
| 245 | 'create' => 'add_post_meta', |
||
| 246 | 'read' => 'get_post_meta', |
||
| 247 | 'update' => 'update_post_meta', |
||
| 248 | 'delete' => 'delete_post_meta', |
||
| 249 | 'match' => 'WP_Query', |
||
| 250 | ), |
||
| 251 | 'content_table' => $this->wpdb->prefix . 'posts', |
||
| 252 | 'id_field' => 'ID', |
||
| 253 | 'meta_table' => $this->wpdb->prefix . 'postmeta', |
||
| 254 | 'meta_join_field' => 'post_id', |
||
| 255 | 'where' => 'AND ' . $this->wpdb->prefix . 'posts.post_type = "' . $object_type . '"', |
||
| 256 | 'ignore_keys' => array(), |
||
| 257 | ); |
||
| 258 | } elseif ( 'category' === $object_type || 'tag' === $object_type || 'post_tag' === $object_type ) { |
||
| 259 | // I am unsure why post_tag wasn't here for so long, but i figure it probably needs to be there. |
||
| 260 | $object_table_structure = array( |
||
| 261 | 'object_name' => 'term', |
||
| 262 | 'content_methods' => array( |
||
| 263 | 'create' => 'wp_insert_term', |
||
| 264 | 'read' => 'get_term_by', |
||
| 265 | 'update' => 'wp_update_term', |
||
| 266 | 'delete' => 'wp_delete_term', |
||
| 267 | 'match' => 'get_term_by', |
||
| 268 | ), |
||
| 269 | 'meta_methods' => array( |
||
| 270 | 'create' => 'add_term_meta', |
||
| 271 | 'read' => 'get_term_meta', |
||
| 272 | 'update' => 'update_term_meta', |
||
| 273 | 'delete' => 'delete_metadata', |
||
| 274 | 'match' => 'WP_Term_Query', |
||
| 275 | ), |
||
| 276 | 'content_table' => $this->wpdb->prefix . 'terms', |
||
| 277 | 'id_field' => 'term_id', |
||
| 278 | 'meta_table' => array( $this->wpdb->prefix . 'termmeta', $this->wpdb->prefix . 'term_taxonomy' ), |
||
| 279 | 'meta_join_field' => 'term_id', |
||
| 280 | 'where' => '', |
||
| 281 | 'ignore_keys' => array(), |
||
| 282 | ); |
||
| 283 | } elseif ( 'comment' === $object_type ) { |
||
| 284 | $object_table_structure = array( |
||
| 285 | 'object_name' => 'comment', |
||
| 286 | 'content_methods' => array( |
||
| 287 | 'create' => 'wp_new_comment', |
||
| 288 | 'read' => 'get_comments', |
||
| 289 | 'update' => 'wp_update_comment', |
||
| 290 | 'delete' => 'wp_delete_comment', |
||
| 291 | 'match' => 'get_comments', |
||
| 292 | ), |
||
| 293 | 'meta_methods' => array( |
||
| 294 | 'create' => 'add_comment_meta', |
||
| 295 | 'read' => 'get_comment_meta', |
||
| 296 | 'update' => 'update_comment_meta', |
||
| 297 | 'delete' => 'delete_comment_metadata', |
||
| 298 | 'match' => 'WP_Comment_Query', |
||
| 299 | ), |
||
| 300 | 'content_table' => $this->wpdb->prefix . 'comments', |
||
| 301 | 'id_field' => 'comment_ID', |
||
| 302 | 'meta_table' => $this->wpdb->prefix . 'commentmeta', |
||
| 303 | 'meta_join_field' => 'comment_id', |
||
| 304 | 'where' => '', |
||
| 305 | 'ignore_keys' => array(), |
||
| 306 | ); |
||
| 307 | } else { // This is for custom post types. |
||
| 308 | $object_table_structure = array( |
||
| 309 | 'object_name' => 'post', |
||
| 310 | 'content_methods' => array( |
||
| 311 | 'create' => 'wp_insert_post', |
||
| 312 | 'read' => 'get_posts', |
||
| 313 | 'update' => 'wp_update_post', |
||
| 314 | 'delete' => 'wp_delete_post', |
||
| 315 | 'match' => 'get_posts', |
||
| 316 | ), |
||
| 317 | 'meta_methods' => array( |
||
| 318 | 'create' => 'add_post_meta', |
||
| 319 | 'read' => 'get_post_meta', |
||
| 320 | 'update' => 'update_post_meta', |
||
| 321 | 'delete' => 'delete_post_meta', |
||
| 322 | 'match' => 'WP_Query', |
||
| 323 | ), |
||
| 324 | 'content_table' => $this->wpdb->prefix . 'posts', |
||
| 325 | 'id_field' => 'ID', |
||
| 326 | 'meta_table' => $this->wpdb->prefix . 'postmeta', |
||
| 327 | 'meta_join_field' => 'post_id', |
||
| 328 | 'where' => 'AND ' . $this->wpdb->prefix . 'posts.post_type = "' . $object_type . '"', |
||
| 329 | 'ignore_keys' => array(), |
||
| 330 | ); |
||
| 331 | } // End if() statement. |
||
| 332 | |||
| 333 | return $object_table_structure; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Get WordPress fields for an object |
||
| 338 | * |
||
| 339 | * @param string $wordpress_object The type of WordPress object. |
||
| 340 | * @param string $id_field The field of that object that corresponds with its ID in the database. |
||
| 341 | * @return array $object_fields |
||
| 342 | */ |
||
| 343 | public function get_wordpress_object_fields( $wordpress_object, $id_field = 'ID' ) { |
||
| 344 | |||
| 345 | $object_table_structure = $this->get_wordpress_table_structure( $wordpress_object ); |
||
| 346 | |||
| 347 | $meta_table = $object_table_structure['meta_table']; |
||
| 348 | $meta_methods = maybe_unserialize( $object_table_structure['meta_methods'] ); |
||
| 349 | $content_table = $object_table_structure['content_table']; |
||
| 350 | $content_methods = maybe_unserialize( $object_table_structure['content_methods'] ); |
||
| 351 | $id_field = $object_table_structure['id_field']; |
||
| 352 | $object_name = $object_table_structure['object_name']; |
||
| 353 | $where = $object_table_structure['where']; |
||
| 354 | $ignore_keys = $object_table_structure['ignore_keys']; |
||
| 355 | |||
| 356 | $object_fields = array(); |
||
| 357 | |||
| 358 | // Try to find the object fields in cache before acquiring it from other source. |
||
| 359 | if ( true === $this->options['cache'] && 'write' !== $this->options['cache'] ) { |
||
| 360 | $cached = $this->cache_get( $wordpress_object, array( 'data', 'meta' ) ); |
||
| 361 | if ( is_array( $cached ) ) { |
||
| 362 | $object_fields['data'] = $cached; |
||
| 363 | $object_fields['from_cache'] = true; |
||
| 364 | $object_fields['cached'] = true; |
||
| 365 | } else { |
||
| 366 | $object_fields['data'] = $this->object_fields( $object_name, $id_field, $content_table, $content_methods, $meta_table, $meta_methods, $where, $ignore_keys ); |
||
| 367 | if ( ! empty( $object_fields['data'] ) ) { |
||
| 368 | $object_fields['cached'] = $this->cache_set( $wordpress_object, array( 'data', 'meta' ), $object_fields['data'], $this->options['cache_expiration'] ); |
||
| 369 | } else { |
||
| 370 | $object_fields['cached'] = false; |
||
| 371 | } |
||
| 372 | $object_fields['from_cache'] = false; |
||
| 373 | } |
||
| 374 | } else { |
||
| 375 | $object_fields['data'] = $this->object_fields( $object_name, $id_field, $content_table, $content_methods, $meta_table, $meta_methods, $where, $ignore_keys ); |
||
| 376 | $object_fields['from_cache'] = false; |
||
| 377 | $object_fields['cached'] = false; |
||
| 378 | } |
||
| 379 | |||
| 380 | /* |
||
| 381 | * Developers can use this hook to change the WordPress object field array. |
||
| 382 | * The returned $object_fields needs to be an array like is described earlier in this method: |
||
| 383 | * $object_fields = array( 'data' => array(), 'from_cache' => bool, 'cached' => bool ); |
||
| 384 | * This is useful for custom objects that do not use the normal metadata table structure. |
||
| 385 | */ |
||
| 386 | $object_fields = apply_filters( $this->option_prefix . 'wordpress_object_fields', $object_fields, $wordpress_object ); |
||
| 387 | |||
| 388 | return $object_fields['data']; |
||
| 389 | |||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Get WordPress data based on what object it is |
||
| 394 | * |
||
| 395 | * @param string $object_type The type of object. |
||
| 396 | * @param int $object_id The ID of the object. |
||
| 397 | * @param bool $is_deleted Whether the WordPress object has been deleted. |
||
| 398 | * @return array $wordpress_object |
||
| 399 | */ |
||
| 400 | public function get_wordpress_object_data( $object_type, $object_id, $is_deleted = false ) { |
||
| 401 | $wordpress_object = array(); |
||
| 402 | $object_table_structure = $this->get_wordpress_table_structure( $object_type ); |
||
| 403 | |||
| 404 | $meta_table = $object_table_structure['meta_table']; |
||
| 405 | $meta_methods = maybe_unserialize( $object_table_structure['meta_methods'] ); |
||
| 406 | $content_table = $object_table_structure['content_table']; |
||
| 407 | $content_methods = maybe_unserialize( $object_table_structure['content_methods'] ); |
||
| 408 | $id_field = $object_table_structure['id_field']; |
||
| 409 | $object_name = $object_table_structure['object_name']; |
||
| 410 | $where = $object_table_structure['where']; |
||
| 411 | $ignore_keys = $object_table_structure['ignore_keys']; |
||
| 412 | |||
| 413 | if ( true === $is_deleted ) { |
||
| 414 | $wordpress_object = array(); |
||
| 415 | $wordpress_object[ $id_field ] = $object_id; |
||
| 416 | return $wordpress_object; |
||
| 417 | } |
||
| 418 | |||
| 419 | if ( 'user' === $object_type ) { |
||
| 420 | $data = get_userdata( $object_id ); |
||
| 421 | } elseif ( 'post' === $object_type || 'attachment' === $object_type ) { |
||
| 422 | $data = get_post( $object_id ); |
||
| 423 | } elseif ( 'category' === $object_type || 'tag' === $object_type || 'post_tag' === $object_type ) { |
||
| 424 | $data = get_term( $object_id ); |
||
| 425 | } elseif ( 'comment' === $object_type ) { |
||
| 426 | $data = get_comment( $object_id ); |
||
| 427 | } else { // This is for custom post types. |
||
| 428 | $data = get_post( $object_id ); |
||
| 429 | } |
||
| 430 | |||
| 431 | if ( ! is_object( $data ) ) { |
||
| 432 | return $wordpress_object; |
||
| 433 | } |
||
| 434 | |||
| 435 | $fields = $this->get_wordpress_object_fields( $object_type ); |
||
| 436 | foreach ( $fields as $key => $value ) { |
||
| 437 | $field = $value['key']; |
||
| 438 | $wordpress_object[ $field ] = $data->{$field}; |
||
| 439 | } |
||
| 440 | |||
| 441 | /* |
||
| 442 | * Allow developers to change the WordPress object, including any formatting that needs to happen to the data. |
||
| 443 | * The returned $wordpress_object needs to be an array like described above. |
||
| 444 | * This is useful for custom objects, hidden fields, or custom formatting. |
||
| 445 | * Here's an example of filters to add/modify data: |
||
| 446 | * |
||
| 447 | add_filter( 'object_sync_for_salesforce_wordpress_object_data', 'modify_data', 10, 2 ); |
||
| 448 | function modify_data( $wordpress_object, $object_type ) { |
||
| 449 | $wordpress_object['field_a'] = 'i am a field value that salesforce wants to store but WordPress does not care about'; |
||
| 450 | // Add field values to specific WordPress objects such as 'post', 'page', 'user', a Custom Post Type, etc. |
||
| 451 | if ( 'user' === $object_type ) { |
||
| 452 | $wordpress_object['field_b'] = 'i am a field value that salesforce wants to store but WordPress does not care about'; |
||
| 453 | } |
||
| 454 | return $wordpress_object; |
||
| 455 | } |
||
| 456 | */ |
||
| 457 | |||
| 458 | $wordpress_object = apply_filters( $this->option_prefix . 'wordpress_object_data', $wordpress_object, $object_type ); |
||
| 459 | |||
| 460 | return $wordpress_object; |
||
| 461 | |||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Check to see if this API call exists in the cache |
||
| 466 | * if it does, return the transient for that key |
||
| 467 | * |
||
| 468 | * @param string $url The API call we'd like to make. |
||
| 469 | * @param array $args The arguents of the API call. |
||
| 470 | * @return $this->sfwp_transients->get $cachekey |
||
| 471 | */ |
||
| 472 | public function cache_get( $url, $args ) { |
||
| 473 | if ( is_array( $args ) ) { |
||
| 474 | $args[] = $url; |
||
| 475 | array_multisort( $args ); |
||
| 476 | } else { |
||
| 477 | $args .= $url; |
||
| 478 | } |
||
| 479 | |||
| 480 | $cachekey = md5( wp_json_encode( $args ) ); |
||
| 481 | return $this->sfwp_transients->get( $cachekey ); |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Create a cache entry for the current result, with the url and args as the key |
||
| 486 | * |
||
| 487 | * @param string $url The API query URL. |
||
| 488 | * @param array $args The arguments passed on the API query. |
||
| 489 | * @param array $data The data received. |
||
| 490 | * @param string $cache_expiration How long to keep the cache result around for. |
||
| 491 | * @return Bool whether or not the value was set |
||
| 492 | * @link https://wordpress.stackexchange.com/questions/174330/transient-storage-location-database-xcache-w3total-cache |
||
| 493 | */ |
||
| 494 | public function cache_set( $url, $args, $data, $cache_expiration = '' ) { |
||
| 495 | if ( is_array( $args ) ) { |
||
| 496 | $args[] = $url; |
||
| 497 | array_multisort( $args ); |
||
| 498 | } else { |
||
| 499 | $args .= $url; |
||
| 500 | } |
||
| 501 | $cachekey = md5( wp_json_encode( $args ) ); |
||
| 502 | // Cache_expiration is how long it should be stored in the cache. |
||
| 503 | // If we didn't give a custom one, use the default. |
||
| 504 | if ( '' === $cache_expiration ) { |
||
| 505 | $cache_expiration = $this->options['cache_expiration']; |
||
| 506 | } |
||
| 507 | return $this->sfwp_transients->set( $cachekey, $data, $cache_expiration ); |
||
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * If there is a WordPress setting for how long to keep this specific cache, return it and set the object property |
||
| 512 | * Otherwise, return seconds in 24 hours |
||
| 513 | * |
||
| 514 | * @param string $option_key The cache item to keep around. |
||
| 515 | * @param int $expire The default time after which to expire the cache. |
||
| 516 | * @return The cache expiration saved in the database. |
||
| 517 | */ |
||
| 518 | public function cache_expiration( $option_key, $expire ) { |
||
| 519 | $cache_expiration = get_option( $option_key, $expire ); |
||
| 520 | return $cache_expiration; |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Get all the fields for an object |
||
| 525 | * The important thing here is returning the fields as an array: |
||
| 526 | * $all_fields = array( 'key' => 'key name', 'table' => 'table name', 'methods' => array( 'create' => '', 'read' => '', 'update' => '', 'delete' => '' ) ); |
||
| 527 | * if there's a better way to do this than the mess of queries below, we should switch to that when we can |
||
| 528 | * we just need to make sure we get all applicable fields for the object itself, as well as its meta fields |
||
| 529 | * |
||
| 530 | * @param string $object_name THe name of the object type. |
||
| 531 | * @param string $id_field The database filed that contains its ID. |
||
| 532 | * @param string $content_table The table that normally contains such objects. |
||
| 533 | * @param array $content_methods Unused, but included as part of the return. |
||
| 534 | * @param string $meta_table The table where meta values for this object type are contained. |
||
| 535 | * @param array $meta_methods Unused, but included as part of the return. |
||
| 536 | * @param string $where SQL query. |
||
| 537 | * @param array $ignore_keys Fields to ignore from the database. |
||
| 538 | * @return array $all_fields The fields for the object. |
||
| 539 | */ |
||
| 540 | private function object_fields( $object_name, $id_field, $content_table, $content_methods, $meta_table, $meta_methods, $where, $ignore_keys = array() ) { |
||
| 541 | // These two queries load all the fields from the specified object unless they have been specified as ignore fields. |
||
| 542 | // They also load the fields that are meta_keys from the specified object's meta table. |
||
| 543 | // Maybe a box for a custom query, since custom fields get done in so many ways. |
||
| 544 | // Eventually this would be the kind of thing we could use fields api for, if it ever gets done. |
||
| 545 | $data_fields = $this->wpdb->get_col( "DESC {$content_table}", 0 ); |
||
| 546 | $data_field_types = $this->wpdb->get_col( "DESC {$content_table}", 1 ); // get the database field types. |
||
| 547 | |||
| 548 | if ( is_array( $meta_table ) ) { |
||
| 549 | $tax_table = $meta_table[1]; |
||
| 550 | $meta_table = $meta_table[0]; |
||
| 551 | } |
||
| 552 | $select_meta = ' |
||
| 553 | SELECT DISTINCT ' . $meta_table . '.meta_key |
||
| 554 | FROM ' . $content_table . ' |
||
| 555 | LEFT JOIN ' . $meta_table . ' |
||
| 556 | ON ' . $content_table . '.' . $id_field . ' = ' . $meta_table . '.' . $object_name . '_id |
||
| 557 | WHERE ' . $meta_table . '.meta_key != "" |
||
| 558 | ' . $where . ' |
||
| 559 | '; |
||
| 560 | $meta_fields = $this->wpdb->get_results( $select_meta ); |
||
| 561 | $all_fields = array(); |
||
| 562 | |||
| 563 | // by default, WordPress fields are editable except for an object ID field. |
||
| 564 | // use the filter below to change this for any given field. |
||
| 565 | // if a field is not editable, it will show in the fieldmap screen with a lock, and will be removed when saving data into WordPress. |
||
| 566 | |||
| 567 | /* // phpcs:ignore Squiz.PHP.CommentedOutCode.Found |
||
| 568 | add_filter( 'object_sync_for_salesforce_wordpress_field_is_editable', 'wordpress_field_is_editable', 10, 2 ); |
||
| 569 | function wordpress_field_is_editable( $editable, $field_name ) { |
||
| 570 | if ( 'ID' === $field_name ) { |
||
| 571 | $editable = true; |
||
| 572 | } |
||
| 573 | return $editable; |
||
| 574 | } |
||
| 575 | */ |
||
| 576 | |||
| 577 | foreach ( $data_fields as $key => $value ) { |
||
| 578 | if ( ! in_array( $value, (array) $ignore_keys, true ) ) { |
||
| 579 | $editable = true; |
||
| 580 | if ( $value === $id_field ) { |
||
| 581 | $editable = false; |
||
| 582 | } |
||
| 583 | $editable = apply_filters( $this->option_prefix . 'wordpress_field_is_editable', $editable, $value ); |
||
| 584 | $all_fields[] = array( |
||
| 585 | 'key' => $value, |
||
| 586 | 'table' => $content_table, |
||
| 587 | 'methods' => serialize( $content_methods ), |
||
| 588 | 'type' => $data_field_types[ $key ], |
||
| 589 | 'editable' => $editable, |
||
| 590 | ); |
||
| 591 | } |
||
| 592 | } |
||
| 593 | |||
| 594 | foreach ( $meta_fields as $key => $value ) { |
||
| 595 | if ( ! in_array( $value->meta_key, (array) $ignore_keys, true ) ) { |
||
| 596 | $editable = true; |
||
| 597 | if ( $value === $id_field ) { |
||
| 598 | $editable = false; |
||
| 599 | } |
||
| 600 | $editable = apply_filters( $this->option_prefix . 'wordpress_field_is_editable', $editable, $value->meta_key ); |
||
| 601 | $all_fields[] = array( |
||
| 602 | 'key' => $value->meta_key, |
||
| 603 | 'table' => $meta_table, |
||
| 604 | 'methods' => serialize( $meta_methods ), |
||
| 605 | 'editable' => $editable, |
||
| 606 | ); |
||
| 607 | } |
||
| 608 | } |
||
| 609 | |||
| 610 | if ( 'term' === $object_name ) { |
||
| 611 | $taxonomy = $this->wpdb->get_col( "DESC {$tax_table}", 0 ); |
||
| 612 | foreach ( $taxonomy as $key => $value ) { |
||
| 613 | $exists = array_search( $value, array_column( $all_fields, 'key' ), true ); |
||
| 614 | if ( 0 !== $exists ) { |
||
| 615 | $editable = true; |
||
| 616 | if ( $value === $id_field ) { |
||
| 617 | $editable = false; |
||
| 618 | } |
||
| 619 | $editable = apply_filters( $this->option_prefix . 'wordpress_field_is_editable', $editable, $value ); |
||
| 620 | $all_fields[] = array( |
||
| 621 | 'key' => $value, |
||
| 622 | 'table' => $tax_table, |
||
| 623 | 'methods' => serialize( $content_methods ), |
||
| 624 | 'editable' => $editable, |
||
| 625 | ); |
||
| 626 | } |
||
| 627 | } |
||
| 628 | } |
||
| 629 | |||
| 630 | return $all_fields; |
||
| 631 | |||
| 632 | } |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Create a new object of a given type. |
||
| 636 | * |
||
| 637 | * @param string $name Object type name, E.g., user, post, comment. |
||
| 638 | * @param array $params Values of the fields to set for the object. |
||
| 639 | * |
||
| 640 | * @return array |
||
| 641 | * data: |
||
| 642 | * id : 123, |
||
| 643 | * success : true |
||
| 644 | * errors : [ ], |
||
| 645 | * from_cache: |
||
| 646 | * cached: |
||
| 647 | * is_redo: |
||
| 648 | * |
||
| 649 | * part of CRUD for WordPress objects |
||
| 650 | */ |
||
| 651 | public function object_create( $name, $params ) { |
||
| 652 | |||
| 653 | $structure = $this->get_wordpress_table_structure( $name ); |
||
| 654 | $id_field = $structure['id_field']; |
||
| 655 | |||
| 656 | switch ( $name ) { |
||
| 657 | case 'user': |
||
| 658 | $result = $this->user_create( $params, $id_field ); |
||
| 659 | break; |
||
| 660 | case 'post': |
||
| 661 | $result = $this->post_create( $params, $id_field ); |
||
| 662 | break; |
||
| 663 | case 'attachment': |
||
| 664 | $result = $this->attachment_create( $params, $id_field ); |
||
| 665 | break; |
||
| 666 | case 'category': |
||
| 667 | case 'tag': |
||
| 668 | case 'post_tag': |
||
| 669 | $result = $this->term_create( $params, $name, $id_field ); |
||
| 670 | break; |
||
| 671 | case 'comment': |
||
| 672 | $result = $this->comment_create( $params, $id_field ); |
||
| 673 | break; |
||
| 674 | default: |
||
| 675 | /* |
||
| 676 | * Developers can use this hook to create objects with their own methods. |
||
| 677 | * The returned $result needs to be an array like this. |
||
| 678 | * $id_field should be the database key; i.e. 'ID' and the value should be the new item's id, or whatever WordPress returns from the method (sometimes this can be an error) |
||
| 679 | * $success should be a boolean value |
||
| 680 | $result = array( 'data' => array( $id_field => $post_id, 'success' => $success ), 'errors' => $errors ); |
||
| 681 | * use hook like: add_filter( 'object_sync_for_salesforce_create_custom_wordpress_item', add_object, 10, 1 ); |
||
| 682 | * the one param is: array( 'name' => objecttype, 'params' => array_of_params, 'id_field' => idfield ) |
||
| 683 | */ |
||
| 684 | // Check to see if someone is calling the filter, and apply it if so. |
||
| 685 | if ( ! has_filter( $this->option_prefix . 'create_custom_wordpress_item' ) ) { |
||
| 686 | $result = $this->post_create( $params, $id_field, $name ); |
||
| 687 | } else { |
||
| 688 | $result = apply_filters( |
||
| 689 | $this->option_prefix . 'create_custom_wordpress_item', |
||
| 690 | array( |
||
| 691 | 'params' => $params, |
||
| 692 | 'name' => $name, |
||
| 693 | 'id_field' => $id_field, |
||
| 694 | ) |
||
| 695 | ); |
||
| 696 | } |
||
| 697 | break; |
||
| 698 | } // End switch() method. |
||
| 699 | |||
| 700 | return $result; |
||
| 701 | } |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Create new records or update existing records. |
||
| 705 | * |
||
| 706 | * The new records or updated records are based on the value of the specified |
||
| 707 | * field. If the value is not unique, REST API returns a 300 response with |
||
| 708 | * the list of matching records. |
||
| 709 | * |
||
| 710 | * @param string $name Object type name, E.g., user, post, comment. |
||
| 711 | * @param string $key The field to check if this record should be created or updated. |
||
| 712 | * @param string $value The value for this record of the field specified for $key. |
||
| 713 | * @param array $methods What WordPress methods do we use to get the data, if there are any. otherwise, maybe will have to do a wpdb query. |
||
| 714 | * @param array $params Values of the fields to set for the object. |
||
| 715 | * @param bool $pull_to_drafts Whether to save to WordPress drafts when pulling from Salesforce. |
||
| 716 | * @param bool $check_only Allows this method to only check for matching records, instead of making any data changes. |
||
| 717 | * |
||
| 718 | * @return array |
||
| 719 | * data: |
||
| 720 | * "id" : 123, |
||
| 721 | * "success" : true |
||
| 722 | * "errors" : [ ], |
||
| 723 | * from_cache: |
||
| 724 | * cached: |
||
| 725 | * is_redo: |
||
| 726 | * |
||
| 727 | * part of CRUD for WordPress objects |
||
| 728 | */ |
||
| 729 | public function object_upsert( $name, $key, $value, $methods, $params, $pull_to_drafts = false, $check_only = false ) { |
||
| 730 | |||
| 731 | $structure = $this->get_wordpress_table_structure( $name ); |
||
| 732 | $id_field = $structure['id_field']; |
||
| 733 | |||
| 734 | // If key is set, remove from $params to avoid SQL errors. |
||
| 735 | if ( isset( $params[ $key ] ) ) { |
||
| 736 | unset( $params[ $key ] ); |
||
| 737 | } |
||
| 738 | |||
| 739 | // Allow developers to change both the key and value by which objects should be matched. |
||
| 740 | $key = apply_filters( $this->option_prefix . 'modify_upsert_key', $key ); |
||
| 741 | $value = apply_filters( $this->option_prefix . 'modify_upsert_value', $value ); |
||
| 742 | |||
| 743 | switch ( $name ) { |
||
| 744 | case 'user': |
||
| 745 | $result = $this->user_upsert( $key, $value, $methods, $params, $id_field, $pull_to_drafts, $check_only ); |
||
| 746 | break; |
||
| 747 | case 'post': |
||
| 748 | $result = $this->post_upsert( $key, $value, $methods, $params, $id_field, $pull_to_drafts, $name, $check_only ); |
||
| 749 | break; |
||
| 750 | case 'attachment': |
||
| 751 | $result = $this->attachment_upsert( $key, $value, $methods, $params, $id_field, $check_only ); |
||
| 752 | break; |
||
| 753 | case 'category': |
||
| 754 | case 'tag': |
||
| 755 | case 'post_tag': |
||
| 756 | $result = $this->term_upsert( $key, $value, $methods, $params, $name, $id_field, $check_only ); |
||
| 757 | break; |
||
| 758 | case 'comment': |
||
| 759 | $result = $this->comment_upsert( $key, $value, $methods, $params, $id_field, $pull_to_drafts, $check_only ); |
||
| 760 | break; |
||
| 761 | default: |
||
| 762 | /* |
||
| 763 | * Developers can use this hook to upsert objects with their own methods. |
||
| 764 | * The returned $result needs to be an array like this: |
||
| 765 | * $id_field should be the database key; i.e. 'ID' and the value should be the item's id, or whatever WordPress returns from the method (sometimes this can be an error) |
||
| 766 | * $success should be a boolean value |
||
| 767 | * $result = array( 'data' => array( $id_field => $post_id, 'success' => $success ), 'errors' => $errors ); |
||
| 768 | * Use hook like this: |
||
| 769 | * add_filter( 'object_sync_for_salesforce_upsert_custom_wordpress_item', upsert_object, 10, 1 ); |
||
| 770 | * The one param is: |
||
| 771 | * array( 'key' => key, 'value' => value, 'methods' => methods, 'params' => array_of_params, 'id_field' => idfield, 'pull_to_drafts' => pulltodrafts, 'name' => name, 'check_only' => $check_only ) |
||
| 772 | */ |
||
| 773 | // Check to see if someone is calling the filter, and apply it if so. |
||
| 774 | if ( ! has_filter( $this->option_prefix . 'upsert_custom_wordpress_item' ) ) { |
||
| 775 | $result = $this->post_upsert( $key, $value, $methods, $params, $id_field, $pull_to_drafts, $name, $check_only ); |
||
| 776 | } else { |
||
| 777 | $result = apply_filters( |
||
| 778 | $this->option_prefix . 'upsert_custom_wordpress_item', |
||
| 779 | array( |
||
| 780 | 'key' => $key, |
||
| 781 | 'value' => $value, |
||
| 782 | 'methods' => $methods, |
||
| 783 | 'params' => $params, |
||
| 784 | 'id_field' => $id_field, |
||
| 785 | 'pull_to_drafts' => $pull_to_drafts, |
||
| 786 | 'name' => $name, |
||
| 787 | 'check_only' => $check_only, |
||
| 788 | ) |
||
| 789 | ); |
||
| 790 | } |
||
| 791 | break; |
||
| 792 | } // End switch() method. |
||
| 793 | |||
| 794 | return $result; |
||
| 795 | } |
||
| 796 | |||
| 797 | /** |
||
| 798 | * Update an existing object. Part of CRUD for WordPress objects |
||
| 799 | * |
||
| 800 | * @param string $name Object type name, E.g., user, post, comment. |
||
| 801 | * @param int $id WordPress id of the object. |
||
| 802 | * @param array $params Values of the fields to set for the object. |
||
| 803 | * @param array $mapping_object is the object map connecting the records. |
||
| 804 | * @return array |
||
| 805 | * data: |
||
| 806 | * success: 1 |
||
| 807 | * "errors" : [ ], |
||
| 808 | * from_cache: |
||
| 809 | * cached: |
||
| 810 | * is_redo: |
||
| 811 | */ |
||
| 812 | public function object_update( $name, $id, $params, $mapping_object = array() ) { |
||
| 813 | |||
| 814 | $structure = $this->get_wordpress_table_structure( $name ); |
||
| 815 | $id_field = $structure['id_field']; |
||
| 816 | |||
| 817 | switch ( $name ) { |
||
| 818 | case 'user': |
||
| 819 | // User id does not come through by default, but we need it to pass to wp method. |
||
| 820 | $result = $this->user_update( $id, $params, $id_field ); |
||
| 821 | break; |
||
| 822 | case 'post': |
||
| 823 | $result = $this->post_update( $id, $params, $id_field ); |
||
| 824 | break; |
||
| 825 | case 'attachment': |
||
| 826 | $result = $this->attachment_update( $id, $params, $id_field ); |
||
| 827 | break; |
||
| 828 | case 'category': |
||
| 829 | case 'tag': |
||
| 830 | case 'post_tag': |
||
| 831 | $result = $this->term_update( $id, $params, $name, $id_field ); |
||
| 832 | break; |
||
| 833 | case 'comment': |
||
| 834 | $result = $this->comment_update( $id, $params, $id_field ); |
||
| 835 | break; |
||
| 836 | default: |
||
| 837 | /* |
||
| 838 | * Developers can use this hook to update objects with their own methods. |
||
| 839 | * The returned $result needs to be an array like this: |
||
| 840 | * $id_field should be the database key; i.e. 'ID' and the value should be the updated item's id, or whatever WordPress returns from the method (sometimes this can be an error) |
||
| 841 | * $success should be a boolean value |
||
| 842 | * $result = array( 'data' => array( $id_field => $post_id, 'success' => $success ), 'errors' => $errors ); |
||
| 843 | * Use hook like this: |
||
| 844 | * add_filter( 'object_sync_for_salesforce_update_custom_wordpress_item', update_object, 10, 1 ); |
||
| 845 | * The one param is: |
||
| 846 | * array( 'id' => id, 'params' => array_of_params, 'name' => objecttype, 'id_field' => idfield ) |
||
| 847 | */ |
||
| 848 | // Check to see if someone is calling the filter, and apply it if so. |
||
| 849 | if ( ! has_filter( $this->option_prefix . 'update_custom_wordpress_item' ) ) { |
||
| 850 | $result = $this->post_update( $id, $params, $id_field, $name ); |
||
| 851 | } else { |
||
| 852 | $result = apply_filters( |
||
| 853 | $this->option_prefix . 'update_custom_wordpress_item', |
||
| 854 | array( |
||
| 855 | 'id' => $id, |
||
| 856 | 'params' => $params, |
||
| 857 | 'name' => $name, |
||
| 858 | 'id_field' => $id_field, |
||
| 859 | ) |
||
| 860 | ); |
||
| 861 | } |
||
| 862 | break; |
||
| 863 | } // End switch() method. |
||
| 864 | |||
| 865 | return $result; |
||
| 866 | } |
||
| 867 | |||
| 868 | /** |
||
| 869 | * Delete a WordPress object. |
||
| 870 | * |
||
| 871 | * @param string $name Object type name, E.g., user, post, comment. |
||
| 872 | * @param int $id WordPress id of the object. |
||
| 873 | * |
||
| 874 | * @return array |
||
| 875 | * data: |
||
| 876 | * success: 1 |
||
| 877 | * "errors" : [ ], |
||
| 878 | * |
||
| 879 | * part of CRUD for WordPress objects |
||
| 880 | */ |
||
| 881 | public function object_delete( $name, $id ) { |
||
| 882 | $structure = $this->get_wordpress_table_structure( $name ); |
||
| 883 | $id_field = $structure['id_field']; |
||
| 884 | |||
| 885 | switch ( $name ) { |
||
| 886 | case 'user': |
||
| 887 | $success = $this->user_delete( $id ); |
||
| 888 | break; |
||
| 889 | case 'post': |
||
| 890 | $success = $this->post_delete( $id ); |
||
| 891 | break; |
||
| 892 | case 'attachment': |
||
| 893 | $success = $this->attachment_delete( $id ); |
||
| 894 | break; |
||
| 895 | case 'category': |
||
| 896 | case 'tag': |
||
| 897 | case 'post_tag': |
||
| 898 | $success = $this->term_delete( $id, $name ); |
||
| 899 | break; |
||
| 900 | case 'comment': |
||
| 901 | $success = $this->comment_delete( $id ); |
||
| 902 | break; |
||
| 903 | default: |
||
| 904 | /* |
||
| 905 | * Developers can use this hook to delete objects with their own methods. |
||
| 906 | * The returned $success is an object of the correct type, or a FALSE |
||
| 907 | * Use hook like: |
||
| 908 | * add_filter( 'object_sync_for_salesforce_delete_custom_wordpress_item', delete_object, 10, 1 ); |
||
| 909 | * The one param is: |
||
| 910 | * array( 'id' => id, 'name' => objecttype ) |
||
| 911 | */ |
||
| 912 | // Check to see if someone is calling the filter, and apply it if so. |
||
| 913 | if ( ! has_filter( $this->option_prefix . 'delete_custom_wordpress_item' ) ) { |
||
| 914 | $success = $this->post_delete( $id ); |
||
| 915 | } else { |
||
| 916 | $success = apply_filters( |
||
| 917 | $this->option_prefix . 'delete_custom_wordpress_item', |
||
| 918 | array( |
||
| 919 | 'id' => $id, |
||
| 920 | 'name' => $name, |
||
| 921 | ) |
||
| 922 | ); |
||
| 923 | } |
||
| 924 | |||
| 925 | $success = $this->post_delete( $id ); |
||
| 926 | break; |
||
| 927 | } // End switch() method. |
||
| 928 | |||
| 929 | $result = array( |
||
| 930 | 'data' => array( |
||
| 931 | 'success' => $success, |
||
| 932 | ), |
||
| 933 | 'errors' => array(), |
||
| 934 | ); |
||
| 935 | return $result; |
||
| 936 | } |
||
| 937 | |||
| 938 | /** |
||
| 939 | * Create a new WordPress user. |
||
| 940 | * |
||
| 941 | * @param array $params array of user data params. |
||
| 942 | * @param string $id_field The column in the DB that holdes the user ID. |
||
| 943 | * |
||
| 944 | * @return array |
||
| 945 | * data: |
||
| 946 | * ID : 123, |
||
| 947 | * success: 1 |
||
| 948 | * "errors" : [ ], |
||
| 949 | */ |
||
| 950 | private function user_create( $params, $id_field = 'ID' ) { |
||
| 951 | |||
| 952 | // Allow username to be email address or username. |
||
| 953 | // The username could be autogenerated before this point for the sake of URLs. |
||
| 954 | $username = $params['user_email']['value']; |
||
| 955 | $email_address = $params['user_email']['value']; |
||
| 956 | if ( isset( $params['user_login']['value'] ) ) { // User_login is used by username_exists. |
||
| 957 | $username = $params['user_login']['value']; |
||
| 958 | } else { |
||
| 959 | $params['user_login'] = array( |
||
| 960 | 'value' => $username, |
||
| 961 | 'method_modify' => 'wp_insert_user', |
||
| 962 | 'method_read' => 'get_user_by', |
||
| 963 | ); |
||
| 964 | } |
||
| 965 | |||
| 966 | // This is a new user. |
||
| 967 | if ( false === username_exists( $username ) ) { |
||
| 968 | |||
| 969 | // Create the user |
||
| 970 | // WordPress sends a password reset link so this password doesn't get used, but it does exist in the database, which is helpful to prevent access before the user uses their password reset email. |
||
| 971 | $params['user_pass'] = array( |
||
| 972 | 'value' => wp_generate_password( 12, false ), |
||
| 973 | 'method_modify' => 'wp_insert_user', |
||
| 974 | 'method_read' => 'get_user_by', |
||
| 975 | ); |
||
| 976 | // Load all params with a method_modify of the object structure's content_method into $content. |
||
| 977 | $content = array(); |
||
| 978 | $structure = $this->get_wordpress_table_structure( 'user' ); |
||
| 979 | foreach ( $params as $key => $value ) { |
||
| 980 | if ( in_array( $value['method_modify'], $structure['content_methods'], true ) ) { |
||
| 981 | $content[ $key ] = $value['value']; |
||
| 982 | unset( $params[ $key ] ); |
||
| 983 | } |
||
| 984 | } |
||
| 985 | |||
| 986 | $user_id = wp_insert_user( $content ); |
||
| 987 | |||
| 988 | if ( is_wp_error( $user_id ) ) { |
||
| 989 | $success = false; |
||
| 990 | $errors = $user_id; |
||
| 991 | } else { |
||
| 992 | $meta_result = $this->create_wp_meta( $params, $user_id, 'user' ); |
||
| 993 | $success = $meta_result['success']; |
||
| 994 | $errors = $meta_result['errors']; |
||
| 995 | |||
| 996 | // Developers can use this hook to set any other user data - permissions, etc. |
||
| 997 | do_action( $this->option_prefix . 'set_more_user_data', $user_id, $params, 'create' ); |
||
| 998 | |||
| 999 | // Send notification of new user. |
||
| 1000 | // todo: Figure out what permissions ought to get notifications for this and make sure it works the right way. In the meantime, allow developers to prevent the notification from being sent. |
||
| 1001 | if ( apply_filters( $this->option_prefix . 'send_new_user_notification', true, $user_id, $params ) ) { |
||
| 1002 | wp_new_user_notification( $user_id, null, 'both' ); |
||
| 1003 | } |
||
| 1004 | } |
||
| 1005 | } else { |
||
| 1006 | $user_id = username_exists( $username ); |
||
| 1007 | } // End if() statement. |
||
| 1008 | |||
| 1009 | if ( is_wp_error( $user_id ) ) { |
||
| 1010 | $success = false; |
||
| 1011 | $errors = $user_id; |
||
| 1012 | } else { |
||
| 1013 | $success = true; |
||
| 1014 | $errors = array(); |
||
| 1015 | } |
||
| 1016 | |||
| 1017 | $result = array( |
||
| 1018 | 'data' => array( |
||
| 1019 | $id_field => $user_id, |
||
| 1020 | 'success' => $success, |
||
| 1021 | ), |
||
| 1022 | 'errors' => $errors, |
||
| 1023 | ); |
||
| 1024 | |||
| 1025 | return $result; |
||
| 1026 | } |
||
| 1027 | |||
| 1028 | /** |
||
| 1029 | * Create a new WordPress user or update it if a match is found. |
||
| 1030 | * |
||
| 1031 | * @param string $key What key we are looking at for possible matches. |
||
| 1032 | * @param string $value What value we are looking at for possible matches. |
||
| 1033 | * @param array $methods What WordPress methods do we use to get the data, if there are any. otherwise, maybe will have to do a wpdb query. |
||
| 1034 | * @param array $params Array of user data params. This is generated by Object_Sync_Sf_Mapping::map_params(). |
||
| 1035 | * @param string $id_field Optional string of what the ID field is, if it is ever not ID. |
||
| 1036 | * @param bool $pull_to_drafts Whether to save to WordPress drafts when pulling from Salesforce. |
||
| 1037 | * @param bool $check_only Allows this method to only check for matching records, instead of making any data changes. |
||
| 1038 | * |
||
| 1039 | * @return array |
||
| 1040 | * data: |
||
| 1041 | * ID : 123, |
||
| 1042 | * success: 1 |
||
| 1043 | * "errors" : [ ], |
||
| 1044 | */ |
||
| 1045 | private function user_upsert( $key, $value, $methods, $params, $id_field = 'ID', $pull_to_drafts = false, $check_only = false ) { |
||
| 1046 | |||
| 1047 | // If the key is user_email, we need to make it just email because that is how the WordPress method reads it. |
||
| 1048 | $method = isset( $methods['method_match'] ) ? $methods['method_match'] : ''; |
||
| 1049 | if ( '' !== $method ) { |
||
| 1050 | // These methods should give us the user object if we are matching for one. |
||
| 1051 | // if we are trying to match to a meta field, the method is an object. |
||
| 1052 | if ( class_exists( $method ) ) { |
||
| 1053 | $args = array( |
||
| 1054 | 'meta_query' => array( |
||
| 1055 | array( |
||
| 1056 | 'key' => $key, |
||
| 1057 | 'value' => $value, |
||
| 1058 | ), |
||
| 1059 | ), |
||
| 1060 | ); |
||
| 1061 | $match_query = new $method( $args ); |
||
| 1062 | // the WP_User_Query object has a get_results method that we use. |
||
| 1063 | $users = $match_query->get_results(); |
||
| 1064 | if ( ! empty( $users ) ) { |
||
| 1065 | $user = $users[0]; |
||
| 1066 | } |
||
| 1067 | } else { |
||
| 1068 | $user = $method( str_replace( 'user_', '', $key ), $value ); |
||
| 1069 | } |
||
| 1070 | |||
| 1071 | if ( isset( $user ) && isset( $user->{$id_field} ) ) { |
||
| 1072 | // User does exist after checking the matching value. we want its id. |
||
| 1073 | $user_id = $user->{$id_field}; |
||
| 1074 | |||
| 1075 | if ( true === $check_only ) { |
||
| 1076 | // We are just checking to see if there is a match. |
||
| 1077 | return $user_id; |
||
| 1078 | } |
||
| 1079 | |||
| 1080 | // On the prematch fields, we specify the method_update param. |
||
| 1081 | if ( isset( $methods['method_update'] ) ) { |
||
| 1082 | $method = $methods['method_update']; |
||
| 1083 | } else { |
||
| 1084 | $method = $methods['method_modify']; |
||
| 1085 | } |
||
| 1086 | $params[ $key ] = array( |
||
| 1087 | 'value' => $value, |
||
| 1088 | 'method_modify' => $method, |
||
| 1089 | 'method_read' => $methods['method_read'], |
||
| 1090 | ); |
||
| 1091 | } elseif ( false === $check_only ) { |
||
| 1092 | // User does not exist after checking the matching value. create it. |
||
| 1093 | // On the prematch fields, we specify the method_create param. |
||
| 1094 | if ( isset( $methods['method_create'] ) ) { |
||
| 1095 | $method = $methods['method_create']; |
||
| 1096 | } else { |
||
| 1097 | $method = $methods['method_modify']; |
||
| 1098 | } |
||
| 1099 | $params[ $key ] = array( |
||
| 1100 | 'value' => $value, |
||
| 1101 | 'method_modify' => $method, |
||
| 1102 | 'method_read' => $methods['method_read'], |
||
| 1103 | ); |
||
| 1104 | $result = $this->user_create( $params ); |
||
| 1105 | return $result; |
||
| 1106 | } else { |
||
| 1107 | // Check only is true but there's not a user yet. |
||
| 1108 | return null; |
||
| 1109 | } // End if() statement. |
||
| 1110 | } else { |
||
| 1111 | // There is no method by which to check the user. we can check other ways here. |
||
| 1112 | $params[ $key ] = array( |
||
| 1113 | 'value' => $value, |
||
| 1114 | 'method_modify' => $methods['method_modify'], |
||
| 1115 | 'method_read' => $methods['method_read'], |
||
| 1116 | ); |
||
| 1117 | |||
| 1118 | // Allow username to be email address or username. |
||
| 1119 | // The username could be autogenerated before this point for the sake of URLs. |
||
| 1120 | if ( isset( $params['user_email']['value'] ) ) { |
||
| 1121 | $username = $params['user_email']['value']; |
||
| 1122 | $email_address = $params['user_email']['value']; |
||
| 1123 | } |
||
| 1124 | if ( isset( $params['user_login']['value'] ) ) { // user_login is used by username_exists. |
||
| 1125 | $username = $params['user_login']['value']; |
||
| 1126 | } |
||
| 1127 | |||
| 1128 | $existing_id = username_exists( $username ); // Returns an id if there is a result. |
||
| 1129 | |||
| 1130 | // User does not exist after more checking. we want to create it. |
||
| 1131 | if ( false === $existing_id && false === $check_only ) { |
||
| 1132 | $result = $this->user_create( $params ); |
||
| 1133 | return $result; |
||
| 1134 | } elseif ( true === $check_only ) { |
||
| 1135 | // We are just checking to see if there is a match. |
||
| 1136 | return $existing_id; |
||
| 1137 | } else { |
||
| 1138 | // User does exist based on username, and we aren't doing a check only. we want to update the wp user here. |
||
| 1139 | $user_id = $existing_id; |
||
| 1140 | } |
||
| 1141 | } // End if() statement. |
||
| 1142 | |||
| 1143 | if ( isset( $user_id ) ) { |
||
| 1144 | foreach ( $params as $key => $value ) { |
||
| 1145 | $params[ $key ]['method_modify'] = $methods['method_update']; |
||
| 1146 | } |
||
| 1147 | $result = $this->user_update( $user_id, $params ); |
||
| 1148 | return $result; |
||
| 1149 | } |
||
| 1150 | |||
| 1151 | // Create log entry for lack of a user id. |
||
| 1152 | $status = 'error'; |
||
| 1153 | $title = sprintf( |
||
| 1154 | // translators: placeholders are: 1) the log status. |
||
| 1155 | esc_html__( '%1$s: Users: Tried to run user_upsert, and ended up without a user id', 'object-sync-for-salesforce' ), |
||
| 1156 | ucfirst( esc_attr( $status ) ) |
||
| 1157 | ); |
||
| 1158 | $this->logging->setup( |
||
| 1159 | // todo: can we get any more specific about this? |
||
| 1160 | $title, |
||
| 1161 | '', |
||
| 1162 | 0, |
||
| 1163 | 0, |
||
| 1164 | $status |
||
| 1165 | ); |
||
| 1166 | |||
| 1167 | } |
||
| 1168 | |||
| 1169 | /** |
||
| 1170 | * Update a WordPress user. |
||
| 1171 | * |
||
| 1172 | * @param string $user_id The ID for the user to be updated. This value needs to be in the array that is sent to wp_update_user. |
||
| 1173 | * @param array $params Array of user data params. |
||
| 1174 | * @param string $id_field Optional string of what the ID field is, if it is ever not ID. |
||
| 1175 | * |
||
| 1176 | * @return array |
||
| 1177 | * data: |
||
| 1178 | * success: 1 |
||
| 1179 | * "errors" : [ ], |
||
| 1180 | */ |
||
| 1181 | private function user_update( $user_id, $params, $id_field = 'ID' ) { |
||
| 1182 | $content = array(); |
||
| 1183 | $content[ $id_field ] = $user_id; |
||
| 1184 | foreach ( $params as $key => $value ) { |
||
| 1185 | |||
| 1186 | // if the update value for email already exists on another user, don't fail this update; keep the user's email address. |
||
| 1187 | if ( 'user_email' === $key && email_exists( $value['value'] ) ) { |
||
| 1188 | unset( $params[ $key ] ); |
||
| 1189 | continue; |
||
| 1190 | } |
||
| 1191 | |||
| 1192 | // if the update value for login already exists on another user, don't fail this update; keep the user's login. |
||
| 1193 | if ( 'user_login' === $key && username_exists( $value['value'] ) ) { |
||
| 1194 | unset( $params[ $key ] ); |
||
| 1195 | continue; |
||
| 1196 | } |
||
| 1197 | |||
| 1198 | if ( 'wp_update_user' === $value['method_modify'] ) { |
||
| 1199 | $content[ $key ] = $value['value']; |
||
| 1200 | unset( $params[ $key ] ); |
||
| 1201 | } |
||
| 1202 | } |
||
| 1203 | |||
| 1204 | $user_id = wp_update_user( $content ); |
||
| 1205 | |||
| 1206 | if ( is_wp_error( $user_id ) ) { |
||
| 1207 | $success = false; |
||
| 1208 | $errors = $user_id; |
||
| 1209 | } else { |
||
| 1210 | $meta_result = $this->update_wp_meta( $params, $user_id, 'user' ); |
||
| 1211 | $success = $meta_result['success']; |
||
| 1212 | $errors = $meta_result['errors']; |
||
| 1213 | // Developers can use this hook to set any other user data - permissions, etc. |
||
| 1214 | do_action( $this->option_prefix . 'set_more_user_data', $user_id, $params, 'update' ); |
||
| 1215 | } // End if() statement. |
||
| 1216 | |||
| 1217 | $result = array( |
||
| 1218 | 'data' => array( |
||
| 1219 | $id_field => $user_id, |
||
| 1220 | 'success' => $success, |
||
| 1221 | ), |
||
| 1222 | 'errors' => $errors, |
||
| 1223 | ); |
||
| 1224 | return $result; |
||
| 1225 | } |
||
| 1226 | |||
| 1227 | /** |
||
| 1228 | * Delete a WordPress user. |
||
| 1229 | * |
||
| 1230 | * @param int $id User ID. |
||
| 1231 | * @param int $reassign If we should reassign any posts to other users. We don't change this from NULL anywhere in this plugin. |
||
| 1232 | * |
||
| 1233 | * @return boolean true if successful |
||
| 1234 | */ |
||
| 1235 | private function user_delete( $id, $reassign = null ) { |
||
| 1236 | // According to https://developer.wordpress.org/reference/functions/wp_delete_user/ we have to include user.php first; otherwise it throws undefined error. |
||
| 1237 | require_once ABSPATH . 'wp-admin/includes/user.php'; |
||
| 1238 | $result = wp_delete_user( $id, $reassign ); |
||
| 1239 | return $result; |
||
| 1240 | } |
||
| 1241 | |||
| 1242 | /** |
||
| 1243 | * Create a new WordPress post. |
||
| 1244 | * |
||
| 1245 | * @param array $params Array of post data params. |
||
| 1246 | * @param string $id_field Optional string of what the ID field is, if it is ever not ID. |
||
| 1247 | * @param string $post_type Optional string for custom post type, if applicable. |
||
| 1248 | * |
||
| 1249 | * @return array |
||
| 1250 | * data: |
||
| 1251 | * ID : 123, |
||
| 1252 | * success: 1 |
||
| 1253 | * "errors" : [ ], |
||
| 1254 | */ |
||
| 1255 | private function post_create( $params, $id_field = 'ID', $post_type = 'post' ) { |
||
| 1256 | // Load all params with a method_modify of the object structure's content_method into $content. |
||
| 1257 | $content = array(); |
||
| 1258 | $structure = $this->get_wordpress_table_structure( $post_type ); |
||
| 1259 | foreach ( $params as $key => $value ) { |
||
| 1260 | if ( in_array( $value['method_modify'], $structure['content_methods'], true ) ) { |
||
| 1261 | $content[ $key ] = $value['value']; |
||
| 1262 | unset( $params[ $key ] ); |
||
| 1263 | } |
||
| 1264 | } |
||
| 1265 | |||
| 1266 | if ( '' !== $post_type ) { |
||
| 1267 | $content['post_type'] = $post_type; |
||
| 1268 | } |
||
| 1269 | |||
| 1270 | // WordPress post creation will fail with an object of 0 if there is no title or content |
||
| 1271 | // I think we should allow this to happen and not make users' data decisions, so |
||
| 1272 | // if we're receiving nothing for either of these, create a blank one so it doesn't fail |
||
| 1273 | // here we have to use $content because $params has already been unset. |
||
| 1274 | if ( ! isset( $content['post_title'] ) ) { |
||
| 1275 | $content['post_title'] = ' '; |
||
| 1276 | } |
||
| 1277 | if ( ! isset( $content['post_content'] ) ) { |
||
| 1278 | $content['post_content'] = ' '; |
||
| 1279 | } |
||
| 1280 | |||
| 1281 | if ( 'tribe_events' === $content['post_type'] && function_exists( 'tribe_create_event' ) ) { |
||
| 1282 | // borrowing some code from https://github.com/tacjtg/rhp-tribe-events/blob/master/rhp-tribe-events.php. |
||
| 1283 | if ( isset( $params['_EventStartDate'] ) ) { |
||
| 1284 | $content = $this->append_tec_event_dates( $params['_EventStartDate']['value'], 'start', $content ); |
||
| 1285 | unset( $params['_EventStartDate'] ); |
||
| 1286 | } |
||
| 1287 | if ( isset( $params['_EventEndDate'] ) ) { |
||
| 1288 | $content = $this->append_tec_event_dates( $params['_EventEndDate']['value'], 'end', $content ); |
||
| 1289 | unset( $params['_EventEndDate'] ); |
||
| 1290 | } |
||
| 1291 | $post_id = tribe_create_event( $content ); |
||
| 1292 | } else { |
||
| 1293 | $post_id = wp_insert_post( $content, true ); // return an error instead of a 0 id. |
||
| 1294 | } |
||
| 1295 | |||
| 1296 | if ( is_wp_error( $post_id ) ) { |
||
| 1297 | $success = false; |
||
| 1298 | $errors = $post_id; |
||
| 1299 | } else { |
||
| 1300 | // If it's a custom record type, fix the methods. |
||
| 1301 | if ( isset( $params['RecordTypeId']['value'] ) ) { |
||
| 1302 | $params['RecordTypeId']['method_modify'] = 'update_post_meta'; |
||
| 1303 | $params['RecordTypeId']['method_read'] = 'get_post_meta'; |
||
| 1304 | } |
||
| 1305 | $meta_result = $this->create_wp_meta( $params, $post_id, 'post' ); |
||
| 1306 | $success = $meta_result['success']; |
||
| 1307 | $errors = $meta_result['errors']; |
||
| 1308 | // Developers can use this hook to set any other post data. |
||
| 1309 | do_action( $this->option_prefix . 'set_more_post_data', $post_id, $params, 'create' ); |
||
| 1310 | } // End if() statement. |
||
| 1311 | |||
| 1312 | if ( is_wp_error( $post_id ) ) { |
||
| 1313 | $success = false; |
||
| 1314 | $errors = $post_id; |
||
| 1315 | } else { |
||
| 1316 | $success = true; |
||
| 1317 | $errors = array(); |
||
| 1318 | } |
||
| 1319 | |||
| 1320 | $result = array( |
||
| 1321 | 'data' => array( |
||
| 1322 | $id_field => $post_id, |
||
| 1323 | 'success' => $success, |
||
| 1324 | ), |
||
| 1325 | 'errors' => $errors, |
||
| 1326 | ); |
||
| 1327 | |||
| 1328 | return $result; |
||
| 1329 | } |
||
| 1330 | |||
| 1331 | /** |
||
| 1332 | * Create a new WordPress post or update it if a match is found. |
||
| 1333 | * |
||
| 1334 | * @param string $key What key we are looking at for possible matches. |
||
| 1335 | * @param string $value What value we are looking at for possible matches. |
||
| 1336 | * @param array $methods What WordPress methods do we use to get the data, if there are any. otherwise, maybe will have to do a wpdb query. |
||
| 1337 | * @param array $params Array of post data params. |
||
| 1338 | * @param string $id_field optional string of what the ID field is, if it is ever not ID. |
||
| 1339 | * @param bool $pull_to_drafts Whether to save to WordPress drafts when pulling from Salesforce. |
||
| 1340 | * @param string $post_type Optional string for custom post type, if applicable. |
||
| 1341 | * @param bool $check_only Allows this method to only check for matching records, instead of making any data changes. |
||
| 1342 | * |
||
| 1343 | * @return array |
||
| 1344 | * data: |
||
| 1345 | * ID : 123, |
||
| 1346 | * success: 1 |
||
| 1347 | * "errors" : [ ], |
||
| 1348 | */ |
||
| 1349 | private function post_upsert( $key, $value, $methods, $params, $id_field = 'ID', $pull_to_drafts = false, $post_type = 'post', $check_only = false ) { |
||
| 1350 | |||
| 1351 | $method = isset( $methods['method_match'] ) ? $methods['method_match'] : ''; |
||
| 1352 | if ( '' !== $method ) { |
||
| 1353 | // By default, posts use get_posts as the method. args can be like this. |
||
| 1354 | // The args don't really make sense, and are inconsistently documented. |
||
| 1355 | // These methods should give us the post object. |
||
| 1356 | $args = array(); |
||
| 1357 | if ( 'post_title' === $key ) { |
||
| 1358 | $params['post_title'] = array( |
||
| 1359 | 'value' => $value, |
||
| 1360 | 'method_modify' => $method, |
||
| 1361 | 'method_read' => $methods['method_read'], |
||
| 1362 | ); |
||
| 1363 | $args['name'] = sanitize_title( $value ); |
||
| 1364 | } else { |
||
| 1365 | $args[ $key ] = $value; |
||
| 1366 | } |
||
| 1367 | $args['post_type'] = $post_type; |
||
| 1368 | $post_statuses = array( 'publish' ); |
||
| 1369 | |||
| 1370 | if ( true === filter_var( $pull_to_drafts, FILTER_VALIDATE_BOOLEAN ) ) { |
||
| 1371 | $post_statuses[] = 'draft'; |
||
| 1372 | } |
||
| 1373 | $args['post_status'] = $post_statuses; |
||
| 1374 | |||
| 1375 | // if we are trying to match to a meta field, the method is an object. |
||
| 1376 | if ( class_exists( $method ) ) { |
||
| 1377 | unset( $args[ $key ] ); |
||
| 1378 | $args['meta_query'] = array( |
||
| 1379 | array( |
||
| 1380 | 'key' => $key, |
||
| 1381 | 'value' => $value, |
||
| 1382 | ), |
||
| 1383 | ); |
||
| 1384 | $match_query = new $method( $args ); |
||
| 1385 | // the WP_Query object has a get_posts method that we use. |
||
| 1386 | $posts = $match_query->get_posts(); |
||
| 1387 | } else { |
||
| 1388 | $posts = $method( $args ); |
||
| 1389 | } |
||
| 1390 | |||
| 1391 | if ( isset( $posts ) && isset( $posts[0]->{$id_field} ) ) { |
||
| 1392 | // Post does exist after checking the matching value. We want its id. |
||
| 1393 | $post_id = $posts[0]->{$id_field}; |
||
| 1394 | |||
| 1395 | if ( true === $check_only ) { |
||
| 1396 | // We are just checking to see if there is a match. |
||
| 1397 | return $post_id; |
||
| 1398 | } |
||
| 1399 | |||
| 1400 | // On the prematch fields, we specify the method_update param. |
||
| 1401 | if ( isset( $methods['method_update'] ) ) { |
||
| 1402 | $method = $methods['method_update']; |
||
| 1403 | } else { |
||
| 1404 | $method = $methods['method_modify']; |
||
| 1405 | } |
||
| 1406 | $params[ $key ] = array( |
||
| 1407 | 'value' => $value, |
||
| 1408 | 'method_modify' => $method, |
||
| 1409 | 'method_read' => $methods['method_read'], |
||
| 1410 | ); |
||
| 1411 | } elseif ( false === $check_only ) { |
||
| 1412 | // Post does not exist after checking the matching value. create it. |
||
| 1413 | // On the prematch fields, we specify the method_create param. |
||
| 1414 | if ( isset( $methods['method_create'] ) ) { |
||
| 1415 | $method = $methods['method_create']; |
||
| 1416 | } else { |
||
| 1417 | $method = $methods['method_modify']; |
||
| 1418 | } |
||
| 1419 | $params[ $key ] = array( |
||
| 1420 | 'value' => $value, |
||
| 1421 | 'method_modify' => $method, |
||
| 1422 | 'method_read' => $methods['method_read'], |
||
| 1423 | ); |
||
| 1424 | $result = $this->post_create( $params, $id_field, $post_type ); |
||
| 1425 | return $result; |
||
| 1426 | } else { |
||
| 1427 | // Check only is true but there's not a post yet. |
||
| 1428 | return null; |
||
| 1429 | } // End if() statement. |
||
| 1430 | } else { |
||
| 1431 | // There is no method by which to check the post. we can check other ways here. |
||
| 1432 | $params[ $key ] = array( |
||
| 1433 | 'value' => $value, |
||
| 1434 | 'method_modify' => $methods['method_modify'], |
||
| 1435 | 'method_read' => $methods['method_read'], |
||
| 1436 | ); |
||
| 1437 | |||
| 1438 | // If we have a title, use it to check for existing post. |
||
| 1439 | if ( isset( $params['post_title']['value'] ) ) { |
||
| 1440 | $title = $params['post_title']['value']; |
||
| 1441 | } |
||
| 1442 | |||
| 1443 | // If we have content, use it to check for existing post. |
||
| 1444 | if ( isset( $params['post_content']['value'] ) ) { |
||
| 1445 | $content = $params['post_content']['value']; |
||
| 1446 | } else { |
||
| 1447 | $content = ''; |
||
| 1448 | } |
||
| 1449 | |||
| 1450 | // If we have a date, use it to check for existing post. |
||
| 1451 | if ( isset( $params['post_date']['value'] ) ) { |
||
| 1452 | $date = $params['post_date']['value']; |
||
| 1453 | } else { |
||
| 1454 | $date = ''; |
||
| 1455 | } |
||
| 1456 | |||
| 1457 | $existing_id = post_exists( $title, $content, $date ); // Returns an id if there is a result. Returns 0 if not. |
||
| 1458 | |||
| 1459 | // Post does not exist after more checking. maybe we want to create it. |
||
| 1460 | if ( 0 === $existing_id && false === $check_only ) { |
||
| 1461 | $result = $this->post_create( $params, $id_field, $post_type ); |
||
| 1462 | return $result; |
||
| 1463 | } elseif ( true === $check_only ) { |
||
| 1464 | // We are just checking to see if there is a match. |
||
| 1465 | return $existing_id; |
||
| 1466 | } else { |
||
| 1467 | // Post does exist based on fields, and we aren't doing a check only. we want to update the wp post here. |
||
| 1468 | $post_id = $existing_id; |
||
| 1469 | } |
||
| 1470 | |||
| 1471 | return $result; |
||
| 1472 | |||
| 1473 | } // End if() statement. |
||
| 1474 | |||
| 1475 | if ( isset( $post_id ) ) { |
||
| 1476 | foreach ( $params as $key => $value ) { |
||
| 1477 | $params[ $key ]['method_modify'] = $methods['method_update']; |
||
| 1478 | } |
||
| 1479 | $result = $this->post_update( $post_id, $params, $id_field, $post_type ); |
||
| 1480 | return $result; |
||
| 1481 | } |
||
| 1482 | // Create log entry for lack of a post id. |
||
| 1483 | $status = 'error'; |
||
| 1484 | $title = sprintf( |
||
| 1485 | // translators: placeholders are: 1) the log status. |
||
| 1486 | esc_html__( '%1$s: Posts: Tried to run post_upsert, and ended up without a post id', 'object-sync-for-salesforce' ), |
||
| 1487 | ucfirst( esc_attr( $status ) ) |
||
| 1488 | ); |
||
| 1489 | $this->logging->setup( |
||
| 1490 | // todo: can we be more explicit here about what post upsert failed? |
||
| 1491 | $title, |
||
| 1492 | '', |
||
| 1493 | 0, |
||
| 1494 | 0, |
||
| 1495 | $status |
||
| 1496 | ); |
||
| 1497 | |||
| 1498 | } |
||
| 1499 | |||
| 1500 | /** |
||
| 1501 | * Update a WordPress post. |
||
| 1502 | * |
||
| 1503 | * @param string $post_id The ID for the post to be updated. This value needs to be in the array that is sent to wp_update_post. |
||
| 1504 | * @param array $params Array of post data params. |
||
| 1505 | * @param string $id_field Optional string of what the ID field is, if it is ever not ID. |
||
| 1506 | * @param string $post_type Optional string for custom post type, if applicable. |
||
| 1507 | * |
||
| 1508 | * @return array |
||
| 1509 | * data: |
||
| 1510 | * success: 1 |
||
| 1511 | * "errors" : [ ], |
||
| 1512 | */ |
||
| 1513 | private function post_update( $post_id, $params, $id_field = 'ID', $post_type = '' ) { |
||
| 1553 | } |
||
| 1554 | |||
| 1555 | /** |
||
| 1556 | * Delete a WordPress post. |
||
| 1557 | * |
||
| 1558 | * @param int $id Post ID. |
||
| 1559 | * @param bool $force_delete If we should bypass the trash. We don't change this from FALSE anywhere in this plugin. |
||
| 1560 | * |
||
| 1561 | * @return mixed post object if successful, false if failed |
||
| 1562 | */ |
||
| 1563 | private function post_delete( $id, $force_delete = false ) { |
||
| 1566 | } |
||
| 1567 | |||
| 1568 | /** |
||
| 1569 | * Create a new WordPress attachment. |
||
| 1570 | * |
||
| 1571 | * @param array $params Array of attachment data params. |
||
| 1572 | * @param string $id_field Optional string of what the ID field is, if it is ever not ID. |
||
| 1573 | * |
||
| 1574 | * @return array |
||
| 1575 | * data: |
||
| 1576 | * ID : 123, |
||
| 1577 | * success: 1 |
||
| 1578 | * "errors" : [ ], |
||
| 1579 | */ |
||
| 1580 | private function attachment_create( $params, $id_field = 'ID' ) { |
||
| 1642 | |||
| 1643 | } |
||
| 1644 | |||
| 1645 | /** |
||
| 1646 | * Create a new WordPress attachment or update it if a match is found. |
||
| 1647 | * |
||
| 1648 | * @param string $key What key we are looking at for possible matches. |
||
| 1649 | * @param string $value What value we are looking at for possible matches. |
||
| 1650 | * @param array $methods What WordPress methods do we use to get the data, if there are any. otherwise, maybe will have to do a wpdb query. |
||
| 1651 | * @param array $params Array of attachment data params. |
||
| 1652 | * @param string $id_field Optional string of what the ID field is, if it is ever not ID. |
||
| 1653 | * @param bool $check_only Allows this method to only check for matching records, instead of making any data changes. |
||
| 1654 | * |
||
| 1655 | * @return array |
||
| 1656 | * data: |
||
| 1657 | * ID : 123, |
||
| 1658 | * success: 1 |
||
| 1659 | * "errors" : [ ], |
||
| 1660 | */ |
||
| 1661 | private function attachment_upsert( $key, $value, $methods, $params, $id_field = 'ID', $check_only = false ) { |
||
| 1662 | |||
| 1663 | $method = isset( $methods['method_match'] ) ? $methods['method_match'] : ''; |
||
| 1664 | if ( '' !== $method ) { |
||
| 1665 | // Get_posts is more helpful here, so that is the method attachment uses for 'read'. |
||
| 1666 | // By default, posts use get_posts as the method. args can be like this. |
||
| 1667 | // The args don't really make sense, and are inconsistently documented. |
||
| 1668 | // These methods should give us the post object. |
||
| 1669 | $args = array(); |
||
| 1670 | if ( 'post_title' === $key ) { |
||
| 1671 | $params['post_title'] = array( |
||
| 1672 | 'value' => $value, |
||
| 1673 | 'method_modify' => $method, |
||
| 1674 | 'method_read' => $methods['method_read'], |
||
| 1675 | ); |
||
| 1676 | $args['name'] = sanitize_title( $value ); |
||
| 1677 | } else { |
||
| 1678 | $args[ $key ] = $value; |
||
| 1679 | } |
||
| 1680 | $args['post_type'] = 'attachment'; |
||
| 1681 | |||
| 1682 | // if we are trying to match to a meta field, the method is an object. |
||
| 1683 | if ( class_exists( $method ) ) { |
||
| 1684 | unset( $args[ $key ] ); |
||
| 1685 | $args['meta_query'] = array( |
||
| 1686 | array( |
||
| 1687 | 'key' => $key, |
||
| 1688 | 'value' => $value, |
||
| 1689 | ), |
||
| 1690 | ); |
||
| 1691 | $match_query = new $method( $args ); |
||
| 1692 | // the WP_Query object has a get_posts method that we use. |
||
| 1693 | $posts = $match_query->get_posts(); |
||
| 1694 | } else { |
||
| 1695 | $posts = $method( $args ); |
||
| 1696 | } |
||
| 1697 | |||
| 1698 | if ( isset( $posts ) && isset( $posts[0]->{$id_field} ) ) { |
||
| 1699 | // Attachment does exist after checking the matching value. we want its id. |
||
| 1700 | $attachment_id = $posts[0]->{$id_field}; |
||
| 1701 | |||
| 1702 | if ( true === $check_only ) { |
||
| 1703 | // We are just checking to see if there is a match. |
||
| 1704 | return $attachment_id; |
||
| 1705 | } |
||
| 1706 | |||
| 1707 | // On the prematch fields, we specify the method_update param. |
||
| 1708 | if ( isset( $methods['method_update'] ) ) { |
||
| 1709 | $method = $methods['method_update']; |
||
| 1710 | } else { |
||
| 1711 | $method = $methods['method_modify']; |
||
| 1712 | } |
||
| 1713 | $params[ $key ] = array( |
||
| 1714 | 'value' => $value, |
||
| 1715 | 'method_modify' => $method, |
||
| 1716 | 'method_read' => $methods['method_read'], |
||
| 1717 | ); |
||
| 1718 | } elseif ( false === $check_only ) { |
||
| 1719 | // Attachment does not exist after checking the matching value. create it. |
||
| 1720 | // On the prematch fields, we specify the method_create param. |
||
| 1721 | if ( isset( $methods['method_create'] ) ) { |
||
| 1722 | $method = $methods['method_create']; |
||
| 1723 | } else { |
||
| 1724 | $method = $methods['method_modify']; |
||
| 1725 | } |
||
| 1726 | $params[ $key ] = array( |
||
| 1727 | 'value' => $value, |
||
| 1728 | 'method_modify' => $method, |
||
| 1729 | 'method_read' => $methods['method_read'], |
||
| 1730 | ); |
||
| 1731 | $result = $this->attachment_create( $params ); |
||
| 1732 | return $result; |
||
| 1733 | } else { |
||
| 1734 | // Check only is true but there's not an attachment yet. |
||
| 1735 | return null; |
||
| 1736 | } // End if() statement. |
||
| 1737 | } else { |
||
| 1738 | // There is no method by which to check the post. we can check other ways here. |
||
| 1739 | $params[ $key ] = array( |
||
| 1740 | 'value' => $value, |
||
| 1741 | 'method_modify' => $methods['method_modify'], |
||
| 1742 | 'method_read' => $methods['method_read'], |
||
| 1743 | ); |
||
| 1744 | |||
| 1745 | // If we have a title, use it to check for existing post. |
||
| 1746 | if ( isset( $params['post_title']['value'] ) ) { |
||
| 1747 | $title = $params['post_title']['value']; |
||
| 1748 | } |
||
| 1749 | |||
| 1750 | // If we have content, use it to check for existing post. |
||
| 1751 | if ( isset( $params['post_content']['value'] ) ) { |
||
| 1752 | $content = $params['post_content']['value']; |
||
| 1753 | } else { |
||
| 1754 | $content = ''; |
||
| 1755 | } |
||
| 1756 | |||
| 1757 | // If we have a date, use it to check for existing post. |
||
| 1758 | if ( isset( $params['post_date']['value'] ) ) { |
||
| 1759 | $date = $params['post_date']['value']; |
||
| 1760 | } else { |
||
| 1761 | $date = ''; |
||
| 1762 | } |
||
| 1763 | |||
| 1764 | $existing_id = post_exists( $title, $content, $date ); // Returns an id if there is a result. Returns 0 if not. |
||
| 1765 | |||
| 1766 | // Attachment does not exist after more checking. maybe we want to create it. |
||
| 1767 | if ( 0 === $existing_id && false === $check_only ) { |
||
| 1768 | $result = $this->attachment_create( $params ); |
||
| 1769 | return $result; |
||
| 1770 | } elseif ( true === $check_only ) { |
||
| 1771 | // We are just checking to see if there is a match. |
||
| 1772 | return $existing_id; |
||
| 1773 | } else { |
||
| 1774 | // Attachment does exist based on fields, and we aren't doing a check only. we want to update the wp attachment here. |
||
| 1775 | $attachment_id = $existing_id; |
||
| 1776 | } |
||
| 1777 | |||
| 1778 | return $result; |
||
| 1779 | |||
| 1780 | } // End if() statement. |
||
| 1781 | |||
| 1782 | if ( isset( $attachment_id ) ) { |
||
| 1783 | foreach ( $params as $key => $value ) { |
||
| 1784 | $params[ $key ]['method_modify'] = $methods['method_update']; |
||
| 1785 | } |
||
| 1786 | $result = $this->attachment_update( $attachment_id, $params ); |
||
| 1787 | return $result; |
||
| 1788 | } |
||
| 1789 | |||
| 1790 | // Create log entry for lack of an attachment id. |
||
| 1791 | $status = 'error'; |
||
| 1792 | $title = sprintf( |
||
| 1793 | // translators: placeholders are: 1) the log status. |
||
| 1794 | esc_html__( '%1$s: Attachments: Tried to run attachment_upsert, and ended up without an attachment id', 'object-sync-for-salesforce' ), |
||
| 1795 | ucfirst( esc_attr( $status ) ) |
||
| 1796 | ); |
||
| 1797 | $this->logging->setup( |
||
| 1798 | $title, |
||
| 1799 | '', |
||
| 1800 | 0, |
||
| 1801 | 0, |
||
| 1802 | $status |
||
| 1803 | ); |
||
| 1804 | } |
||
| 1805 | |||
| 1806 | /** |
||
| 1807 | * Update a WordPress attachment. |
||
| 1808 | * |
||
| 1809 | * @param string $attachment_id The ID for the attachment to be updated. This value needs to be in the array that is sent to the update methods. |
||
| 1810 | * @param array $params Array of attachment data params. |
||
| 1811 | * @param string $id_field Optional string of what the ID field is, if it is ever not ID. |
||
| 1812 | * |
||
| 1813 | * @return array |
||
| 1814 | * data: |
||
| 1815 | * success: 1 |
||
| 1816 | * "errors" : [ ], |
||
| 1817 | * |
||
| 1818 | * Note: this method uses wp_insert_attachment for core content fields as there isn't a corresponding method for updating these rows |
||
| 1819 | * it does use wp_update_attachment_metadata for the meta fields, though. |
||
| 1820 | * Developers should use hooks to change this, if it does not meet their needs. |
||
| 1821 | */ |
||
| 1822 | private function attachment_update( $attachment_id, $params, $id_field = 'ID' ) { |
||
| 1823 | $content = array(); |
||
| 1824 | $content[ $id_field ] = $attachment_id; |
||
| 1825 | foreach ( $params as $key => $value ) { |
||
| 1826 | if ( 'wp_insert_attachment' === $value['method_modify'] ) { // Should also be insert attachment maybe. |
||
| 1827 | $content[ $key ] = $value['value']; |
||
| 1828 | unset( $params[ $key ] ); |
||
| 1829 | } |
||
| 1830 | } |
||
| 1831 | |||
| 1832 | if ( isset( $params['filename']['value'] ) ) { |
||
| 1833 | $filename = $params['filename']['value']; |
||
| 1834 | } else { |
||
| 1835 | $filename = false; |
||
| 1836 | } |
||
| 1837 | |||
| 1838 | if ( isset( $params['parent']['value'] ) ) { |
||
| 1839 | $parent = $params['parent']['value']; |
||
| 1840 | } else { |
||
| 1841 | $parent = 0; |
||
| 1842 | } |
||
| 1843 | |||
| 1844 | $attachment_id = wp_insert_attachment( $content, $filename, $parent ); |
||
| 1845 | |||
| 1846 | if ( is_wp_error( $attachment_id ) ) { |
||
| 1847 | $success = false; |
||
| 1848 | $errors = $attachment_id; |
||
| 1849 | } else { |
||
| 1850 | $success = true; |
||
| 1851 | $errors = array(); |
||
| 1852 | |||
| 1853 | if ( false !== $filename ) { |
||
| 1854 | // According to https://codex.wordpress.org/Function_Reference/wp_insert_attachment we need this file. |
||
| 1855 | require_once ABSPATH . 'wp-admin/includes/image.php'; |
||
| 1856 | // Generate metadata for the attachment. |
||
| 1857 | $attach_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
||
| 1858 | } |
||
| 1859 | |||
| 1860 | // Put the data from salesforce into the meta array. |
||
| 1861 | $attach_new_data = array(); |
||
| 1862 | foreach ( $params as $key => $value ) { |
||
| 1863 | $method = $value['method_modify']; |
||
| 1864 | $attach_new_data[ $key ] = $value['value']; |
||
| 1865 | } |
||
| 1866 | |||
| 1867 | if ( isset( $attach_data ) ) { |
||
| 1868 | $attach_data = array_merge( $attach_data, $attach_new_data ); |
||
| 1869 | } else { |
||
| 1870 | $attach_data = $attach_new_data; |
||
| 1871 | } |
||
| 1872 | |||
| 1873 | $meta_updated = wp_update_attachment_metadata( $attachment_id, $attach_data ); |
||
| 1874 | |||
| 1875 | if ( false === $meta_updated ) { |
||
| 1876 | $success = false; |
||
| 1877 | $errors[] = array( |
||
| 1878 | 'key' => $key, |
||
| 1879 | 'value' => $value, |
||
| 1880 | ); |
||
| 1881 | } |
||
| 1882 | |||
| 1883 | if ( 0 !== $parent ) { |
||
| 1884 | set_post_thumbnail( $parent_post_id, $attachment_id ); |
||
| 1885 | } |
||
| 1886 | |||
| 1887 | // Developers can use this hook to set any other attachment data. |
||
| 1888 | do_action( $this->option_prefix . 'set_more_attachment_data', $attachment_id, $params, 'update' ); |
||
| 1889 | |||
| 1890 | } // End if() statement. |
||
| 1891 | |||
| 1892 | $result = array( |
||
| 1893 | 'data' => array( |
||
| 1894 | $id_field => $attachment_id, |
||
| 1895 | 'success' => $success, |
||
| 1896 | ), |
||
| 1897 | 'errors' => $errors, |
||
| 1898 | ); |
||
| 1899 | return $result; |
||
| 1900 | } |
||
| 1901 | |||
| 1902 | /** |
||
| 1903 | * Delete a WordPress attachment. |
||
| 1904 | * |
||
| 1905 | * @param int $id Attachment ID. |
||
| 1906 | * @param bool $force_delete If we should bypass the trash. We don't change this from FALSE anywhere in this plugin. |
||
| 1907 | * |
||
| 1908 | * @return mixed |
||
| 1909 | * attachment object if successful, false if failed |
||
| 1910 | */ |
||
| 1911 | private function attachment_delete( $id, $force_delete = false ) { |
||
| 1912 | $result = wp_delete_attachment( $id, $force_delete ); |
||
| 1913 | return $result; |
||
| 1914 | } |
||
| 1915 | |||
| 1916 | /** |
||
| 1917 | * Create a new WordPress term. |
||
| 1918 | * |
||
| 1919 | * @param array $params Array of term data params. |
||
| 1920 | * @param string $taxonomy The taxonomy to which to add the term. this is required. |
||
| 1921 | * @param string $id_field Optional string of what the ID field is, if it is ever not ID. |
||
| 1922 | * |
||
| 1923 | * @return array |
||
| 1924 | * data: |
||
| 1925 | * ID : 123, |
||
| 1926 | * success: 1 |
||
| 1927 | * "errors" : [ ], |
||
| 1928 | */ |
||
| 1929 | private function term_create( $params, $taxonomy, $id_field = 'ID' ) { |
||
| 1930 | if ( 'tag' === $taxonomy ) { |
||
| 1931 | $taxonomy = 'post_tag'; |
||
| 1932 | } |
||
| 1933 | // Load all params with a method_modify of the object structure's content_method into $content. |
||
| 1934 | $content = array(); |
||
| 1935 | $structure = $this->get_wordpress_table_structure( $taxonomy ); |
||
| 1936 | $args = array(); |
||
| 1937 | foreach ( $params as $key => $value ) { |
||
| 1938 | if ( 'name' === $key ) { |
||
| 1939 | $name = $value['value']; |
||
| 1940 | unset( $params[ $key ] ); |
||
| 1941 | } |
||
| 1942 | if ( in_array( $value['method_modify'], $structure['content_methods'], true ) && 'name' !== $key ) { |
||
| 1943 | $args[ $key ] = $value['value']; |
||
| 1944 | unset( $params[ $key ] ); |
||
| 1945 | } |
||
| 1946 | } |
||
| 1947 | if ( isset( $name ) ) { |
||
| 1948 | $term = wp_insert_term( $name, $taxonomy, $args ); |
||
| 1949 | } |
||
| 1950 | |||
| 1951 | if ( is_wp_error( $term ) ) { |
||
| 1952 | $success = false; |
||
| 1953 | $errors = $term; |
||
| 1954 | } else { |
||
| 1955 | $term_id = $term[ "$id_field" ]; |
||
| 1956 | $meta_result = $this->create_wp_meta( $params, $term_id, 'term' ); |
||
| 1957 | $success = $meta_result['success']; |
||
| 1958 | $errors = $meta_result['errors']; |
||
| 1959 | // Developers can use this hook to set any other term data. |
||
| 1960 | do_action( $this->option_prefix . 'set_more_term_data', $term_id, $params, 'create' ); |
||
| 1961 | } // End if() statement. |
||
| 1962 | |||
| 1963 | if ( is_wp_error( $term ) ) { |
||
| 1964 | $success = false; |
||
| 1965 | $errors = $term; |
||
| 1966 | } else { |
||
| 1967 | $success = true; |
||
| 1968 | $errors = array(); |
||
| 1969 | } |
||
| 1970 | |||
| 1971 | $result = array( |
||
| 1972 | 'data' => array( |
||
| 1973 | $id_field => $term_id, |
||
| 1974 | 'success' => $success, |
||
| 1975 | ), |
||
| 1976 | 'errors' => $errors, |
||
| 1977 | ); |
||
| 1978 | |||
| 1979 | return $result; |
||
| 1980 | |||
| 1981 | } |
||
| 1982 | |||
| 1983 | /** |
||
| 1984 | * Create a new WordPress term or update it if a match is found. |
||
| 1985 | * |
||
| 1986 | * @param string $key What key we are looking at for possible matches. |
||
| 1987 | * @param string $value What value we are looking at for possible matches. |
||
| 1988 | * @param array $methods What WordPress methods do we use to get the data, if there are any. otherwise, maybe will have to do a wpdb query. |
||
| 1989 | * @param array $params Array of term data params. |
||
| 1990 | * @param string $taxonomy The taxonomy to which to add the term. this is required.. |
||
| 1991 | * @param string $id_field Optional string of what the ID field is, if it is ever not ID. |
||
| 1992 | * @param bool $pull_to_drafts Whether to save to WordPress drafts when pulling from Salesforce. |
||
| 1993 | * @param bool $check_only Allows this method to only check for matching records, instead of making any data changes. |
||
| 1994 | * |
||
| 1995 | * @return array |
||
| 1996 | * data: |
||
| 1997 | * ID : 123, |
||
| 1998 | * success: 1 |
||
| 1999 | * "errors" : [ ], |
||
| 2000 | */ |
||
| 2001 | private function term_upsert( $key, $value, $methods, $params, $taxonomy, $id_field = 'ID', $pull_to_drafts = false, $check_only = false ) { |
||
| 2002 | if ( 'tag' === $taxonomy ) { |
||
| 2003 | $taxonomy = 'post_tag'; |
||
| 2004 | } |
||
| 2005 | $method = isset( $methods['method_match'] ) ? $methods['method_match'] : ''; |
||
| 2006 | if ( '' !== $method ) { |
||
| 2007 | // These methods should give us the term object if we are matching for one. |
||
| 2008 | // If we are trying to match to a meta field, the method is an object. |
||
| 2009 | if ( class_exists( $method ) ) { |
||
| 2010 | $args = array( |
||
| 2011 | 'taxonomy' => $taxonomy, |
||
| 2012 | 'meta_key' => $key, |
||
| 2013 | 'meta_value' => $value, |
||
| 2014 | ); |
||
| 2015 | $match_query = new $method( $args ); |
||
| 2016 | // the WP_Term_Query object has a get_terms method that we use. |
||
| 2017 | $terms = $match_query->get_terms(); |
||
| 2018 | if ( ! empty( $terms ) ) { |
||
| 2019 | $term = $terms[0]; |
||
| 2020 | } |
||
| 2021 | } else { |
||
| 2022 | $term = $method( $key, $value, $taxonomy ); // We need to put the taxonomy in there probably. |
||
| 2023 | } |
||
| 2024 | |||
| 2025 | if ( isset( $term ) && isset( $term->{$id_field} ) ) { |
||
| 2026 | // Term does exist after checking the matching value. we want its id. |
||
| 2027 | $term_id = $term->{$id_field}; |
||
| 2028 | |||
| 2029 | if ( true === $check_only ) { |
||
| 2030 | // We are just checking to see if there is a match. |
||
| 2031 | return $term_id; |
||
| 2032 | } |
||
| 2033 | |||
| 2034 | // On the prematch fields, we specify the method_update param. |
||
| 2035 | if ( isset( $methods['method_update'] ) ) { |
||
| 2036 | $method = $methods['method_update']; |
||
| 2037 | } else { |
||
| 2038 | $method = $methods['method_modify']; |
||
| 2039 | } |
||
| 2040 | $params[ $key ] = array( |
||
| 2041 | 'value' => $value, |
||
| 2042 | 'method_modify' => $method, |
||
| 2043 | 'method_read' => $methods['method_read'], |
||
| 2044 | ); |
||
| 2045 | } elseif ( false === $check_only ) { |
||
| 2046 | // Term does not exist after checking the matching value. Create it. |
||
| 2047 | // On the prematch fields, we specify the method_create param. |
||
| 2048 | if ( isset( $methods['method_create'] ) ) { |
||
| 2049 | $method = $methods['method_create']; |
||
| 2050 | } else { |
||
| 2051 | $method = $methods['method_modify']; |
||
| 2052 | } |
||
| 2053 | $params[ $key ] = array( |
||
| 2054 | 'value' => $value, |
||
| 2055 | 'method_modify' => $method, |
||
| 2056 | 'method_read' => $methods['method_read'], |
||
| 2057 | ); |
||
| 2058 | $result = $this->term_create( $params, $taxonomy, $id_field ); |
||
| 2059 | return $result; |
||
| 2060 | } else { |
||
| 2061 | // Check only is true but there's not a term yet. |
||
| 2062 | return null; |
||
| 2063 | } // End if() statement. |
||
| 2064 | } else { |
||
| 2065 | // There is no method by which to check the term. we can check other ways here. |
||
| 2066 | $params[ $key ] = array( |
||
| 2067 | 'value' => $value, |
||
| 2068 | 'method_modify' => $methods['method_modify'], |
||
| 2069 | 'method_read' => $methods['method_read'], |
||
| 2070 | ); |
||
| 2071 | |||
| 2072 | if ( isset( $params['name']['value'] ) ) { |
||
| 2073 | $term = $params['name']['value']; |
||
| 2074 | } |
||
| 2075 | |||
| 2076 | if ( isset( $params['parent']['value'] ) ) { |
||
| 2077 | $parent = $params['parent']['value']; |
||
| 2078 | } else { |
||
| 2079 | $parent = 0; |
||
| 2080 | } |
||
| 2081 | |||
| 2082 | // Returns an id if there is a result. Returns null if it does not exist. |
||
| 2083 | // wpcom_vip_term_exists is cached, and therefore preferred. |
||
| 2084 | if ( function_exists( 'wpcom_vip_term_exists' ) ) { |
||
| 2085 | $existing_id = wpcom_vip_term_exists( $term, $taxonomy, $parent ); |
||
| 2086 | } else { |
||
| 2087 | $existing_id = term_exists( $term, $taxonomy, $parent ); |
||
| 2088 | } |
||
| 2089 | |||
| 2090 | // Term does not exist after more checking. maybe we want to create it. |
||
| 2091 | if ( null === $existing_id && false === $check_only ) { |
||
| 2092 | $result = $this->term_create( $params, $taxonomy, $id_field ); |
||
| 2093 | return $result; |
||
| 2094 | } elseif ( true === $check_only ) { |
||
| 2095 | // We are just checking to see if there is a match. |
||
| 2096 | return $existing_id; |
||
| 2097 | } else { |
||
| 2098 | // Term does exist based on criteria, and we aren't doing a check only. we want to update the wp term here. |
||
| 2099 | $term_id = $existing_id; |
||
| 2100 | } |
||
| 2101 | } // End if() statement. |
||
| 2102 | |||
| 2103 | if ( isset( $term_id ) ) { |
||
| 2104 | foreach ( $params as $key => $value ) { |
||
| 2105 | $params[ $key ]['method_modify'] = $methods['method_update']; |
||
| 2106 | } |
||
| 2107 | $result = $this->term_update( $term_id, $params, $taxonomy, $id_field ); |
||
| 2108 | return $result; |
||
| 2109 | } |
||
| 2110 | // Create log entry for lack of a term id. |
||
| 2111 | $status = 'error'; |
||
| 2112 | $title = sprintf( |
||
| 2113 | // translators: placeholders are: 1) the log status. |
||
| 2114 | esc_html__( '%1$s: Terms: Tried to run term_upsert, and ended up without a term id', 'object-sync-for-salesforce' ), |
||
| 2115 | ucfirst( esc_attr( $status ) ) |
||
| 2116 | ); |
||
| 2117 | $this->logging->setup( |
||
| 2118 | $title, |
||
| 2119 | '', |
||
| 2120 | 0, |
||
| 2121 | 0, |
||
| 2122 | $status |
||
| 2123 | ); |
||
| 2124 | |||
| 2125 | } |
||
| 2126 | |||
| 2127 | /** |
||
| 2128 | * Update a WordPress term. |
||
| 2129 | * |
||
| 2130 | * @param int $term_id The ID for the term to be updated. This value needs to be in the array that is sent to wp_update_term. |
||
| 2131 | * @param array $params Array of term data params. |
||
| 2132 | * @param string $taxonomy The taxonomy to which to add the term. this is required. |
||
| 2133 | * @param string $id_field Optional string of what the ID field is, if it is ever not ID. |
||
| 2134 | * |
||
| 2135 | * @return array |
||
| 2136 | * data: |
||
| 2137 | * success: 1 |
||
| 2138 | * "errors" : [ ], |
||
| 2139 | */ |
||
| 2140 | private function term_update( $term_id, $params, $taxonomy, $id_field = 'ID' ) { |
||
| 2141 | if ( 'tag' === $taxonomy ) { |
||
| 2142 | $taxonomy = 'post_tag'; |
||
| 2143 | } |
||
| 2144 | $args = array(); |
||
| 2145 | foreach ( $params as $key => $value ) { |
||
| 2146 | if ( 'wp_update_term' === $value['method_modify'] ) { |
||
| 2147 | $args[ $key ] = $value['value']; |
||
| 2148 | unset( $params[ $key ] ); |
||
| 2149 | } |
||
| 2150 | } |
||
| 2151 | $term = wp_update_term( $term_id, $taxonomy, $args ); |
||
| 2152 | |||
| 2153 | if ( is_wp_error( $term ) ) { |
||
| 2154 | $success = false; |
||
| 2155 | $errors = $term; |
||
| 2156 | } else { |
||
| 2157 | $term_id = $term[ "$id_field" ]; |
||
| 2158 | $meta_result = $this->update_wp_meta( $params, $term_id, 'term' ); |
||
| 2159 | $success = $meta_result['success']; |
||
| 2160 | $errors = $meta_result['errors']; |
||
| 2161 | // Developers can use this hook to set any other term data. |
||
| 2162 | do_action( $this->option_prefix . 'set_more_term_data', $term_id, $params, 'update' ); |
||
| 2163 | } // End if() statement. |
||
| 2164 | |||
| 2165 | if ( is_wp_error( $term ) ) { |
||
| 2166 | $success = false; |
||
| 2167 | $errors = $term; |
||
| 2168 | } else { |
||
| 2169 | $success = true; |
||
| 2170 | $errors = array(); |
||
| 2171 | } |
||
| 2172 | |||
| 2173 | $result = array( |
||
| 2174 | 'data' => array( |
||
| 2175 | $id_field => $term_id, |
||
| 2176 | 'success' => $success, |
||
| 2177 | ), |
||
| 2178 | 'errors' => $errors, |
||
| 2179 | ); |
||
| 2180 | |||
| 2181 | return $result; |
||
| 2182 | |||
| 2183 | } |
||
| 2184 | |||
| 2185 | /** |
||
| 2186 | * Delete a WordPress term. |
||
| 2187 | * |
||
| 2188 | * @param int $term_id The ID for the term to be updated. This value needs to be in the array that is sent to wp_update_term. |
||
| 2189 | * @param string $taxonomy The taxonomy from which to delete the term. this is required. |
||
| 2190 | * |
||
| 2191 | * @return bool True if successful, false if failed. |
||
| 2192 | */ |
||
| 2193 | private function term_delete( $term_id, $taxonomy ) { |
||
| 2199 | } |
||
| 2200 | |||
| 2201 | /** |
||
| 2202 | * Create a new WordPress comment. |
||
| 2203 | * |
||
| 2204 | * @param array $params Array of comment data params. |
||
| 2205 | * @param string $id_field Optional string of what the ID field is, if it is ever not comment_ID. |
||
| 2206 | * |
||
| 2207 | * @return array |
||
| 2208 | * data: |
||
| 2209 | * ID : 123, |
||
| 2210 | * success: 1 |
||
| 2211 | * "errors" : [ ], |
||
| 2212 | */ |
||
| 2213 | private function comment_create( $params, $id_field = 'comment_ID' ) { |
||
| 2214 | // Load all params with a method_modify of the object structure's content_method into $content. |
||
| 2215 | $content = array(); |
||
| 2216 | $structure = $this->get_wordpress_table_structure( 'comment' ); |
||
| 2217 | foreach ( $params as $key => $value ) { |
||
| 2218 | if ( in_array( $value['method_modify'], $structure['content_methods'], true ) ) { |
||
| 2219 | $content[ $key ] = $value['value']; |
||
| 2220 | unset( $params[ $key ] ); |
||
| 2221 | } |
||
| 2222 | } |
||
| 2223 | |||
| 2224 | // Fields that are required for comments, even if they are empty values. |
||
| 2225 | if ( ! isset( $content['comment_author'] ) ) { |
||
| 2226 | $content['comment_author'] = ''; |
||
| 2227 | } |
||
| 2228 | if ( ! isset( $content['comment_author_IP'] ) ) { |
||
| 2229 | $content['comment_author_IP'] = ''; |
||
| 2230 | } |
||
| 2231 | if ( ! isset( $content['comment_author_email'] ) ) { |
||
| 2232 | $content['comment_author_email'] = ''; |
||
| 2233 | } |
||
| 2234 | if ( ! isset( $content['comment_author_url'] ) ) { |
||
| 2235 | $content['comment_author_url'] = ''; |
||
| 2236 | } |
||
| 2237 | if ( ! isset( $content['comment_type'] ) ) { |
||
| 2238 | $content['comment_type'] = ''; |
||
| 2239 | } |
||
| 2240 | |||
| 2241 | $comment_id = wp_new_comment( $content ); |
||
| 2242 | |||
| 2243 | if ( is_wp_error( $comment_id ) ) { |
||
| 2244 | $success = false; |
||
| 2245 | $errors = $comment_id; |
||
| 2246 | } else { |
||
| 2247 | $meta_result = $this->create_wp_meta( $params, $comment_id, 'comment' ); |
||
| 2248 | $success = $meta_result['success']; |
||
| 2249 | $errors = $meta_result['errors']; |
||
| 2250 | // Developers can use this hook to set any other comment data. |
||
| 2251 | do_action( $this->option_prefix . 'set_more_comment_data', $comment_id, $params, 'create' ); |
||
| 2252 | } // End if() statement. |
||
| 2253 | |||
| 2254 | if ( is_wp_error( $comment_id ) ) { |
||
| 2255 | $success = false; |
||
| 2256 | $errors = $comment_id; |
||
| 2257 | } else { |
||
| 2258 | $success = true; |
||
| 2259 | $errors = array(); |
||
| 2260 | } |
||
| 2261 | |||
| 2262 | $result = array( |
||
| 2263 | 'data' => array( |
||
| 2264 | $id_field => $comment_id, |
||
| 2265 | 'success' => $success, |
||
| 2266 | ), |
||
| 2267 | 'errors' => $errors, |
||
| 2268 | ); |
||
| 2269 | |||
| 2270 | return $result; |
||
| 2271 | |||
| 2272 | } |
||
| 2273 | |||
| 2274 | /** |
||
| 2275 | * Create a new WordPress comment or update it if a match is found. |
||
| 2276 | * |
||
| 2277 | * @param string $key What key we are looking at for possible matches. |
||
| 2278 | * @param string $value What value we are looking at for possible matches. |
||
| 2279 | * @param array $methods What WordPress methods do we use to get the data, if there are any. otherwise, maybe will have to do a wpdb query. |
||
| 2280 | * @param array $params Array of comment data params. |
||
| 2281 | * @param string $id_field Optional string of what the ID field is, if it is ever not comment_ID. |
||
| 2282 | * @param bool $pull_to_drafts Whether to save to WordPress drafts when pulling from Salesforce. |
||
| 2283 | * @param bool $check_only Allows this method to only check for matching records, instead of making any data changes. |
||
| 2284 | * |
||
| 2285 | * @return array |
||
| 2286 | * data: |
||
| 2287 | * ID : 123, |
||
| 2288 | * success: 1 |
||
| 2289 | * "errors" : [ ], |
||
| 2290 | */ |
||
| 2291 | private function comment_upsert( $key, $value, $methods, $params, $id_field = 'comment_ID', $pull_to_drafts = false, $check_only = false ) { |
||
| 2292 | $method = isset( $methods['method_match'] ) ? $methods['method_match'] : ''; |
||
| 2293 | if ( 'get_comment' === $method ) { |
||
| 2294 | $method = 'get_comments'; |
||
| 2295 | } |
||
| 2296 | if ( '' !== $method ) { |
||
| 2297 | |||
| 2298 | // These methods should give us the comment object if we are matching for one. |
||
| 2299 | // If we are trying to match to a meta field, the method is an object. |
||
| 2300 | if ( class_exists( $method ) ) { |
||
| 2301 | $args = array( |
||
| 2302 | 'meta_query' => array( |
||
| 2303 | array( |
||
| 2304 | 'key' => $key, |
||
| 2305 | 'value' => $value, |
||
| 2306 | ), |
||
| 2307 | ), |
||
| 2308 | ); |
||
| 2309 | $match_query = new $method( $args ); |
||
| 2310 | // the WP_Comment_Query object has a get_comments method that we use. |
||
| 2311 | $comments = $match_query->get_comments(); |
||
| 2312 | if ( ! empty( $comments ) ) { |
||
| 2313 | $comment = $users[0]; |
||
| 2314 | } |
||
| 2315 | } else { |
||
| 2316 | $match = array(); |
||
| 2317 | if ( 'comment_author' === $key ) { |
||
| 2318 | $match['author__in'] = array( $value ); |
||
| 2319 | } else { |
||
| 2320 | $key = str_replace( 'comment_', '', $key ); |
||
| 2321 | $match[ $key ] = $value; |
||
| 2322 | } |
||
| 2323 | $comments = $method( $match ); |
||
| 2324 | } |
||
| 2325 | |||
| 2326 | if ( 1 === count( $comments ) && isset( $comments ) && isset( $comments[0]->{$id_field} ) ) { |
||
| 2327 | $comment = $comments[0]; |
||
| 2328 | // Comment does exist after checking the matching value. we want its id. |
||
| 2329 | $comment_id = $comment->{$id_field}; |
||
| 2330 | |||
| 2331 | if ( true === $check_only ) { |
||
| 2332 | // We are just checking to see if there is a match. |
||
| 2333 | return $comment_id; |
||
| 2334 | } |
||
| 2335 | |||
| 2336 | // On the prematch fields, we specify the method_update param. |
||
| 2337 | if ( isset( $methods['method_update'] ) ) { |
||
| 2338 | $method = $methods['method_update']; |
||
| 2339 | } else { |
||
| 2340 | $method = $methods['method_modify']; |
||
| 2341 | } |
||
| 2342 | $params[ $key ] = array( |
||
| 2343 | 'value' => $value, |
||
| 2344 | 'method_modify' => $method, |
||
| 2345 | 'method_read' => $methods['method_read'], |
||
| 2346 | ); |
||
| 2347 | } elseif ( count( $comments ) > 1 ) { |
||
| 2348 | $status = 'error'; |
||
| 2349 | // Create log entry for multiple matches. |
||
| 2350 | $this->logging->setup( |
||
| 2351 | sprintf( |
||
| 2352 | // translators: %1$s is the log status. %2$s is a number. %3$s is a key. %4$s is the value of that key. %5$s is a var_export'd array of comments. |
||
| 2353 | esc_html__( '%1$s: Comments: there are %2$s comment matches for the Salesforce key %3$s with the value of %4$s. Here they are: %5$s', 'object-sync-for-salesforce' ), |
||
| 2354 | ucfirst( esc_attr( $status ) ), |
||
| 2355 | absint( count( $comments ) ), |
||
| 2356 | esc_html( $key ), |
||
| 2357 | esc_html( $value ), |
||
| 2358 | esc_html( var_export( $comments ) ) // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export |
||
| 2359 | ), |
||
| 2360 | '', |
||
| 2361 | 0, |
||
| 2362 | 0, |
||
| 2363 | $status |
||
| 2364 | ); |
||
| 2365 | } elseif ( false === $check_only ) { |
||
| 2366 | // Comment does not exist after checking the matching value. Create it. |
||
| 2367 | // On the prematch fields, we specify the method_create param. |
||
| 2368 | if ( isset( $methods['method_create'] ) ) { |
||
| 2369 | $method = $methods['method_create']; |
||
| 2370 | } else { |
||
| 2371 | $method = $methods['method_modify']; |
||
| 2372 | } |
||
| 2373 | $params[ $key ] = array( |
||
| 2374 | 'value' => $value, |
||
| 2375 | 'method_modify' => $method, |
||
| 2376 | 'method_read' => $methods['method_read'], |
||
| 2377 | ); |
||
| 2378 | $result = $this->comment_create( $params, $id_field ); |
||
| 2379 | return $result; |
||
| 2380 | } else { |
||
| 2381 | // Check only is true but there's not a comment yet. |
||
| 2382 | return null; |
||
| 2383 | } // End if() statement. |
||
| 2384 | } else { |
||
| 2385 | // There is no method by which to check the comment. We can check other ways here. |
||
| 2386 | $params[ $key ] = array( |
||
| 2387 | 'value' => $value, |
||
| 2388 | 'method_modify' => $methods['method_modify'], |
||
| 2389 | 'method_read' => $methods['method_read'], |
||
| 2390 | ); |
||
| 2391 | |||
| 2392 | if ( isset( $params['comment_author']['value'] ) ) { |
||
| 2393 | $comment_author = $params['comment_author']['value']; |
||
| 2394 | } |
||
| 2395 | |||
| 2396 | if ( isset( $params['comment_date']['value'] ) ) { |
||
| 2397 | $comment_date = $params['comment_date']['value']; |
||
| 2398 | } |
||
| 2399 | |||
| 2400 | if ( isset( $params['timezone']['value'] ) ) { |
||
| 2401 | $timezone = $params['timezone']['value']; |
||
| 2402 | } else { |
||
| 2403 | $timezone = 'blog'; |
||
| 2404 | } |
||
| 2405 | |||
| 2406 | $existing_id = comment_exists( $comment_author, $comment_date, $timezone ); // Returns an id if there is a result. Uses $wpdb->get_var, so it returns null if there is no value. |
||
| 2407 | |||
| 2408 | // Comment does not exist after more checking. We want to create it. |
||
| 2409 | if ( null === $existing_id && false === $check_only ) { |
||
| 2410 | $result = $this->comment_create( $params, $id_field ); |
||
| 2411 | return $result; |
||
| 2412 | } elseif ( true === $check_only ) { |
||
| 2413 | // We are just checking to see if there is a match. |
||
| 2414 | return $existing_id; |
||
| 2415 | } else { |
||
| 2416 | // Comment does exist based on username, and we aren't doing a check only. we want to update the wp user here. |
||
| 2417 | $comment_id = $existing_id; |
||
| 2418 | } |
||
| 2419 | } // End if() that sets up the parameters in the $params array. |
||
| 2420 | |||
| 2421 | if ( isset( $comment_id ) ) { |
||
| 2422 | foreach ( $params as $key => $value ) { |
||
| 2423 | $params[ $key ]['method_modify'] = $methods['method_update']; |
||
| 2424 | } |
||
| 2425 | $result = $this->comment_update( $comment_id, $params, $id_field ); |
||
| 2426 | return $result; |
||
| 2427 | } |
||
| 2428 | |||
| 2429 | // Create log entry for lack of a comment id. |
||
| 2430 | $status = 'error'; |
||
| 2431 | $title = sprintf( |
||
| 2432 | // translators: placeholders are: 1) the log status. |
||
| 2433 | esc_html__( '%1$s: Comments: Tried to run comment_upsert, and ended up without a comment id', 'object-sync-for-salesforce' ), |
||
| 2434 | ucfirst( esc_attr( $status ) ) |
||
| 2435 | ); |
||
| 2436 | $this->logging->setup( |
||
| 2437 | $title, |
||
| 2438 | '', |
||
| 2439 | 0, |
||
| 2440 | 0, |
||
| 2441 | $status |
||
| 2442 | ); |
||
| 2443 | |||
| 2444 | } |
||
| 2445 | |||
| 2446 | /** |
||
| 2447 | * Update a WordPress comment. |
||
| 2448 | * |
||
| 2449 | * @param int $comment_id The ID for the comment to be updated. This value needs to be in the array that is sent to wp_update_comment. |
||
| 2450 | * @param array $params Array of comment data params. |
||
| 2451 | * @param string $id_field Optional string of what the ID field is, if it is ever not ID. |
||
| 2452 | * |
||
| 2453 | * @return array |
||
| 2454 | * data: |
||
| 2455 | * success: 1 |
||
| 2456 | * "errors" : [ ], |
||
| 2457 | */ |
||
| 2458 | private function comment_update( $comment_id, $params, $id_field = 'comment_ID' ) { |
||
| 2459 | $content = array(); |
||
| 2460 | $content[ $id_field ] = $comment_id; |
||
| 2461 | foreach ( $params as $key => $value ) { |
||
| 2462 | if ( 'wp_update_comment' === $value['method_modify'] ) { |
||
| 2463 | $content[ $key ] = $value['value']; |
||
| 2464 | unset( $params[ $key ] ); |
||
| 2465 | } |
||
| 2466 | } |
||
| 2467 | |||
| 2468 | $updated = wp_update_comment( $content ); |
||
| 2469 | |||
| 2470 | if ( 0 === $updated ) { |
||
| 2471 | $success = false; |
||
| 2472 | $errors = $updated; |
||
| 2473 | } else { |
||
| 2474 | $meta_result = $this->update_wp_meta( $params, $comment_id, 'comment' ); |
||
| 2475 | $success = $meta_result['success']; |
||
| 2476 | $errors = $meta_result['errors']; |
||
| 2477 | // Developers can use this hook to set any other comment data. |
||
| 2478 | do_action( $this->option_prefix . 'set_more_comment_data', $comment_id, $params, 'update' ); |
||
| 2479 | } // End if() statement. |
||
| 2480 | |||
| 2481 | if ( is_wp_error( $updated ) ) { |
||
| 2482 | $success = false; |
||
| 2483 | $errors = $updated; |
||
| 2484 | } else { |
||
| 2485 | $success = true; |
||
| 2486 | $errors = array(); |
||
| 2487 | } |
||
| 2488 | |||
| 2489 | $result = array( |
||
| 2490 | 'data' => array( |
||
| 2491 | $id_field => $comment_id, |
||
| 2492 | 'success' => $success, |
||
| 2493 | ), |
||
| 2494 | 'errors' => $errors, |
||
| 2495 | ); |
||
| 2496 | |||
| 2497 | return $result; |
||
| 2498 | |||
| 2499 | } |
||
| 2500 | |||
| 2501 | /** |
||
| 2502 | * Delete a WordPress comment. |
||
| 2503 | * |
||
| 2504 | * @param int $id Comment ID. |
||
| 2505 | * @param bool $force_delete If we should bypass the trash. We don't change this from FALSE anywhere in this plugin. |
||
| 2506 | * |
||
| 2507 | * @return boolean true if successful, false if failed. |
||
| 2508 | */ |
||
| 2509 | private function comment_delete( $id, $force_delete = false ) { |
||
| 2510 | $result = wp_delete_comment( $id, $force_delete ); |
||
| 2511 | return $result; |
||
| 2512 | } |
||
| 2513 | |||
| 2514 | /** |
||
| 2515 | * Standard method for creating meta values |
||
| 2516 | * This works for users, posts, terms, and comments. It does not work for attachments. |
||
| 2517 | * |
||
| 2518 | * @param array $params the values to be saved. |
||
| 2519 | * @param int|wp_error $parent_object_id the WordPress object ID that this metadata is associated with. It shouldn't ever end up here as an error, but it's worth documenting. |
||
| 2520 | * @param string $parent_object_type the WordPress object type. |
||
| 2521 | * |
||
| 2522 | * @return array $meta_result contains the success flag and the array of errors |
||
| 2523 | */ |
||
| 2524 | private function create_wp_meta( $params, $parent_object_id, $parent_object_type ) { |
||
| 2525 | $success = true; |
||
| 2526 | $errors = array(); |
||
| 2527 | if ( ! is_wp_error( $parent_object_id ) && is_array( $params ) && ! empty( $params ) ) { |
||
| 2528 | foreach ( $params as $key => $value ) { |
||
| 2529 | |||
| 2530 | // if the value is empty, skip it. |
||
| 2531 | if ( '' === $value['value'] ) { |
||
| 2532 | continue; |
||
| 2533 | } |
||
| 2534 | |||
| 2535 | $modify = $value['method_modify']; |
||
| 2536 | // Todo: we could provide a way for passing the values in a custom order here. |
||
| 2537 | $meta_id = $modify( $parent_object_id, $key, $value['value'] ); |
||
| 2538 | if ( false === $meta_id ) { |
||
| 2539 | $success = false; |
||
| 2540 | $errors[] = array( |
||
| 2541 | 'message' => sprintf( |
||
| 2542 | // translators: 1) is the WordPress object type, 2) is the method that should be used to save the value. |
||
| 2543 | esc_html__( 'Tried to add %1$s meta with method %2$s.', 'object-sync-for-salesforce' ), |
||
| 2544 | esc_attr( $parent_object_type ), |
||
| 2545 | esc_html( $method ) |
||
| 2546 | ), |
||
| 2547 | 'key' => $key, |
||
| 2548 | 'value' => $value, |
||
| 2549 | ); |
||
| 2550 | } |
||
| 2551 | } // End foreach. |
||
| 2552 | } |
||
| 2553 | $meta_result = array( |
||
| 2554 | 'success' => $success, |
||
| 2555 | 'errors' => $errors, |
||
| 2556 | ); |
||
| 2557 | return $meta_result; |
||
| 2558 | } |
||
| 2559 | |||
| 2560 | /** |
||
| 2561 | * Standard method for updating meta values |
||
| 2562 | * This works for users, posts, terms, and comments. It does not work for attachments. |
||
| 2563 | * |
||
| 2564 | * @param array $params the values to be saved. |
||
| 2565 | * @param int|wp_error $parent_object_id the WordPress object ID that this metadata is associated with. It shouldn't ever end up here as an error, but it's worth documenting. |
||
| 2566 | * @param string $parent_object_type the WordPress object type. |
||
| 2567 | * |
||
| 2568 | * @return array $meta_result contains the success flag, the changed flag, and the array of errors |
||
| 2569 | */ |
||
| 2570 | private function update_wp_meta( $params, $parent_object_id, $parent_object_type ) { |
||
| 2626 | } |
||
| 2627 | |||
| 2628 | /** |
||
| 2629 | * Generate date formats for The Event Calendar plugin |
||
| 2630 | * |
||
| 2631 | * @param string $date the string value of the date from Salesforce. |
||
| 2632 | * @param string $type this should be start or end. |
||
| 2633 | * @param array $content the other mapped params. |
||
| 2634 | * |
||
| 2635 | * @return array $content |
||
| 2636 | */ |
||
| 2637 | private function append_tec_event_dates( $date, $type, $content ) { |
||
| 2638 | if ( ( 'start' === $type || 'end' === $type ) && class_exists( 'Tribe__Date_Utils' ) ) { |
||
| 2639 | $dates = array(); |
||
| 2640 | $date_type = ucfirst( $type ); |
||
| 2641 | $timestamp = strtotime( $date ); |
||
| 2642 | $dates[ 'Event' . $date_type . 'Date' ] = gmdate( Tribe__Date_Utils::DBDATEFORMAT, $timestamp ); |
||
| 2643 | $dates[ 'Event' . $date_type . 'Hour' ] = gmdate( Tribe__Date_Utils::HOURFORMAT, $timestamp ); |
||
| 2644 | $dates[ 'Event' . $date_type . 'Minute' ] = gmdate( Tribe__Date_Utils::MINUTEFORMAT, $timestamp ); |
||
| 2645 | $dates[ 'Event' . $date_type . 'Meridian' ] = gmdate( Tribe__Date_Utils::MERIDIANFORMAT, $timestamp ); |
||
| 2646 | $content = $content + $dates; |
||
| 2647 | } |
||
| 2648 | return $content; |
||
| 2649 | } |
||
| 2650 | |||
| 2651 | /** |
||
| 2652 | * Generate an edit link for the WordPress record. |
||
| 2653 | * |
||
| 2654 | * @param string $object_type the type of WordPress object. |
||
| 2655 | * @param int $wordpress_id the ID of the WordPress object. |
||
| 2656 | * |
||
| 2657 | * @return string $edit_link |
||
| 2658 | */ |
||
| 2659 | public function object_edit_link( $object_type, $wordpress_id ) { |
||
| 2660 | $edit_link = ''; |
||
| 2661 | if ( 'user' === $object_type ) { |
||
| 2662 | $edit_link = get_edit_user_link( $wordpress_id ); |
||
| 2663 | } elseif ( 'post' === $object_type || 'page' === $object_type || 'attachment' === $object_type ) { |
||
| 2664 | $edit_link = get_edit_post_link( $wordpress_id ); |
||
| 2665 | } elseif ( 'category' === $object_type || 'tag' === $object_type || 'post_tag' === $object_type ) { |
||
| 2666 | $edit_link = get_edit_term_link( $wordpress_id ); |
||
| 2667 | } elseif ( 'comment' === $object_type ) { |
||
| 2668 | $edit_link = get_edit_comment_link( $wordpress_id ); |
||
| 2669 | } else { // This is for custom post types. |
||
| 2670 | $edit_link = get_edit_post_link( $wordpress_id ); |
||
| 2671 | } // End if() statement. |
||
| 2672 | return $edit_link; |
||
| 2673 | } |
||
| 2674 | |||
| 2675 | /** |
||
| 2676 | * Generate a delete link for the WordPress record. |
||
| 2677 | * |
||
| 2678 | * @param string $object_type the type of WordPress object. |
||
| 2679 | * @param int $wordpress_id the ID of the WordPress object. |
||
| 2680 | * |
||
| 2681 | * @return string $delete_link |
||
| 2682 | */ |
||
| 2683 | public function object_delete_link( $object_type, $wordpress_id ) { |
||
| 2691 | } |
||
| 2692 | |||
| 2693 | } |
||
| 2694 |