PageRenderTime 25ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/server/wordpress/wp-content/plugins/duplicate-post/src/utils.php

https://gitlab.com/suporte.spturis/carnaval2015.spturis.com.br
PHP | 237 lines | 117 code | 28 blank | 92 comment | 16 complexity | 8003898fd5811339338a36e4d411e28c MD5 | raw file
  1. <?php
  2. namespace Yoast\WP\Duplicate_Post;
  3. use WP_Post;
  4. /**
  5. * Utility methods for Duplicate Post.
  6. *
  7. * @since 4.0
  8. */
  9. class Utils {
  10. /**
  11. * Flattens a version number for use in a filename.
  12. *
  13. * @param string $version The original version number.
  14. *
  15. * @return string The flattened version number.
  16. */
  17. public static function flatten_version( $version ) {
  18. $parts = \explode( '.', $version );
  19. if ( \count( $parts ) === 2 && \preg_match( '/^\d+$/', $parts[1] ) === 1 ) {
  20. $parts[] = '0';
  21. }
  22. return \implode( '', $parts );
  23. }
  24. /**
  25. * Adds slashes only to strings.
  26. *
  27. * @param mixed $value Value to slash only if string.
  28. *
  29. * @return string|mixed
  30. */
  31. public static function addslashes_to_strings_only( $value ) {
  32. return \is_string( $value ) ? \addslashes( $value ) : $value;
  33. }
  34. /**
  35. * Replaces faulty core wp_slash().
  36. *
  37. * Until WP 5.5 wp_slash() recursively added slashes not just to strings in array/objects, leading to errors.
  38. *
  39. * @param mixed $value What to add slashes to.
  40. *
  41. * @return mixed
  42. */
  43. public static function recursively_slash_strings( $value ) {
  44. return \map_deep( $value, [ self::class, 'addslashes_to_strings_only' ] );
  45. }
  46. /**
  47. * Gets the original post.
  48. *
  49. * @param int|WP_Post|null $post Optional. Post ID or Post object.
  50. * @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
  51. *
  52. * @return WP_Post|null Post data if successful, null otherwise.
  53. */
  54. public static function get_original( $post = null, $output = \OBJECT ) {
  55. $post = \get_post( $post );
  56. if ( ! $post ) {
  57. return null;
  58. }
  59. $original_id = self::get_original_post_id( $post->ID );
  60. if ( empty( $original_id ) ) {
  61. return null;
  62. }
  63. return \get_post( $original_id, $output );
  64. }
  65. /**
  66. * Determines if the post has ancestors marked for copy.
  67. *
  68. * If we are copying children, and the post has already an ancestor marked for copy, we have to filter it out.
  69. *
  70. * @param WP_Post $post The post object.
  71. * @param array $post_ids The array of marked post IDs.
  72. *
  73. * @return bool Whether the post has ancestors marked for copy.
  74. */
  75. public static function has_ancestors_marked( $post, $post_ids ) {
  76. $ancestors_in_array = 0;
  77. $parent = \wp_get_post_parent_id( $post->ID );
  78. while ( $parent ) {
  79. if ( \in_array( $parent, $post_ids, true ) ) {
  80. ++$ancestors_in_array;
  81. }
  82. $parent = \wp_get_post_parent_id( $parent );
  83. }
  84. return ( $ancestors_in_array !== 0 );
  85. }
  86. /**
  87. * Returns a link to edit, preview or view a post, in accordance to user capabilities.
  88. *
  89. * @param WP_Post $post Post ID or Post object.
  90. *
  91. * @return string|null The link to edit, preview or view a post.
  92. */
  93. public static function get_edit_or_view_link( $post ) {
  94. $post = \get_post( $post );
  95. if ( ! $post ) {
  96. return null;
  97. }
  98. $can_edit_post = \current_user_can( 'edit_post', $post->ID );
  99. $title = \_draft_or_post_title( $post );
  100. $post_type_object = \get_post_type_object( $post->post_type );
  101. if ( $can_edit_post && $post->post_status !== 'trash' ) {
  102. return \sprintf(
  103. '<a href="%s" aria-label="%s">%s</a>',
  104. \esc_url( \get_edit_post_link( $post->ID ) ),
  105. /* translators: %s: post title */
  106. \esc_attr( \sprintf( \__( 'Edit &#8220;%s&#8221;', 'duplicate-post' ), $title ) ),
  107. $title
  108. );
  109. }
  110. elseif ( \is_post_type_viewable( $post_type_object ) ) {
  111. if ( \in_array( $post->post_status, [ 'pending', 'draft', 'future' ], true ) ) {
  112. if ( $can_edit_post ) {
  113. $preview_link = \get_preview_post_link( $post );
  114. return \sprintf(
  115. '<a href="%s" rel="bookmark" aria-label="%s">%s</a>',
  116. \esc_url( $preview_link ),
  117. /* translators: %s: post title */
  118. \esc_attr( \sprintf( \__( 'Preview &#8220;%s&#8221;', 'duplicate-post' ), $title ) ),
  119. $title
  120. );
  121. }
  122. }
  123. elseif ( $post->post_status !== 'trash' ) {
  124. return \sprintf(
  125. '<a href="%s" rel="bookmark" aria-label="%s">%s</a>',
  126. \esc_url( \get_permalink( $post->ID ) ),
  127. /* translators: %s: post title */
  128. \esc_attr( \sprintf( \__( 'View &#8220;%s&#8221;', 'duplicate-post' ), $title ) ),
  129. $title
  130. );
  131. }
  132. }
  133. return $title;
  134. }
  135. /**
  136. * Gets the ID of the original post intended to be rewritten with the copy for Rewrite & Republish.
  137. *
  138. * @param int $post_id The copy post ID.
  139. *
  140. * @return int The original post id of a copy for Rewrite & Republish.
  141. */
  142. public static function get_original_post_id( $post_id ) {
  143. return (int) \get_post_meta( $post_id, '_dp_original', true );
  144. }
  145. /**
  146. * Gets the registered WordPress roles.
  147. *
  148. * @codeCoverageIgnore As this is a simple wrapper method for a built-in WordPress method, we don't have to test it.
  149. *
  150. * @return array The roles.
  151. */
  152. public static function get_roles() {
  153. global $wp_roles;
  154. return $wp_roles->get_names();
  155. }
  156. /**
  157. * Gets the default meta field names to be filtered out.
  158. *
  159. * @return array The names of the meta fields to filter out by default.
  160. */
  161. public static function get_default_filtered_meta_names() {
  162. return [
  163. '_edit_lock',
  164. '_edit_last',
  165. '_dp_original',
  166. '_dp_is_rewrite_republish_copy',
  167. '_dp_has_rewrite_republish_copy',
  168. '_dp_has_been_republished',
  169. '_dp_creation_date_gmt',
  170. ];
  171. }
  172. /**
  173. * Gets a Duplicate Post option from the database.
  174. *
  175. * @param string $option The option to get.
  176. * @param string $key The key to retrieve, if the option is an array.
  177. *
  178. * @return mixed The option.
  179. */
  180. public static function get_option( $option, $key = '' ) {
  181. $option = \get_option( $option );
  182. if ( ! \is_array( $option ) || empty( $key ) ) {
  183. return $option;
  184. }
  185. if ( ! \array_key_exists( $key, $option ) ) {
  186. return '';
  187. }
  188. return $option[ $key ];
  189. }
  190. /**
  191. * Determines if a plugin is active.
  192. *
  193. * We can't use is_plugin_active because this must work on the frontend too.
  194. *
  195. * @param string $plugin Path to the plugin file relative to the plugins directory.
  196. *
  197. * @return bool Whether a plugin is currently active.
  198. */
  199. public static function is_plugin_active( $plugin ) {
  200. if ( \in_array( $plugin, (array) \get_option( 'active_plugins', [] ), true ) ) {
  201. return true;
  202. }
  203. if ( ! \is_multisite() ) {
  204. return false;
  205. }
  206. $plugins = \get_site_option( 'active_sitewide_plugins' );
  207. return isset( $plugins[ $plugin ] );
  208. }
  209. }