@@ -115,6 +115,16 @@ public function register() {
115
115
add_action ( 'admin_notices ' , array ( $ this , 'show_publish_error_admin_notice ' ) );
116
116
add_action ( 'post_submitbox_minor_actions ' , array ( $ this , 'hide_disabled_publishing_actions ' ) );
117
117
add_action ( 'admin_print_scripts-revision.php ' , array ( $ this , 'disable_revision_ui_for_published_posts ' ) );
118
+
119
+ // Version check for bulk action.
120
+ if ( version_compare ( get_bloginfo ( 'version ' ), '4.7 ' , '>= ' ) ) {
121
+ add_filter ( 'bulk_actions-edit- ' . self ::SLUG , array ( $ this , 'add_snapshot_bulk_actions ' ) );
122
+ add_filter ( 'handle_bulk_actions-edit- ' . self ::SLUG , array ( $ this , 'handle_snapshot_bulk_actions ' ), 10 , 3 );
123
+ } else {
124
+ add_action ( 'admin_print_footer_scripts-edit.php ' , array ( $ this , 'snapshot_merge_print_script ' ) );
125
+ add_action ( 'load-edit.php ' , array ( $ this , 'handle_snapshot_bulk_actions_workaround ' ) );
126
+ }
127
+ add_action ( 'admin_notices ' , array ( $ this , 'admin_show_merge_error ' ) );
118
128
}
119
129
120
130
/**
@@ -675,4 +685,141 @@ public function hide_disabled_publishing_actions( $post ) {
675
685
</style>
676
686
<?php
677
687
}
688
+
689
+ /**
690
+ * Add snapshot bulk actions.
691
+ *
692
+ * @param array $bulk_actions actions.
693
+ *
694
+ * @return mixed
695
+ */
696
+ public function add_snapshot_bulk_actions ( $ bulk_actions ) {
697
+ $ bulk_actions ['merge_snapshot ' ] = __ ( 'Merge Snapshot ' , 'customize-snapshots ' );
698
+ return $ bulk_actions ;
699
+ }
700
+
701
+ /**
702
+ * Handle bulk actions.
703
+ *
704
+ * @param string $redirect_to url to redirect to.
705
+ * @param string $do_action current action.
706
+ * @param array $post_ids post ids.
707
+ *
708
+ * @return string url.
709
+ */
710
+ public function handle_snapshot_bulk_actions ( $ redirect_to , $ do_action , $ post_ids ) {
711
+ if ( 'merge_snapshot ' !== $ do_action ) {
712
+ return $ redirect_to ;
713
+ }
714
+ $ posts = array_map ( 'get_post ' , $ post_ids );
715
+ if ( count ( $ posts ) <= 1 ) {
716
+ return empty ( $ redirect_to ) ? add_query_arg ( array ( 'merge-error ' => 1 ) ) : add_query_arg ( array ( 'merge-error ' => 1 ), $ redirect_to );
717
+ }
718
+
719
+ usort ( $ posts , function ( $ a , $ b ) {
720
+ $ compare_a = $ a ->post_modified ;
721
+ $ compare_b = $ b ->post_modified ;
722
+ if ( '0000-00-00 00:00:00 ' === $ compare_a ) {
723
+ $ compare_a = $ a ->post_date ;
724
+ }
725
+ if ( '0000-00-00 00:00:00 ' === $ compare_b ) {
726
+ $ compare_b = $ b ->post_date ;
727
+ }
728
+ return strtotime ( $ compare_a ) - strtotime ( $ compare_b );
729
+ } );
730
+
731
+ $ snapshot_post_data = array ();
732
+ foreach ( $ posts as $ post ) {
733
+ $ snapshot_post_data [] = array (
734
+ 'data ' => $ this ->get_post_content ( $ post ),
735
+ 'uuid ' => $ post ->post_name ,
736
+ );
737
+ }
738
+ $ snapshots_data = wp_list_pluck ( $ snapshot_post_data , 'data ' );
739
+ $ conflict_keys = call_user_func_array ( 'array_intersect_key ' , $ snapshots_data );
740
+ $ merged_snapshot_data = call_user_func_array ( 'array_merge ' , $ snapshots_data );
741
+
742
+ foreach ( $ conflict_keys as $ key => $ conflict_val ) {
743
+ $ original_values = array ();
744
+ foreach ( $ snapshot_post_data as $ post_data ) {
745
+ if ( isset ( $ post_data ['data ' ][ $ key ] ) ) {
746
+ $ original_values [] = array (
747
+ 'uuid ' => $ post_data ['uuid ' ],
748
+ 'value ' => $ post_data ['data ' ][ $ key ]['value ' ],
749
+ );
750
+ }
751
+ }
752
+ $ merged_snapshot_data [ $ key ]['merge_conflict ' ] = $ original_values ;
753
+ }
754
+ $ post_id = $ this ->save ( array (
755
+ 'uuid ' => Customize_Snapshot_Manager::generate_uuid (),
756
+ 'status ' => 'draft ' ,
757
+ 'data ' => $ merged_snapshot_data ,
758
+ 'post_date ' => current_time ( 'mysql ' , false ),
759
+ 'post_date_gmt ' => current_time ( 'mysql ' , true ),
760
+ ) );
761
+ $ redirect_to = get_edit_post_link ( $ post_id , 'raw ' );
762
+ return $ redirect_to ;
763
+ }
764
+
765
+ /**
766
+ * Insert script for adding merge snapshot bulk action polyfill.
767
+ */
768
+ public function snapshot_merge_print_script () {
769
+ global $ post_type ;
770
+ if ( self ::SLUG === $ post_type ) {
771
+ ?>
772
+ <script type="text/javascript">
773
+ jQuery( function( $ ) {
774
+ var optionText = <?php echo wp_json_encode ( __ ( 'Merge Snapshot ' , 'customize-snapshots ' ) ); ?> ;
775
+ $( 'select[name="action"], select[name="action2"]' ).each( function() {
776
+ var option = $( '<option>', {
777
+ text: optionText,
778
+ value: 'merge_snapshot'
779
+ } );
780
+ $( this ).append( option );
781
+ } );
782
+ } );
783
+ </script>
784
+ <?php
785
+ }
786
+ }
787
+
788
+ /**
789
+ * Handles bulk action for 4.6.x and older version.
790
+ */
791
+ public function handle_snapshot_bulk_actions_workaround () {
792
+ $ wp_list_table = _get_list_table ( 'WP_Posts_List_Table ' );
793
+ $ action = $ wp_list_table ->current_action ();
794
+ if ( 'merge_snapshot ' !== $ action || ( isset ( $ _REQUEST ['post_type ' ] ) && self ::SLUG !== wp_unslash ( $ _REQUEST ['post_type ' ] ) ) ) {
795
+ return ;
796
+ }
797
+ check_admin_referer ( 'bulk-posts ' );
798
+ $ post_ids = array_map ( 'intval ' , $ _REQUEST ['post ' ] );
799
+ if ( empty ( $ post_ids ) ) {
800
+ return ;
801
+ }
802
+ $ redirect_url = $ this ->handle_snapshot_bulk_actions ( wp_get_referer (), 'merge_snapshot ' , $ post_ids );
803
+ if ( ! empty ( $ redirect_url ) ) {
804
+ wp_safe_redirect ( $ redirect_url );
805
+ exit ;
806
+ }
807
+ }
808
+
809
+ /**
810
+ * Show admin notice in case of merge error
811
+ */
812
+ public function admin_show_merge_error () {
813
+ if ( ! isset ( $ _REQUEST ['merge-error ' ] ) ) {
814
+ return ;
815
+ }
816
+ $ error = array (
817
+ 1 => __ ( 'At-least two snapshot required for merge. ' , 'customize-snapshots ' ),
818
+ );
819
+ $ error_code = intval ( $ _REQUEST ['merge-error ' ] );
820
+ if ( ! isset ( $ error [ $ error_code ] ) ) {
821
+ return ;
822
+ }
823
+ printf ( '<div class="notice notice-error is-dismissible"><p>%s</p></div> ' , esc_html ( $ error [ $ error_code ] ) );
824
+ }
678
825
}
0 commit comments