Code Duplication    Length = 65-66 lines in 3 locations

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

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

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

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

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

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