Bez popisu

class-css-customizer-nudge.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * CSS_Customizer_Nudge file.
  4. * CSS Nudge implementation for Atomic and WPCOM.
  5. *
  6. * @package Jetpack
  7. */
  8. namespace Automattic\Jetpack\Dashboard_Customizations;
  9. /**
  10. * Class WPCOM_CSS_Customizer
  11. *
  12. * @package Automattic\Jetpack\Dashboard_Customizations
  13. */
  14. class CSS_Customizer_Nudge {
  15. /**
  16. * Call to Action URL.
  17. *
  18. * @var string
  19. */
  20. private $cta_url;
  21. /**
  22. * The nudge message.
  23. *
  24. * @var string
  25. */
  26. private $nudge_copy;
  27. /**
  28. * The name of the control in Customizer.
  29. *
  30. * @var string
  31. */
  32. private $control_name;
  33. /**
  34. * CSS_Customizer_Nudge constructor.
  35. *
  36. * @param string $cta_url The URL to the plans.
  37. * @param string $nudge_copy The nudge text.
  38. * @param string $control_name The slug prefix of the nudge.
  39. */
  40. public function __construct( $cta_url, $nudge_copy, $control_name = 'custom_css' ) {
  41. $this->cta_url = $cta_url;
  42. $this->nudge_copy = $nudge_copy;
  43. $this->control_name = $control_name;
  44. }
  45. /**
  46. * Register the assets required for the CSS nudge page from the Customizer.
  47. */
  48. public function customize_controls_enqueue_scripts_nudge() {
  49. \wp_enqueue_script(
  50. 'additional-css-js',
  51. plugins_url( 'js/additional-css.js', __FILE__ ),
  52. array(),
  53. JETPACK__VERSION,
  54. true
  55. );
  56. \wp_enqueue_style(
  57. 'additional-css',
  58. plugins_url( 'css/additional-css.css', __FILE__ ),
  59. array(),
  60. JETPACK__VERSION
  61. );
  62. }
  63. /**
  64. * Register the CSS nudge in the Customizer.
  65. *
  66. * @param \WP_Customize_Manager $wp_customize The customize manager.
  67. */
  68. public function customize_register_nudge( \WP_Customize_Manager $wp_customize ) {
  69. // Show a nudge in place of the normal CSS section.
  70. \add_action( 'customize_controls_enqueue_scripts', array( $this, 'customize_controls_enqueue_scripts_nudge' ) );
  71. $wp_customize->add_setting(
  72. $this->control_name . '[dummy_setting]',
  73. array(
  74. 'type' => $this->control_name . '_dummy_setting',
  75. 'default' => '',
  76. 'transport' => 'refresh',
  77. )
  78. );
  79. $wp_customize->add_section( $this->create_css_nudge_section( $wp_customize ) );
  80. $wp_customize->add_control( $this->create_css_nudge_control( $wp_customize ) );
  81. }
  82. /**
  83. * Create a nudge control object.
  84. *
  85. * @param \WP_Customize_Manager $wp_customize The Core Customize Manager.
  86. *
  87. * @return CSS_Nudge_Customize_Control
  88. */
  89. public function create_css_nudge_control( \WP_Customize_Manager $wp_customize ) {
  90. return new CSS_Nudge_Customize_Control(
  91. $wp_customize,
  92. $this->control_name . '_control',
  93. array(
  94. 'cta_url' => $this->cta_url,
  95. 'nudge_copy' => $this->nudge_copy,
  96. 'label' => __( 'Custom CSS', 'jetpack' ),
  97. 'section' => $this->control_name,
  98. 'settings' => $this->control_name . '[dummy_setting]',
  99. )
  100. );
  101. }
  102. /**
  103. * Create the nudge section.
  104. *
  105. * @param \WP_Customize_Manager $wp_customize The core Customize Manager.
  106. *
  107. * @return \WP_Customize_Section
  108. */
  109. public function create_css_nudge_section( \WP_Customize_Manager $wp_customize ) {
  110. return new \WP_Customize_Section(
  111. $wp_customize,
  112. $this->control_name,
  113. array(
  114. 'title' => __( 'Additional CSS', 'jetpack' ),
  115. 'priority' => 200,
  116. )
  117. );
  118. }
  119. }