PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/code/cake/app/webroot/cp/wp-content/plugins/commentpress-core/commentpress-core/class_commentpress_workflow.php

https://github.com/DigitalPaulScholtenProject/DPSP-Platform
PHP | 637 lines | 162 code | 287 blank | 188 comment | 40 complexity | fbda6e98d3439a5d6419fcf7821874f4 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php /*
  2. ================================================================================
  3. Class CommentpressCoreWorkflow
  4. ================================================================================
  5. AUTHOR: Christian Wach <needle@haystack.co.uk>
  6. --------------------------------------------------------------------------------
  7. NOTES
  8. =====
  9. This class provides "Translation" workflow to CommentPress Core.
  10. --------------------------------------------------------------------------------
  11. */
  12. /*
  13. ================================================================================
  14. Class Name
  15. ================================================================================
  16. */
  17. class CommentpressCoreWorkflow {
  18. /*
  19. ============================================================================
  20. Properties
  21. ============================================================================
  22. */
  23. // parent object reference
  24. var $parent_obj;
  25. /**
  26. * @description: initialises this object
  27. * @param object $parent_obj a reference to the parent object
  28. * @return object
  29. * @todo:
  30. *
  31. */
  32. function __construct( $parent_obj = null ) {
  33. // store reference to "parent" (calling obj, not OOP parent)
  34. $this->parent_obj = $parent_obj;
  35. // store reference to database wrapper (child of calling obj)
  36. $this->db = $this->parent_obj->db;
  37. // init
  38. $this->_init();
  39. // --<
  40. return $this;
  41. }
  42. /**
  43. * PHP 4 constructor
  44. */
  45. function CommentpressCoreWorkflow( $parent_obj = null ) {
  46. // is this php5?
  47. if ( version_compare( PHP_VERSION, "5.0.0", "<" ) ) {
  48. // call php5 constructor
  49. $this->__construct( $parent_obj );
  50. }
  51. // --<
  52. return $this;
  53. }
  54. /**
  55. * @description: set up all items associated with this object
  56. * @todo:
  57. *
  58. */
  59. function initialise() {
  60. }
  61. /**
  62. * @description: if needed, destroys all items associated with this object
  63. * @todo:
  64. *
  65. */
  66. function destroy() {
  67. }
  68. //##############################################################################
  69. /*
  70. ============================================================================
  71. PUBLIC METHODS
  72. ============================================================================
  73. */
  74. /**
  75. * @description: enable workflow
  76. * @todo:
  77. *
  78. */
  79. function blog_workflow_exists( $exists ) {
  80. // switch on, but allow overrides
  81. return apply_filters(
  82. 'cp_class_commentpress_workflow_enabled',
  83. true
  84. );
  85. }
  86. /**
  87. * @description: override the name of the workflow checkbox label
  88. * @todo:
  89. *
  90. */
  91. function blog_workflow_label( $name ) {
  92. // set label, but allow overrides
  93. return apply_filters(
  94. 'cp_class_commentpress_workflow_label',
  95. __( 'Enable Translation Workflow', 'commentpress-core' )
  96. );
  97. }
  98. /**
  99. * @description: amend the group meta if workflow is enabled
  100. * @todo:
  101. *
  102. */
  103. function group_meta_set_blog_type( $blog_type, $blog_workflow ) {
  104. // if the blog workflow is enabled, then this is a translation group
  105. if ( $blog_workflow == '1' ) {
  106. // translation is type 2
  107. $blog_type = '2';
  108. }
  109. // return, but allow overrides
  110. return apply_filters(
  111. 'cp_class_commentpress_workflow_group_blogtype',
  112. $blog_type
  113. );
  114. }
  115. /**
  116. * @description: add our metabox if workflow is enabled
  117. * @todo:
  118. *
  119. */
  120. function workflow_metabox() {
  121. global $post;
  122. // Use nonce for verification
  123. wp_nonce_field( 'commentpress_post_workflow_settings', 'commentpress_workflow_nonce' );
  124. // label
  125. echo '<h3>' . __( 'Original Text', 'commentpress-core' ) . '</h3>';
  126. // set key
  127. $key = '_cp_original_text';
  128. // get content
  129. $content = get_post_meta( $post->ID, $key, true );
  130. // set editor ID (sucks that it can't use - and _)
  131. $editor_id = 'cporiginaltext';
  132. // call the editor
  133. wp_editor(
  134. esc_html( stripslashes( $content ) ),
  135. $editor_id,
  136. $settings = array(
  137. 'media_buttons' => false
  138. )
  139. );
  140. // label
  141. echo '<h3>' . __( 'Literal Translation', 'commentpress-core' ) . '</h3>';
  142. // set key
  143. $key = '_cp_literal_translation';
  144. // get content
  145. $content = get_post_meta( $post->ID, $key, true );
  146. // set editor ID (sucks that it can't use - and _)
  147. $editor_id = 'cpliteraltranslation';
  148. // call the editor
  149. wp_editor(
  150. esc_html( stripslashes( $content ) ),
  151. $editor_id,
  152. $settings = array(
  153. 'media_buttons' => false
  154. )
  155. );
  156. }
  157. /**
  158. * @description: amend the workflow metabox title
  159. * @todo:
  160. *
  161. */
  162. function workflow_metabox_title( $title ) {
  163. // set label, but allow overrides
  164. return apply_filters(
  165. 'cp_class_commentpress_workflow_metabox_title',
  166. __( 'Translations', 'commentpress-core' )
  167. );
  168. }
  169. /**
  170. * @description: amend the workflow metabox title
  171. * @todo:
  172. *
  173. */
  174. function workflow_save_post( $post_obj ) {
  175. // how do we get the content of wp_editor()?
  176. // if no post, kick out
  177. if ( !$post_obj ) { return; }
  178. // if not page, kick out
  179. if ( $post_obj->post_type != 'post' ) { return; }
  180. // authenticate
  181. $_nonce = isset( $_POST['commentpress_workflow_nonce'] ) ? $_POST['commentpress_workflow_nonce'] : '';
  182. if ( !wp_verify_nonce( $_nonce, 'commentpress_post_workflow_settings' ) ) { return; }
  183. // is this an auto save routine?
  184. if ( defined('DOING_AUTOSAVE') AND DOING_AUTOSAVE ) { return; }
  185. //print_r( array( 'can' => current_user_can( 'edit_posts' ) ) ); die();
  186. // Check permissions
  187. if ( !current_user_can( 'edit_posts' ) ) { return; }
  188. // OK, we're authenticated
  189. // check for revision
  190. if ( $post_obj->post_type == 'revision' ) {
  191. // get parent
  192. if ( $post_obj->post_parent != 0 ) {
  193. $post = get_post( $post_obj->post_parent );
  194. } else {
  195. $post = $post_obj;
  196. }
  197. } else {
  198. $post = $post_obj;
  199. }
  200. // database object
  201. global $wpdb;
  202. // ---------------------------------------------------------------------
  203. // Save the content of the two wp_editors
  204. // ---------------------------------------------------------------------
  205. // get original text
  206. $original = ( isset( $_POST['cporiginaltext'] ) ) ? $_POST['cporiginaltext'] : '';
  207. //print_r( $post ); die();
  208. // set key
  209. $key = '_cp_original_text';
  210. // if the custom field already has a value...
  211. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  212. // if empty string...
  213. if ( $original === '' ) {
  214. // delete the meta_key
  215. delete_post_meta( $post->ID, $key );
  216. } else {
  217. // update the data
  218. update_post_meta( $post->ID, $key, $original );
  219. }
  220. } else {
  221. // only add meta if we have field data
  222. if ( $original !== '' ) {
  223. // add the data
  224. add_post_meta( $post->ID, $key, $wpdb->escape( $original ) );
  225. }
  226. }
  227. // get literal translation
  228. $literal = ( isset( $_POST['cpliteraltranslation'] ) ) ? $_POST['cpliteraltranslation'] : '';
  229. // set key
  230. $key = '_cp_literal_translation';
  231. // if the custom field already has a value...
  232. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  233. // if empty string...
  234. if ( $literal === '' ) {
  235. // delete the meta_key
  236. delete_post_meta( $post->ID, $key );
  237. } else {
  238. // update the data
  239. update_post_meta( $post->ID, $key, $literal );
  240. }
  241. } else {
  242. // only add meta if we have field data
  243. if ( $literal !== '' ) {
  244. // add the data
  245. add_post_meta( $post->ID, $key, $wpdb->escape( $literal ) );
  246. }
  247. }
  248. }
  249. /**
  250. * @description: add the workflow content to the new version
  251. * @todo:
  252. *
  253. */
  254. function workflow_save_copy( $new_post_id ) {
  255. // ---------------------------------------------------------------------
  256. // If we are making a copy of the current version, also save meta
  257. // ---------------------------------------------------------------------
  258. // find and save the data
  259. $_data = ( isset( $_POST['commentpress_new_post'] ) ) ? $_POST['commentpress_new_post'] : '0';
  260. // do we want to create a new revision?
  261. if ( $_data == '0' ) { return; }
  262. // database object
  263. global $wpdb;
  264. // get original text
  265. $original = ( isset( $_POST['cporiginaltext'] ) ) ? $_POST['cporiginaltext'] : '';
  266. //print_r( $post ); die();
  267. // set key
  268. $key = '_cp_original_text';
  269. // if the custom field already has a value...
  270. if ( get_post_meta( $new_post_id, $key, true ) !== '' ) {
  271. // if empty string...
  272. if ( $original === '' ) {
  273. // delete the meta_key
  274. delete_post_meta( $post->ID, $key );
  275. } else {
  276. // update the data
  277. update_post_meta( $post->ID, $key, $original );
  278. }
  279. } else {
  280. // only add meta if we have field data
  281. if ( $original != '' ) {
  282. // add the data
  283. add_post_meta( $new_post_id, $key, $wpdb->escape( $original ) );
  284. }
  285. }
  286. // get literal translation
  287. $literal = ( isset( $_POST['cpliteraltranslation'] ) ) ? $_POST['cpliteraltranslation'] : '';
  288. // set key
  289. $key = '_cp_literal_translation';
  290. // if the custom field already has a value...
  291. if ( get_post_meta( $new_post_id, $key, true ) !== '' ) {
  292. // if empty string...
  293. if ( $literal === '' ) {
  294. // delete the meta_key
  295. delete_post_meta( $post->ID, $key );
  296. } else {
  297. // update the data
  298. update_post_meta( $post->ID, $key, $literal );
  299. }
  300. } else {
  301. // only add meta if we have field data
  302. if ( $literal != '' ) {
  303. // add the data
  304. add_post_meta( $new_post_id, $key, $wpdb->escape( $literal ) );
  305. }
  306. }
  307. }
  308. //##############################################################################
  309. /*
  310. ============================================================================
  311. PRIVATE METHODS
  312. ============================================================================
  313. */
  314. /**
  315. * @description: object initialisation
  316. * @todo:
  317. *
  318. */
  319. function _init() {
  320. // register hooks
  321. $this->_register_hooks();
  322. }
  323. /**
  324. * @description: register Wordpress hooks
  325. * @todo:
  326. *
  327. */
  328. function _register_hooks() {
  329. // enable workflow
  330. add_filter( 'cp_blog_workflow_exists', array( $this, 'blog_workflow_exists' ), 21 );
  331. // override label
  332. add_filter( 'cp_blog_workflow_label', array( $this, 'blog_workflow_label' ), 21 );
  333. // override blog type if workflow is on
  334. add_filter( 'cp_get_group_meta_for_blog_type', array( $this, 'group_meta_set_blog_type' ), 21, 2 );
  335. // is this the back end?
  336. if ( is_admin() ) {
  337. // add meta box for translation workflow
  338. add_action( 'cp_workflow_metabox', array( $this, 'workflow_metabox' ), 10, 2 );
  339. // override meta box title for translation workflow
  340. add_filter( 'cp_workflow_metabox_title', array( $this, 'workflow_metabox_title' ), 21, 1 );
  341. // save post with translation workflow
  342. add_action( 'cp_workflow_save_post', array( $this, 'workflow_save_post' ), 21, 1 );
  343. // save translation workflow for copied posts
  344. add_action( 'cp_workflow_save_copy', array( $this, 'workflow_save_copy' ), 21, 1 );
  345. }
  346. }
  347. //##############################################################################
  348. } // class ends