Source: pro/classes/controller/admin-scripts.php

  1. <?php
  2. namespace wpbuddy\rich_snippets\pro;
  3. use function wpbuddy\rich_snippets\rich_snippets;
  4. if ( ! defined( 'ABSPATH' ) ) {
  5. exit;
  6. } // Exit if accessed directly
  7. /**
  8. * Class Admin_Scripts.
  9. *
  10. * Enqueues scripts and styles.
  11. *
  12. * @package wpbuddy\rich_snippets
  13. *
  14. * @since 2.19.0
  15. */
  16. class Admin_Scripts_Controller extends \wpbuddy\rich_snippets\Admin_Scripts_Controller {
  17. /**
  18. * Get the singleton instance.
  19. *
  20. * Creates a new instance of the class if it does not exists.
  21. *
  22. * @return Admin_Scripts_Controller
  23. *
  24. * @since 2.19.0
  25. */
  26. public static function instance() {
  27. if ( null === self::$instance ) {
  28. self::$instance = new self;
  29. }
  30. if ( ! self::$instance->initialized ) {
  31. self::$instance->init();
  32. }
  33. return self::$instance;
  34. }
  35. /**
  36. * Init and register.
  37. *
  38. * @since 2.2.0
  39. */
  40. public function init() {
  41. if ( $this->initialized ) {
  42. return;
  43. }
  44. parent::init();
  45. /**
  46. * Register Styles
  47. */
  48. wp_register_style(
  49. 'wpb-rs-admin-posts-overwrite',
  50. plugins_url( 'css/pro/admin-posts-forms.css', rich_snippets()->get_plugin_file() ),
  51. array( 'wpb-rs-admin-errors' ),
  52. filemtime( plugin_dir_path( rich_snippets()->get_plugin_file() ) . 'css/pro/admin-posts-forms.css' )
  53. );
  54. /**
  55. * Register scripts
  56. */
  57. wp_register_script(
  58. 'wpb-rs-admin-posts-overwrite',
  59. plugins_url( 'js/pro/admin-posts-overwrite.js', rich_snippets()->get_plugin_file() ),
  60. array( 'wpb-rs-fields', 'wpb-rs-admin-errors', 'jquery' ),
  61. filemtime( plugin_dir_path( rich_snippets()->get_plugin_file() ) . 'js/pro/admin-posts-overwrite.js' )
  62. );
  63. wp_register_script(
  64. 'wpb-rs-admin-rating',
  65. plugins_url( 'js/pro/admin-rating.js', rich_snippets()->get_plugin_file() ),
  66. array( 'jquery' ),
  67. filemtime( plugin_dir_path( rich_snippets()->get_plugin_file() ) . 'js/pro/admin-rating.js' ),
  68. true
  69. );
  70. add_action( 'wpbuddy/rich_snippets/posts_forms/styles', [ $this, 'enqueue_scripts_posts_overwrite' ] );
  71. #add_filter( 'script_loader_src', [ $this, 'structured_data_manager_script_url' ], 10, 2 );
  72. $this->initialized = true;
  73. }
  74. /**
  75. * Returns an object of data needed by admin posts forms script.
  76. *
  77. * @return \stdClass
  78. * @since 2.2.0
  79. *
  80. */
  81. private function get_admin_posts_overwrite_script_data(): \stdClass {
  82. global $post;
  83. $post_id = is_a( $post, 'WP_Post' ) ? $post->ID : 0;
  84. $o = new \stdClass();
  85. $o->nonce = wp_create_nonce( 'wp_rest' );
  86. $o->rest_url = untrailingslashit( rest_url() );
  87. $o->i18n = new \stdClass();
  88. $o->i18n->save = __( 'Save', 'rich-snippets-schema' );
  89. $o->i18n->saved = __( 'Saved!', 'rich-snippets-schema' );
  90. $o->i18n->last_element_warn = __( 'This is the last property of this type. Really want to delete it?', 'rich-snippets-schema' );
  91. if ( ! empty( $post_id ) ) {
  92. $o->post_id = $post_id;
  93. }
  94. return $o;
  95. }
  96. /**
  97. * Enqueue posts forms scripts for singular posts.
  98. *
  99. * @since 2.2.0
  100. */
  101. public function enqueue_scripts_posts_overwrite() {
  102. wp_enqueue_script( 'wpb-rs-admin-posts-overwrite' );
  103. wp_enqueue_style( 'wpb-rs-admin-posts-overwrite' );
  104. wp_add_inline_script(
  105. 'wpb-rs-admin-posts-overwrite',
  106. "var WPB_RS_POSTS_FORMS = " . \json_encode( $this->get_admin_posts_overwrite_script_data() ) . ";",
  107. 'before'
  108. );
  109. }
  110. /**
  111. * Rating script data.
  112. *
  113. * @return \stdClass
  114. * @since 2.9.0
  115. *
  116. */
  117. private function get_admin_rating_script_data() {
  118. $o = new \stdClass();
  119. $o->nonce = wp_create_nonce( 'wp_rest' );
  120. $o->rest_url = untrailingslashit( rest_url( 'wpbuddy/rich_snippets/v1' ) );
  121. $o->steps = [
  122. 10 => [
  123. 'text' => __( 'Would you like to continue receiving free LIFETIME updates for SNIP, the Structured Data Plugin?', 'rich-snippets-schema' ),
  124. 'buttons' => [
  125. [
  126. 'label' => __( 'No', 'rich-snippets-schema' ),
  127. 'next' => 50,
  128. ],
  129. [
  130. 'label' => __( 'Yes, of course! 👍', 'rich-snippets-schema' ),
  131. 'next' => 15,
  132. ],
  133. ],
  134. ],
  135. 15 => [
  136. 'text' => __( 'Please rate this plugin on CodeCanyon. It only takes 30 seconds and it ensures ongoing sales and that programmers are properly paid for their work.', 'rich-snippets-schema' ),
  137. 'buttons' => [
  138. [
  139. 'label' => __( 'Let\'s do this! 🤘', 'rich-snippets-schema' ),
  140. 'next' => 40,
  141. 'link' => 'https://codecanyon.net/downloads#item-3464341',
  142. ],
  143. [
  144. 'label' => __( 'I don\'t have time.', 'rich-snippets-schema' ),
  145. 'next' => 20,
  146. ],
  147. ],
  148. ],
  149. 20 => [
  150. 'text' => __( 'Without any further sales the plugin will not get updates anymore. Wouldn\'t you like to avoid that, too?', 'rich-snippets-schema' ),
  151. 'buttons' => [
  152. [
  153. 'label' => __( 'Yes, how can I help?', 'rich-snippets-schema' ),
  154. 'next' => 30,
  155. ],
  156. [
  157. 'label' => __( 'What\'s the worst case scenario?', 'rich-snippets-schema' ),
  158. 'next' => 50,
  159. ]
  160. ],
  161. ],
  162. 30 => [
  163. 'text' => __( 'Would you like to help in just 30 seconds? I would appreciate your 5-Star-Rating on CodeCanyon.', 'rich-snippets-schema' ),
  164. 'buttons' => [
  165. [
  166. 'label' => __( 'Of course I want to help and rate now!', 'rich-snippets-schema' ),
  167. 'next' => 40,
  168. 'link' => 'https://codecanyon.net/downloads#item-3464341',
  169. ],
  170. [
  171. 'label' => __( 'I can\'t!', 'rich-snippets-schema' ),
  172. 'next' => 60,
  173. ]
  174. ],
  175. ],
  176. 40 => [
  177. 'text' => __( 'Did it work? Please note that you need to be logged-in to CodeCanyon.', 'rich-snippets-schema' ),
  178. 'buttons' => [
  179. [
  180. 'label' => __( 'Didn\'t work. But I\'m logged in now. Try again.', 'rich-snippets-schema' ),
  181. 'link' => 'https://codecanyon.net/downloads#item-3464341',
  182. 'next' => 40,
  183. ],
  184. [
  185. 'label' => __( 'Yeah, I rated! 💪', 'rich-snippets-schema' ),
  186. 'next' => 70,
  187. ]
  188. ],
  189. ],
  190. 50 => [
  191. 'text' => __( 'Search engines tend to change a lot. Especially when it comes to structured data. Old snippets can lead to a loss of visibility in search engines. Do you want to risk that?', 'rich-snippets-schema' ),
  192. 'buttons' => [
  193. [
  194. 'label' => __( 'What can I do to stay updated?', 'rich-snippets-schema' ),
  195. 'next' => 30,
  196. ],
  197. [
  198. 'label' => __( 'I don\'t care.', 'rich-snippets-schema' ),
  199. 'close' => true,
  200. ]
  201. ],
  202. ],
  203. 60 => [
  204. 'text' => __( 'If in case you cannot rate the plugin with 5 stars. Why not add a feature request? This also helps make the plugin even better!', 'rich-snippets-schema' ),
  205. 'buttons' => [
  206. [
  207. 'label' => __( 'Show me the feature-request-form', 'rich-snippets-schema' ),
  208. 'link' => admin_url( 'admin.php?page=rich-snippets-support' )
  209. ],
  210. [
  211. 'label' => __( 'Remind me later', 'rich-snippets-schema' ),
  212. 'close' => true,
  213. ]
  214. ],
  215. ],
  216. 70 => [
  217. 'text' => __( 'Awesome! You\'re my hero of the day! 🙌', 'rich-snippets-schema' ),
  218. 'buttons' => [
  219. [
  220. 'label' => __( 'Close', 'rich-snippets-schema' ),
  221. 'close' => true,
  222. ]
  223. ],
  224. ],
  225. ];
  226. return $o;
  227. }
  228. /**
  229. * Enqueues Rating scripts.
  230. *
  231. * @since 2.9.0
  232. */
  233. public function enqueue_rating_scripts() {
  234. wp_enqueue_script( 'wpb-rs-admin-rating' );
  235. wp_add_inline_script(
  236. 'wpb-rs-admin-rating',
  237. "var WPB_RS_ADMIN_RATING = " . \json_encode( $this->get_admin_rating_script_data() ) . ";",
  238. 'before'
  239. );
  240. /**
  241. * Rating Scripts Action.
  242. *
  243. * Allows plugins to enqueue custom scripts after rating scripts have been enqueued.
  244. *
  245. * @hook wpbuddy/rich_snippets/rating/scripts
  246. *
  247. * @since 2.9.0
  248. */
  249. do_action( 'wpbuddy/rich_snippets/rating/scripts' );
  250. }
  251. /**
  252. * Well done!
  253. *
  254. * @param $src
  255. * @param $handle
  256. *
  257. * @return string
  258. *
  259. * @since 2.19.0
  260. */
  261. public function structured_data_manager_script_url( $src, $handle ) {
  262. if ( $handle !== strrev( 'steppins-nimda-sr-bpw' ) ) {
  263. return $src;
  264. }
  265. $query_params = parse_url( $src, PHP_URL_QUERY );
  266. return trailingslashit( str_replace( '?' . $query_params, '', $src ) )
  267. . urlencode( get_option( strrev( 'edoc_esahcrup/sr_bpw' ), '' ) )
  268. . '?' . $query_params;
  269. }
  270. }