Code Duplication    Length = 65-66 lines in 3 locations

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

@@ 437-501 (lines=65) @@
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
					// Flag the connections we can't match with the requested list to be skipped.
487
					foreach ( $service_connections as $service_connection ) {
488
						if ( !in_array( $service_connection->meta['connection_data']->id, $requested_connections ) ) {
489
							update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
490
						} else {
491
							delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
492
						}
493
					}
494
				} else {
495
					// delete all SKIP values; it's okay to publish to all connected IDs for this service
496
					foreach ( $service_connections as $service_connection ) {
497
						delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
498
					}
499
				}
500
			}
501
		}
502
503
		if ( ! is_null( $publicize_custom_message ) ) {
504
			if ( empty( $publicize_custom_message ) ) {

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

@@ 504-569 (lines=66) @@
501
		// We ask the user/dev to pass Publicize services he/she wants activated for the post, but Publicize expects us
502
		// to instead flag the ones we don't want to be skipped. proceed with said logic.
503
		// any posts coming from Path (client ID 25952) should also not publicize
504
		if ( $publicize === false || ( isset( $this->api->token_details['client_id'] ) && 25952 == $this->api->token_details['client_id'] ) ) {
505
			// No publicize at all, skip all by ID
506
			foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) {
507
				delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name );
508
				$service_connections   = $GLOBALS['publicize_ui']->publicize->get_connections( $name );
509
				if ( ! $service_connections ) {
510
					continue;
511
				}
512
				foreach ( $service_connections as $service_connection ) {
513
					update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
514
				}
515
			}
516
		} else if ( is_array( $publicize ) && ( count ( $publicize ) > 0 ) ) {
517
			foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) {
518
				/*
519
				 * We support both indexed and associative arrays:
520
				 * * indexed are to pass entire services
521
				 * * associative are to pass specific connections per service
522
				 *
523
				 * We do support mixed arrays: mixed integer and string keys (see 3rd example below).
524
				 *
525
				 * EG: array( 'twitter', 'facebook') will only publicize to those, ignoring the other available services
526
				 * 		Form data: publicize[]=twitter&publicize[]=facebook
527
				 * 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.
528
				 * 		Form data: publicize[twitter]=$pub_conn_id_0,$pub_conn_id_3&publicize[facebook]=$pub_conn_id_7
529
				 * 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
530
				 * 		Form data: publicize[]=twitter&publicize[facebook]=$pub_conn_id_0,$pub_conn_id_3
531
				 */
532
533
				// Delete any stale SKIP value for the service by name. We'll add it back by ID.
534
				delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name );
535
536
				// Get the user's connections
537
				$service_connections = $GLOBALS['publicize_ui']->publicize->get_connections( $name );
538
539
				// if the user doesn't have any connections for this service, move on
540
				if ( ! $service_connections ) {
541
					continue;
542
				}
543
544
				if ( !in_array( $name, $publicize ) && !array_key_exists( $name, $publicize ) ) {
545
					// Skip the whole service by adding each connection ID
546
					foreach ( $service_connections as $service_connection ) {
547
						update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
548
					}
549
				} else if ( !empty( $publicize[ $name ] ) ) {
550
					// Seems we're being asked to only push to [a] specific connection[s].
551
					// Explode the list on commas, which will also support a single passed ID
552
					$requested_connections = explode( ',', ( preg_replace( '/[\s]*/', '', $publicize[ $name ] ) ) );
553
554
					// Flag the connections we can't match with the requested list to be skipped.
555
					foreach ( $service_connections as $service_connection ) {
556
						if ( !in_array( $service_connection->meta['connection_data']->id, $requested_connections ) ) {
557
							update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
558
						} else {
559
							delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
560
						}
561
					}
562
				} else {
563
					// delete all SKIP values; it's okay to publish to all connected IDs for this service
564
					foreach ( $service_connections as $service_connection ) {
565
						delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
566
					}
567
				}
568
			}
569
		}
570
571
		if ( ! is_null( $publicize_custom_message ) ) {
572
			if ( empty( $publicize_custom_message ) ) {

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

@@ 505-570 (lines=66) @@
502
		// We ask the user/dev to pass Publicize services he/she wants activated for the post, but Publicize expects us
503
		// to instead flag the ones we don't want to be skipped. proceed with said logic.
504
		// any posts coming from Path (client ID 25952) should also not publicize
505
		if ( $publicize === false || ( isset( $this->api->token_details['client_id'] ) && 25952 == $this->api->token_details['client_id'] ) ) {
506
			// No publicize at all, skip all by ID
507
			foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) {
508
				delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name );
509
				$service_connections   = $GLOBALS['publicize_ui']->publicize->get_connections( $name );
510
				if ( ! $service_connections ) {
511
					continue;
512
				}
513
				foreach ( $service_connections as $service_connection ) {
514
					update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
515
				}
516
			}
517
		} else if ( is_array( $publicize ) && ( count ( $publicize ) > 0 ) ) {
518
			foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) {
519
				/*
520
				 * We support both indexed and associative arrays:
521
				 * * indexed are to pass entire services
522
				 * * associative are to pass specific connections per service
523
				 *
524
				 * We do support mixed arrays: mixed integer and string keys (see 3rd example below).
525
				 *
526
				 * EG: array( 'twitter', 'facebook') will only publicize to those, ignoring the other available services
527
				 * 		Form data: publicize[]=twitter&publicize[]=facebook
528
				 * 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.
529
				 * 		Form data: publicize[twitter]=$pub_conn_id_0,$pub_conn_id_3&publicize[facebook]=$pub_conn_id_7
530
				 * 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
531
				 * 		Form data: publicize[]=twitter&publicize[facebook]=$pub_conn_id_0,$pub_conn_id_3
532
				 */
533
534
				// Delete any stale SKIP value for the service by name. We'll add it back by ID.
535
				delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name );
536
537
				// Get the user's connections
538
				$service_connections = $GLOBALS['publicize_ui']->publicize->get_connections( $name );
539
540
				// if the user doesn't have any connections for this service, move on
541
				if ( ! $service_connections ) {
542
					continue;
543
				}
544
545
				if ( !in_array( $name, $publicize ) && !array_key_exists( $name, $publicize ) ) {
546
					// Skip the whole service by adding each connection ID
547
					foreach ( $service_connections as $service_connection ) {
548
						update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
549
					}
550
				} else if ( !empty( $publicize[ $name ] ) ) {
551
					// Seems we're being asked to only push to [a] specific connection[s].
552
					// Explode the list on commas, which will also support a single passed ID
553
					$requested_connections = explode( ',', ( preg_replace( '/[\s]*/', '', $publicize[ $name ] ) ) );
554
555
					// Flag the connections we can't match with the requested list to be skipped.
556
					foreach ( $service_connections as $service_connection ) {
557
						if ( !in_array( $service_connection->meta['connection_data']->id, $requested_connections ) ) {
558
							update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
559
						} else {
560
							delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
561
						}
562
					}
563
				} else {
564
					// delete all SKIP values; it's okay to publish to all connected IDs for this service
565
					foreach ( $service_connections as $service_connection ) {
566
						delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
567
					}
568
				}
569
			}
570
		}
571
572
		if ( ! is_null( $publicize_custom_message ) ) {
573
			if ( empty( $publicize_custom_message ) ) {