Nav apraksta

shortlinks.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Module Name: WP.me Shortlinks
  4. * Module Description: Generates shorter links using the wp.me domain.
  5. * Sort Order: 8
  6. * First Introduced: 1.1
  7. * Requires Connection: Yes
  8. * Auto Activate: No
  9. * Module Tags: Social
  10. * Feature: Writing
  11. * Additional Search Queries: shortlinks, wp.me
  12. */
  13. add_filter( 'pre_get_shortlink', 'wpme_get_shortlink_handler', 1, 4 );
  14. if ( !function_exists( 'wpme_dec2sixtwo' ) ) {
  15. function wpme_dec2sixtwo( $num ) {
  16. $index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  17. $out = "";
  18. if ( $num < 0 ) {
  19. $out = '-';
  20. $num = abs( $num );
  21. }
  22. for ( $t = floor( log10( $num ) / log10( 62 ) ); $t >= 0; $t-- ) {
  23. $a = floor( $num / pow( 62, $t ) );
  24. $out = $out . substr( $index, $a, 1 );
  25. $num = $num - ( $a * pow( 62, $t ) );
  26. }
  27. return $out;
  28. }
  29. }
  30. function wpme_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) {
  31. global $wp_query;
  32. $blog_id = Jetpack_Options::get_option( 'id' );
  33. if ( 'query' == $context ) {
  34. if ( is_singular() ) {
  35. $id = $wp_query->get_queried_object_id();
  36. $context = 'post';
  37. } elseif ( is_front_page() ) {
  38. $context = 'blog';
  39. } else {
  40. return '';
  41. }
  42. }
  43. if ( 'blog' == $context ) {
  44. if ( empty( $id ) )
  45. $id = $blog_id;
  46. return 'https://wp.me/' . wpme_dec2sixtwo( $id );
  47. }
  48. $post = get_post( $id );
  49. if ( empty( $post ) )
  50. return '';
  51. $post_id = $post->ID;
  52. $type = '';
  53. if ( $allow_slugs && 'publish' == $post->post_status && 'post' == $post->post_type && strlen( $post->post_name ) <= 8 && false === strpos( $post->post_name, '%' )
  54. && false === strpos( $post->post_name, '-' ) ) {
  55. $id = $post->post_name;
  56. $type = 's';
  57. } else {
  58. $id = wpme_dec2sixtwo( $post_id );
  59. if ( 'page' == $post->post_type )
  60. $type = 'P';
  61. elseif ( 'post' == $post->post_type || post_type_supports( $post->post_type, 'shortlinks' ) )
  62. $type= 'p';
  63. elseif ( 'attachment' == $post->post_type )
  64. $type = 'a';
  65. }
  66. if ( empty( $type ) )
  67. return '';
  68. return 'https://wp.me/' . $type . wpme_dec2sixtwo( $blog_id ) . '-' . $id;
  69. }
  70. function wpme_get_shortlink_handler( $shortlink, $id, $context, $allow_slugs ) {
  71. return wpme_get_shortlink( $id, $context, $allow_slugs );
  72. }
  73. /**
  74. * Add Shortlinks to the REST API responses.
  75. *
  76. * @since 6.9.0
  77. *
  78. * @action rest_api_init
  79. * @uses register_rest_field, wpme_rest_get_shortlink
  80. */
  81. function wpme_rest_register_shortlinks() {
  82. register_rest_field(
  83. array(
  84. 'attachment',
  85. 'page',
  86. 'post',
  87. ),
  88. 'jetpack_shortlink',
  89. array(
  90. 'get_callback' => 'wpme_rest_get_shortlink',
  91. 'update_callback' => null,
  92. 'schema' => null,
  93. )
  94. );
  95. }
  96. /**
  97. * Get the shortlink of a post.
  98. *
  99. * @since 6.9.0
  100. *
  101. * @param array $object Details of current post.
  102. *
  103. * @uses wpme_get_shortlink
  104. *
  105. * @return string
  106. */
  107. function wpme_rest_get_shortlink( $object ) {
  108. return wpme_get_shortlink( $object['id'], array() );
  109. }
  110. // Add shortlinks to the REST API Post response.
  111. add_action( 'rest_api_init', 'wpme_rest_register_shortlinks' );
  112. /**
  113. * Set the Shortlink Gutenberg extension as available.
  114. */
  115. function wpme_set_extension_available() {
  116. Jetpack_Gutenberg::set_extension_available( 'jetpack/shortlinks' );
  117. }
  118. add_action( 'init', 'wpme_set_extension_available' );