PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/p/multisite-content-copier/admin/edit-post.php

https://bitbucket.org/matthewselby/wpdev
PHP | 271 lines | 186 code | 49 blank | 36 comment | 26 complexity | c0784f1d0fdb684dc24c913361439b45 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, LGPL-3.0, LGPL-2.1, AGPL-1.0, BSD-3-Clause, MIT, GPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. class MCC_Post_Meta_Box {
  3. public function __construct() {
  4. add_action( 'add_meta_boxes', array( &$this, 'add_meta_boxes' ), 10, 1 );
  5. add_action( 'admin_enqueue_scripts', array( &$this, 'enqueue_scripts' ) );
  6. //add_action( 'edit_form_top', array( &$this, 'maybe_sync_post' ) );
  7. //add_action( 'save_post', array( &$this, 'process_sync_form' ), 10, 3 );
  8. }
  9. public function enqueue_scripts( $hook ) {
  10. if ( $hook == 'post.php' && is_super_admin() ) {
  11. wp_enqueue_script( 'mcc-meta-box', MULTISTE_CC_ASSETS_URL . 'js/meta-box.js', array( 'jquery' ) );
  12. //wp_enqueue_script( 'mcc-meta-box-sync', MULTISTE_CC_ASSETS_URL . 'js/meta-box-sync.js', array( 'jquery' ) );
  13. $object = array(
  14. 'select_an_option' => __( 'You must select a destination', MULTISTE_CC_LANG_DOMAIN ),
  15. 'select_a_group' => __( 'You must select a group', MULTISTE_CC_LANG_DOMAIN )
  16. );
  17. wp_localize_script( 'mcc-meta-box', 'mcc_meta_texts', $object );
  18. }
  19. }
  20. public function add_meta_boxes( $post_type ) {
  21. if ( ! is_super_admin() )
  22. return;
  23. $post_types = array();
  24. $_post_types = mcc_get_registered_cpts();
  25. foreach ( $_post_types as $post_type )
  26. $post_types[] = $post_type->name;
  27. $post_types = array_merge( $post_types, array( 'post', 'page' ) );
  28. foreach ( $post_types as $post_type ) {
  29. add_meta_box(
  30. 'copier-meta-box',
  31. __( 'Multisite Content Copier: Copy content', MULTISTE_CC_LANG_DOMAIN ),
  32. array( &$this, 'render_copier_meta_box' ),
  33. $post_type,
  34. 'normal',
  35. 'default'
  36. );
  37. //add_meta_box(
  38. //'syncer-meta-box',
  39. //__( 'Multisite Content Copier: Sync post', MULTISTE_CC_LANG_DOMAIN ),
  40. //array( &$this, 'render_syncer_meta_box' ),
  41. //$post_type,
  42. //'normal',
  43. //'default'
  44. // );
  45. }
  46. }
  47. public function render_copier_meta_box( $post ) {
  48. $model = mcc_get_model();
  49. if ( ! in_array( $post->post_status, array( 'publish', 'draft', 'pending', 'future' ) ) ) {
  50. echo '<p>' . __( 'Please save this post if you would like to copy it.', MULTISTE_CC_LANG_DOMAIN ) . '</p>';
  51. }
  52. else {
  53. ?>
  54. <?php if ( get_post_meta( $post->ID, 'mcc_copied' ) ): ?>
  55. <p><?php _e( 'You have already copied this post, copying it again could cause duplicated posts', MULTISTE_CC_LANG_DOMAIN ); ?></p>
  56. <?php endif; ?>
  57. <?php $this->render_form_fields( 'mcc_' ); ?>
  58. <?php
  59. $link = add_query_arg(
  60. array(
  61. 'content_blog' => get_current_blog_id(),
  62. 'post_id' => $post->ID,
  63. 'mcc_action' => 'mcc_submit_metabox'
  64. ),
  65. Multisite_Content_Copier::$network_main_menu_page->get_permalink()
  66. );
  67. $link = wp_nonce_url( $link, 'mcc_submit_meta_box' );
  68. ?>
  69. <a id="mcc_copy_link" class="button-primary" href="<?php echo esc_url( $link ); ?>"><?php _e( 'Copy', MULTISTE_CC_LANG_DOMAIN ); ?></a>
  70. <?php
  71. }
  72. }
  73. /**
  74. * Render the meta box that handles the sync
  75. *
  76. * @return type
  77. */
  78. public function render_syncer_meta_box() {
  79. global $post;
  80. $synced = get_post_meta( $post->ID, 'post_synced', false );
  81. $model = mcc_get_model();
  82. if ( ! in_array( $post->post_status, array( 'publish', 'draft', 'pending' ) ) ) {
  83. echo '<p>' . __( 'Please save this post if you would like to sync it.', MULTISTE_CC_LANG_DOMAIN ) . '</p>';
  84. }
  85. else {
  86. $this->render_form_fields( 'mcc_sync_' );
  87. }
  88. wp_nonce_field( 'mcc_sync_post', '_mccnonce' );
  89. ?>
  90. <input type="submit" class="button-primary" name="mcc_sync_submit" value="<?php _e( 'Sync content', MULTISTE_CC_LANG_DOMAIN ); ?>" />
  91. <?php
  92. //$this->render_sync_scripts();
  93. }
  94. private function render_form_fields( $prefix_slug ) {
  95. global $post;
  96. ?>
  97. <h4><?php _e( 'Select destinations', MULTISTE_CC_LANG_DOMAIN ); ?></h4>
  98. <div style="margin-left:20px;">
  99. <p>
  100. <label>
  101. <input type="radio" name="<?php echo $prefix_slug; ?>dest_blog_type" class="<?php echo $prefix_slug; ?>dest_blog_type" value="all">
  102. <?php _e( 'All sites', MULTISTE_CC_LANG_DOMAIN ); ?>
  103. </label>
  104. </p>
  105. <p>
  106. <label>
  107. <input type="radio" name="<?php echo $prefix_slug; ?>dest_blog_type" class="<?php echo $prefix_slug; ?>dest_blog_type" value="group">
  108. <?php _e( 'Site group', MULTISTE_CC_LANG_DOMAIN ); ?>
  109. </label>
  110. <select name="<?php echo $prefix_slug; ?>blog_group" id="<?php echo $prefix_slug; ?>blog_group">
  111. <?php mcc_get_groups_dropdown(); ?>
  112. </select>
  113. </p>
  114. <?php $settings = mcc_get_settings(); ?>
  115. <?php if ( $settings['blog_templates_integration'] ): ?>
  116. <p>
  117. <label>
  118. <input type="radio" name="<?php echo $prefix_slug; ?>dest_blog_type" class="<?php echo $prefix_slug; ?>dest_blog_type" value="nbt-group">
  119. <?php _e( 'Blog Templates Group', MULTISTE_CC_LANG_DOMAIN ); ?>
  120. </label>
  121. <select name="<?php echo $prefix_slug; ?>nbt_blog_group" id="<?php echo $prefix_slug; ?>nbt_blog_group">
  122. <?php mcc_get_nbt_groups_dropdown(); ?>
  123. </select>
  124. </p>
  125. <?php endif; ?>
  126. </div>
  127. <h4><?php _e( 'Additional Options', MULTISTE_CC_LANG_DOMAIN ); ?></h4>
  128. <?php
  129. switch ( $post->post_type ) {
  130. case 'post':
  131. $options = mcc_get_post_additional_settings();
  132. break;
  133. case 'page':
  134. $options = mcc_get_page_additional_settings();
  135. break;
  136. default:
  137. $options = mcc_get_cpt_additional_settings();
  138. break;
  139. }
  140. ?>
  141. <ul style="margin-left:20px;">
  142. <?php foreach ( $options as $option_slug => $label ): ?>
  143. <li><label><input type="checkbox" class="mcc_options" name="<?php echo $option_slug; ?>" value="<?php echo $option_slug; ?>"></input> <?php echo $label; ?></label></li>
  144. <?php endforeach; ?>
  145. </ul>
  146. <?php
  147. }
  148. public function maybe_update_sync_content( $post_ID, $post_after, $post_before ) {
  149. }
  150. public function maybe_sync_post() {
  151. //if ( isset( $_GET['sync'] ) && $_GET['sync'] == 'true' ) {
  152. //$destination = $_GET['d'];
  153. //$group = isset( $_GET['g'] ) ? absint( $_GET['g'] ) : 0;
  154. //
  155. //if ( 'all' == $destination ) {
  156. //wp_update_network_counts();
  157. //$blogs_count = get_blog_count();
  158. //var_dump($blogs_count);
  159. //}
  160. //}
  161. }
  162. /**
  163. * Establish relationships between this post and
  164. * the rest of the blogs in order to sync content
  165. *
  166. * @param type $new_status
  167. * @param type $old_status
  168. * @param type $post
  169. * @return type
  170. */
  171. public function process_sync_form() {
  172. global $post;
  173. if ( ! empty( $_POST['mcc_sync_submit'] ) ) {
  174. check_admin_referer( 'mcc_sync_post', '_mccnonce' );
  175. $errors = array();
  176. if ( empty( $_POST['mcc_sync_dest_blog_type'] ) || ! in_array( $_POST['mcc_sync_dest_blog_type'], array( 'all', 'group', 'nbt-group' ) ) ) {
  177. $errors[] = new WP_Error( 'sync_destination', __( 'You must select a destination', MULTISTE_CC_LANG_DOMAIN ) );
  178. }
  179. if ( 'group' == $destination ) {
  180. if ( empty( $_POST['mcc_sync_blog_group'] ) ) {
  181. $errors[] = new WP_Error( 'sync_blog_group', __( 'You must select a group', MULTISTE_CC_LANG_DOMAIN ) );
  182. }
  183. }
  184. if ( 'nbt-group' == $destination ) {
  185. if ( empty( $_POST['mcc_sync_nbt_blog_group'] ) ) {
  186. $errors[] = new WP_Error( 'sync_blog_group', __( 'You must select a group', MULTISTE_CC_LANG_DOMAIN ) );
  187. }
  188. }
  189. if ( ! empty( $errors ) ) {
  190. ob_start();
  191. foreach ( $errors as $error ) {
  192. ?>
  193. <p><?php echo $error->get_error_message(); ?></p>
  194. <?php
  195. }
  196. wp_die( ob_get_clean() );
  197. }
  198. else {
  199. add_filter( 'redirect_post_location', array( &$this, 'redirect_post_location' ), 10, 2 );
  200. }
  201. }
  202. }
  203. public function redirect_post_location( $location, $post_id ) {
  204. $destination = $_POST['mcc_sync_dest_blog_type'];
  205. $group = 0;
  206. if ( 'group' == $destination ) {
  207. $group = absint( $_POST['mcc_sync_blog_group'] );
  208. }
  209. if ( 'nbt-group' == $destination ) {
  210. $group = absint( $_POST['mcc_sync_nbt_blog_group'] );
  211. }
  212. $location = add_query_arg(
  213. array(
  214. 'sync' => 'true',
  215. 'd' => $destination,
  216. 'g' => $group
  217. ),
  218. $location
  219. );
  220. return $location;
  221. }
  222. }