Code Duplication    Length = 65-66 lines in 3 locations

json-endpoints/class.wpcom-json-api-update-post-v1-1-endpoint.php 1 location

@@ 437-502 (lines=66) @@
434
		// We ask the user/dev to pass Publicize services he/she wants activated for the post, but Publicize expects us
435
		// to instead flag the ones we don't want to be skipped. proceed with said logic.
436
		// any posts coming from Path (client ID 25952) should also not publicize
437
		if ( $publicize === false || ( isset( $this->api->token_details['client_id'] ) && 25952 == $this->api->token_details['client_id'] ) ) {
438
			// No publicize at all, skip all by ID
439
			foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) {
440
				delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name );
441
				$service_connections   = $GLOBALS['publicize_ui']->publicize->get_connections( $name );
442
				if ( ! $service_connections ) {
443
					continue;
444
				}
445
				foreach ( $service_connections as $service_connection ) {
446
					update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
447
				}
448
			}
449
		} else if ( is_array( $publicize ) && ( count ( $publicize ) > 0 ) ) {
450
			foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) {
451
				/*
452
				 * We support both indexed and associative arrays:
453
				 * * indexed are to pass entire services
454
				 * * associative are to pass specific connections per service
455
				 *
456
				 * We do support mixed arrays: mixed integer and string keys (see 3rd example below).
457
				 *
458
				 * EG: array( 'twitter', 'facebook') will only publicize to those, ignoring the other available services
459
				 * 		Form data: publicize[]=twitter&publicize[]=facebook
460
				 * EG: array( 'twitter' => '(int) $pub_conn_id_0, (int) $pub_conn_id_3', 'facebook' => (int) $pub_conn_id_7 ) will publicize to two Twitter accounts, and one Facebook connection, of potentially many.
461
				 * 		Form data: publicize[twitter]=$pub_conn_id_0,$pub_conn_id_3&publicize[facebook]=$pub_conn_id_7
462
				 * EG: array( 'twitter', 'facebook' => '(int) $pub_conn_id_0, (int) $pub_conn_id_3' ) will publicize to all available Twitter accounts, but only 2 of potentially many Facebook connections
463
				 * 		Form data: publicize[]=twitter&publicize[facebook]=$pub_conn_id_0,$pub_conn_id_3
464
				 */
465
466
				// Delete any stale SKIP value for the service by name. We'll add it back by ID.
467
				delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name );
468
469
				// Get the user's connections
470
				$service_connections = $GLOBALS['publicize_ui']->publicize->get_connections( $name );
471
472
				// if the user doesn't have any connections for this service, move on
473
				if ( ! $service_connections ) {
474
					continue;
475
				}
476
477
				if ( !in_array( $name, $publicize ) && !array_key_exists( $name, $publicize ) ) {
478
					// Skip the whole service by adding each connection ID
479
					foreach ( $service_connections as $service_connection ) {
480
						update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
481
					}
482
				} else if ( !empty( $publicize[ $name ] ) ) {
483
					// Seems we're being asked to only push to [a] specific connection[s].
484
					// Explode the list on commas, which will also support a single passed ID
485
					$requested_connections = explode( ',', ( preg_replace( '/[\s]*/', '', $publicize[ $name ] ) ) );
486
487
					// Flag the connections we can't match with the requested list to be skipped.
488
					foreach ( $service_connections as $service_connection ) {
489
						if ( !in_array( $service_connection->meta['connection_data']->id, $requested_connections ) ) {
490
							update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
491
						} else {
492
							delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
493
						}
494
					}
495
				} else {
496
					// delete all SKIP values; it's okay to publish to all connected IDs for this service
497
					foreach ( $service_connections as $service_connection ) {
498
						delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
499
					}
500
				}
501
			}
502
		}
503
504
		if ( ! is_null( $publicize_custom_message ) ) {
505
			if ( empty( $publicize_custom_message ) ) {

json-endpoints/class.wpcom-json-api-update-post-endpoint.php 1 location

@@ 424-488 (lines=65) @@
421
		// We ask the user/dev to pass Publicize services he/she wants activated for the post, but Publicize expects us
422
		// to instead flag the ones we don't want to be skipped. proceed with said logic.
423
		// any posts coming from Path (client ID 25952) should also not publicize
424
		if ( $publicize === false || ( isset( $this->api->token_details['client_id'] ) && 25952 == $this->api->token_details['client_id'] ) ) {
425
			// No publicize at all, skip all by ID
426
			foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) {
427
				delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name );
428
				$service_connections   = $GLOBALS['publicize_ui']->publicize->get_connections( $name );
429
				if ( ! $service_connections ) {
430
					continue;
431
				}
432
				foreach ( $service_connections as $service_connection ) {
433
					update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
434
				}
435
			}
436
		} else if ( is_array( $publicize ) && ( count ( $publicize ) > 0 ) ) {
437
			foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) {
438
				/*
439
				 * We support both indexed and associative arrays:
440
				 * * indexed are to pass entire services
441
				 * * associative are to pass specific connections per service
442
				 *
443
				 * We do support mixed arrays: mixed integer and string keys (see 3rd example below).
444
				 *
445
				 * EG: array( 'twitter', 'facebook') will only publicize to those, ignoring the other available services
446
				 * 		Form data: publicize[]=twitter&publicize[]=facebook
447
				 * EG: array( 'twitter' => '(int) $pub_conn_id_0, (int) $pub_conn_id_3', 'facebook' => (int) $pub_conn_id_7 ) will publicize to two Twitter accounts, and one Facebook connection, of potentially many.
448
				 * 		Form data: publicize[twitter]=$pub_conn_id_0,$pub_conn_id_3&publicize[facebook]=$pub_conn_id_7
449
				 * EG: array( 'twitter', 'facebook' => '(int) $pub_conn_id_0, (int) $pub_conn_id_3' ) will publicize to all available Twitter accounts, but only 2 of potentially many Facebook connections
450
				 * 		Form data: publicize[]=twitter&publicize[facebook]=$pub_conn_id_0,$pub_conn_id_3
451
				 */
452
453
				// Delete any stale SKIP value for the service by name. We'll add it back by ID.
454
				delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name );
455
456
				// Get the user's connections
457
				$service_connections = $GLOBALS['publicize_ui']->publicize->get_connections( $name );
458
459
				// if the user doesn't have any connections for this service, move on
460
				if ( ! $service_connections ) {
461
					continue;
462
				}
463
464
				if ( !in_array( $name, $publicize ) && !array_key_exists( $name, $publicize ) ) {
465
					// Skip the whole service by adding each connection ID
466
					foreach ( $service_connections as $service_connection ) {
467
						update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
468
					}
469
				} else if ( !empty( $publicize[ $name ] ) ) {
470
					// Seems we're being asked to only push to [a] specific connection[s].
471
					// Explode the list on commas, which will also support a single passed ID
472
					$requested_connections = explode( ',', ( preg_replace( '/[\s]*/', '', $publicize[ $name ] ) ) );
473
					// Flag the connections we can't match with the requested list to be skipped.
474
					foreach ( $service_connections as $service_connection ) {
475
						if ( !in_array( $service_connection->meta['connection_data']->id, $requested_connections ) ) {
476
							update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
477
						} else {
478
							delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
479
						}
480
					}
481
				} else {
482
					// delete all SKIP values; it's okay to publish to all connected IDs for this service
483
					foreach ( $service_connections as $service_connection ) {
484
						delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
485
					}
486
				}
487
			}
488
		}
489
490
		if ( ! is_null( $publicize_custom_message ) ) {
491
			if ( empty( $publicize_custom_message ) ) {

json-endpoints/class.wpcom-json-api-update-post-v1-2-endpoint.php 1 location

@@ 437-502 (lines=66) @@
434
		// We ask the user/dev to pass Publicize services he/she wants activated for the post, but Publicize expects us
435
		// to instead flag the ones we don't want to be skipped. proceed with said logic.
436
		// any posts coming from Path (client ID 25952) should also not publicize
437
		if ( $publicize === false || ( isset( $this->api->token_details['client_id'] ) && 25952 == $this->api->token_details['client_id'] ) ) {
438
			// No publicize at all, skip all by ID
439
			foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) {
440
				delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name );
441
				$service_connections   = $GLOBALS['publicize_ui']->publicize->get_connections( $name );
442
				if ( ! $service_connections ) {
443
					continue;
444
				}
445
				foreach ( $service_connections as $service_connection ) {
446
					update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
447
				}
448
			}
449
		} else if ( is_array( $publicize ) && ( count ( $publicize ) > 0 ) ) {
450
			foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) {
451
				/*
452
				 * We support both indexed and associative arrays:
453
				 * * indexed are to pass entire services
454
				 * * associative are to pass specific connections per service
455
				 *
456
				 * We do support mixed arrays: mixed integer and string keys (see 3rd example below).
457
				 *
458
				 * EG: array( 'twitter', 'facebook') will only publicize to those, ignoring the other available services
459
				 * 		Form data: publicize[]=twitter&publicize[]=facebook
460
				 * EG: array( 'twitter' => '(int) $pub_conn_id_0, (int) $pub_conn_id_3', 'facebook' => (int) $pub_conn_id_7 ) will publicize to two Twitter accounts, and one Facebook connection, of potentially many.
461
				 * 		Form data: publicize[twitter]=$pub_conn_id_0,$pub_conn_id_3&publicize[facebook]=$pub_conn_id_7
462
				 * EG: array( 'twitter', 'facebook' => '(int) $pub_conn_id_0, (int) $pub_conn_id_3' ) will publicize to all available Twitter accounts, but only 2 of potentially many Facebook connections
463
				 * 		Form data: publicize[]=twitter&publicize[facebook]=$pub_conn_id_0,$pub_conn_id_3
464
				 */
465
466
				// Delete any stale SKIP value for the service by name. We'll add it back by ID.
467
				delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name );
468
469
				// Get the user's connections
470
				$service_connections = $GLOBALS['publicize_ui']->publicize->get_connections( $name );
471
472
				// if the user doesn't have any connections for this service, move on
473
				if ( ! $service_connections ) {
474
					continue;
475
				}
476
477
				if ( !in_array( $name, $publicize ) && !array_key_exists( $name, $publicize ) ) {
478
					// Skip the whole service by adding each connection ID
479
					foreach ( $service_connections as $service_connection ) {
480
						update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
481
					}
482
				} else if ( !empty( $publicize[ $name ] ) ) {
483
					// Seems we're being asked to only push to [a] specific connection[s].
484
					// Explode the list on commas, which will also support a single passed ID
485
					$requested_connections = explode( ',', ( preg_replace( '/[\s]*/', '', $publicize[ $name ] ) ) );
486
487
					// Flag the connections we can't match with the requested list to be skipped.
488
					foreach ( $service_connections as $service_connection ) {
489
						if ( !in_array( $service_connection->meta['connection_data']->id, $requested_connections ) ) {
490
							update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
491
						} else {
492
							delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
493
						}
494
					}
495
				} else {
496
					// delete all SKIP values; it's okay to publish to all connected IDs for this service
497
					foreach ( $service_connections as $service_connection ) {
498
						delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
499
					}
500
				}
501
			}
502
		}
503
504
		if ( ! is_null( $publicize_custom_message ) ) {
505
			if ( empty( $publicize_custom_message ) ) {