Code Duplication    Length = 65-66 lines in 3 locations

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

@@ 626-690 (lines=65) @@
623
		// We ask the user/dev to pass Publicize services he/she wants activated for the post, but Publicize expects us
624
		// to instead flag the ones we don't want to be skipped. proceed with said logic.
625
		// any posts coming from Path (client ID 25952) should also not publicize
626
		if ( $publicize === false || ( isset( $this->api->token_details['client_id'] ) && 25952 == $this->api->token_details['client_id'] ) ) {
627
			// No publicize at all, skip all by ID
628
			foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) {
629
				delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name );
630
				$service_connections   = $GLOBALS['publicize_ui']->publicize->get_connections( $name );
631
				if ( ! $service_connections ) {
632
					continue;
633
				}
634
				foreach ( $service_connections as $service_connection ) {
635
					update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
636
				}
637
			}
638
		} else if ( is_array( $publicize ) && ( count ( $publicize ) > 0 ) ) {
639
			foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) {
640
				/*
641
				 * We support both indexed and associative arrays:
642
				 * * indexed are to pass entire services
643
				 * * associative are to pass specific connections per service
644
				 *
645
				 * We do support mixed arrays: mixed integer and string keys (see 3rd example below).
646
				 *
647
				 * EG: array( 'twitter', 'facebook') will only publicize to those, ignoring the other available services
648
				 * 		Form data: publicize[]=twitter&publicize[]=facebook
649
				 * 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.
650
				 * 		Form data: publicize[twitter]=$pub_conn_id_0,$pub_conn_id_3&publicize[facebook]=$pub_conn_id_7
651
				 * 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
652
				 * 		Form data: publicize[]=twitter&publicize[facebook]=$pub_conn_id_0,$pub_conn_id_3
653
				 */
654
655
				// Delete any stale SKIP value for the service by name. We'll add it back by ID.
656
				delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name );
657
658
				// Get the user's connections
659
				$service_connections = $GLOBALS['publicize_ui']->publicize->get_connections( $name );
660
661
				// if the user doesn't have any connections for this service, move on
662
				if ( ! $service_connections ) {
663
					continue;
664
				}
665
666
				if ( !in_array( $name, $publicize ) && !array_key_exists( $name, $publicize ) ) {
667
					// Skip the whole service by adding each connection ID
668
					foreach ( $service_connections as $service_connection ) {
669
						update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
670
					}
671
				} else if ( !empty( $publicize[ $name ] ) ) {
672
					// Seems we're being asked to only push to [a] specific connection[s].
673
					// Explode the list on commas, which will also support a single passed ID
674
					$requested_connections = explode( ',', ( preg_replace( '/[\s]*/', '', $publicize[ $name ] ) ) );
675
					// Flag the connections we can't match with the requested list to be skipped.
676
					foreach ( $service_connections as $service_connection ) {
677
						if ( !in_array( $service_connection->meta['connection_data']->id, $requested_connections ) ) {
678
							update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
679
						} else {
680
							delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
681
						}
682
					}
683
				} else {
684
					// delete all SKIP values; it's okay to publish to all connected IDs for this service
685
					foreach ( $service_connections as $service_connection ) {
686
						delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
687
					}
688
				}
689
			}
690
		}
691
692
		if ( ! is_null( $publicize_custom_message ) ) {
693
			if ( empty( $publicize_custom_message ) ) {

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

@@ 699-764 (lines=66) @@
696
		// We ask the user/dev to pass Publicize services he/she wants activated for the post, but Publicize expects us
697
		// to instead flag the ones we don't want to be skipped. proceed with said logic.
698
		// any posts coming from Path (client ID 25952) should also not publicize
699
		if ( $publicize === false || ( isset( $this->api->token_details['client_id'] ) && 25952 == $this->api->token_details['client_id'] ) ) {
700
			// No publicize at all, skip all by ID
701
			foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) {
702
				delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name );
703
				$service_connections   = $GLOBALS['publicize_ui']->publicize->get_connections( $name );
704
				if ( ! $service_connections ) {
705
					continue;
706
				}
707
				foreach ( $service_connections as $service_connection ) {
708
					update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
709
				}
710
			}
711
		} else if ( is_array( $publicize ) && ( count ( $publicize ) > 0 ) ) {
712
			foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) {
713
				/*
714
				 * We support both indexed and associative arrays:
715
				 * * indexed are to pass entire services
716
				 * * associative are to pass specific connections per service
717
				 *
718
				 * We do support mixed arrays: mixed integer and string keys (see 3rd example below).
719
				 *
720
				 * EG: array( 'twitter', 'facebook') will only publicize to those, ignoring the other available services
721
				 * 		Form data: publicize[]=twitter&publicize[]=facebook
722
				 * 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.
723
				 * 		Form data: publicize[twitter]=$pub_conn_id_0,$pub_conn_id_3&publicize[facebook]=$pub_conn_id_7
724
				 * 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
725
				 * 		Form data: publicize[]=twitter&publicize[facebook]=$pub_conn_id_0,$pub_conn_id_3
726
				 */
727
728
				// Delete any stale SKIP value for the service by name. We'll add it back by ID.
729
				delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name );
730
731
				// Get the user's connections
732
				$service_connections = $GLOBALS['publicize_ui']->publicize->get_connections( $name );
733
734
				// if the user doesn't have any connections for this service, move on
735
				if ( ! $service_connections ) {
736
					continue;
737
				}
738
739
				if ( !in_array( $name, $publicize ) && !array_key_exists( $name, $publicize ) ) {
740
					// Skip the whole service by adding each connection ID
741
					foreach ( $service_connections as $service_connection ) {
742
						update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
743
					}
744
				} else if ( !empty( $publicize[ $name ] ) ) {
745
					// Seems we're being asked to only push to [a] specific connection[s].
746
					// Explode the list on commas, which will also support a single passed ID
747
					$requested_connections = explode( ',', ( preg_replace( '/[\s]*/', '', $publicize[ $name ] ) ) );
748
749
					// Flag the connections we can't match with the requested list to be skipped.
750
					foreach ( $service_connections as $service_connection ) {
751
						if ( !in_array( $service_connection->meta['connection_data']->id, $requested_connections ) ) {
752
							update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
753
						} else {
754
							delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
755
						}
756
					}
757
				} else {
758
					// delete all SKIP values; it's okay to publish to all connected IDs for this service
759
					foreach ( $service_connections as $service_connection ) {
760
						delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
761
					}
762
				}
763
			}
764
		}
765
766
		if ( ! is_null( $publicize_custom_message ) ) {
767
			if ( empty( $publicize_custom_message ) ) {

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

@@ 670-735 (lines=66) @@
667
		// We ask the user/dev to pass Publicize services he/she wants activated for the post, but Publicize expects us
668
		// to instead flag the ones we don't want to be skipped. proceed with said logic.
669
		// any posts coming from Path (client ID 25952) should also not publicize
670
		if ( $publicize === false || ( isset( $this->api->token_details['client_id'] ) && 25952 == $this->api->token_details['client_id'] ) ) {
671
			// No publicize at all, skip all by ID
672
			foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) {
673
				delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name );
674
				$service_connections   = $GLOBALS['publicize_ui']->publicize->get_connections( $name );
675
				if ( ! $service_connections ) {
676
					continue;
677
				}
678
				foreach ( $service_connections as $service_connection ) {
679
					update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
680
				}
681
			}
682
		} else if ( is_array( $publicize ) && ( count ( $publicize ) > 0 ) ) {
683
			foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) {
684
				/*
685
				 * We support both indexed and associative arrays:
686
				 * * indexed are to pass entire services
687
				 * * associative are to pass specific connections per service
688
				 *
689
				 * We do support mixed arrays: mixed integer and string keys (see 3rd example below).
690
				 *
691
				 * EG: array( 'twitter', 'facebook') will only publicize to those, ignoring the other available services
692
				 * 		Form data: publicize[]=twitter&publicize[]=facebook
693
				 * 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.
694
				 * 		Form data: publicize[twitter]=$pub_conn_id_0,$pub_conn_id_3&publicize[facebook]=$pub_conn_id_7
695
				 * 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
696
				 * 		Form data: publicize[]=twitter&publicize[facebook]=$pub_conn_id_0,$pub_conn_id_3
697
				 */
698
699
				// Delete any stale SKIP value for the service by name. We'll add it back by ID.
700
				delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name );
701
702
				// Get the user's connections
703
				$service_connections = $GLOBALS['publicize_ui']->publicize->get_connections( $name );
704
705
				// if the user doesn't have any connections for this service, move on
706
				if ( ! $service_connections ) {
707
					continue;
708
				}
709
710
				if ( !in_array( $name, $publicize ) && !array_key_exists( $name, $publicize ) ) {
711
					// Skip the whole service by adding each connection ID
712
					foreach ( $service_connections as $service_connection ) {
713
						update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
714
					}
715
				} else if ( !empty( $publicize[ $name ] ) ) {
716
					// Seems we're being asked to only push to [a] specific connection[s].
717
					// Explode the list on commas, which will also support a single passed ID
718
					$requested_connections = explode( ',', ( preg_replace( '/[\s]*/', '', $publicize[ $name ] ) ) );
719
720
					// Flag the connections we can't match with the requested list to be skipped.
721
					foreach ( $service_connections as $service_connection ) {
722
						if ( !in_array( $service_connection->meta['connection_data']->id, $requested_connections ) ) {
723
							update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
724
						} else {
725
							delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
726
						}
727
					}
728
				} else {
729
					// delete all SKIP values; it's okay to publish to all connected IDs for this service
730
					foreach ( $service_connections as $service_connection ) {
731
						delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
732
					}
733
				}
734
			}
735
		}
736
737
		if ( ! is_null( $publicize_custom_message ) ) {
738
			if ( empty( $publicize_custom_message ) ) {