09-plugin.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace cx_newsletter;
  3. use \add_action;
  4. use \add_filter;
  5. use \load_plugin_textdomain;
  6. use \wp_next_scheduled;
  7. use \wp_schedule_event;
  8. use \add_menu_page;
  9. use \wpdb;
  10. class plugin {
  11. public function __construct(string $plugin) {
  12. add_action('admin_menu', [$this, 'admin']);
  13. add_action('init', [$this, 'init']);
  14. add_action('rest_api_init', [$this, 'register_rest']);
  15. add_action('cx_newsletter_email_sender', [$this, 'email_sender']);
  16. add_filter('cron_schedules', [$this, 'add_schedules']);
  17. global $wpdb;
  18. $this->database = $wpdb;
  19. $this->plugin = $plugin;
  20. $directory = new \cx_appengine\directory(templates_dir, 'html');
  21. $dictionary = new wordpress_dictionary;
  22. $this->templates = new \cx_appengine\templates($directory, $dictionary);
  23. $this->settings = new plugin_settings();
  24. $this->tables = new table_names($this->get_database());
  25. $this->versions = new database_versions_manager();
  26. $this->init_cron();
  27. }
  28. public function init() {
  29. $this->export = new customers_export_page(
  30. $this->database,
  31. $this->tables,
  32. $this->settings
  33. );
  34. $this->export->prepare();
  35. $this->translations();
  36. }
  37. private function translations() : void {
  38. load_plugin_textdomain(
  39. 'cx_newsletter',
  40. false,
  41. $this->plugin.'/translates');
  42. }
  43. public function add_schedules(array $intervals) : array {
  44. $intervals['cx_newsletter_send'] = [];
  45. $intervals['cx_newsletter_send']['interval'] = 60;
  46. $intervals['cx_newsletter_send']['display'] = 'cx-newsletter';
  47. return $intervals;
  48. }
  49. public function register_rest() : void {
  50. $this->messages_get = new messages_get_endpoint(
  51. $this->database,
  52. $this->settings,
  53. $this->tables
  54. );
  55. $this->messages_send = new messages_send_endpoint(
  56. $this->database,
  57. $this->settings,
  58. $this->tables
  59. );
  60. $this->settings_check = new settings_check_endpoint(
  61. $this->database,
  62. $this->settings,
  63. $this->tables
  64. );
  65. $registerer = new rest_register('cx-newsletter/v1');
  66. $registerer
  67. ->register($this->messages_get)
  68. ->register($this->messages_send)
  69. ->register($this->settings_check);
  70. }
  71. private function init_cron() : void {
  72. if (wp_next_scheduled('cx_newsletter_email_sender') === false) {
  73. wp_schedule_event(
  74. time(),
  75. 'cx_newsletter_send',
  76. 'cx_newsletter_email_sender'
  77. );
  78. }
  79. }
  80. public function email_sender() {
  81. $sender = new cron_emails_sender(
  82. $this->database,
  83. $this->tables,
  84. $this->settings
  85. );
  86. $sender
  87. ->prepare()
  88. ->process();
  89. }
  90. public function admin() {
  91. $this->update();
  92. $this->connect_menu();
  93. $config_panel = new config_panel_view(
  94. $this->get_database(),
  95. $this->get_settings(),
  96. $this->get_templates(),
  97. $this->get_tables()
  98. );
  99. $dashboard = new dashboard_view(
  100. $this->get_database(),
  101. $this->get_settings(),
  102. $this->get_templates(),
  103. $this->get_tables()
  104. );
  105. $manage_messages = new manage_messages_view(
  106. $this->get_database(),
  107. $this->get_settings(),
  108. $this->get_templates(),
  109. $this->get_tables()
  110. );
  111. $manage_customers = new manage_customers_view(
  112. $this->get_database(),
  113. $this->get_settings(),
  114. $this->get_templates(),
  115. $this->get_tables()
  116. );
  117. $show_campaigns = new show_campaigns_view(
  118. $this->get_database(),
  119. $this->get_settings(),
  120. $this->get_templates(),
  121. $this->get_tables()
  122. );
  123. $manage_groups = new manage_groups_view(
  124. $this->get_database(),
  125. $this->get_settings(),
  126. $this->get_templates(),
  127. $this->get_tables()
  128. );
  129. $config_panel->connect();
  130. $dashboard->connect();
  131. $manage_messages->connect();
  132. $manage_customers->connect();
  133. $show_campaigns->connect();
  134. $manage_groups->connect();
  135. }
  136. private function connect_menu() : void {
  137. add_menu_page(
  138. 'cx-newsletter',
  139. 'cx-newsletter',
  140. 'customize',
  141. 'cx_newsletter',
  142. '',
  143. 'dashicons-art'
  144. );
  145. }
  146. public function update() {
  147. $this
  148. ->get_plugin_updater()
  149. ->update();
  150. }
  151. private function get_plugin_updater() : plugin_updater {
  152. return new plugin_updater(
  153. $this->get_settings(),
  154. $this->get_versions(),
  155. $this->get_tables(),
  156. $this->get_database()
  157. );
  158. }
  159. public function install() {
  160. $this
  161. ->get_plugin_updater()
  162. ->install();
  163. }
  164. public function remove() {
  165. $this
  166. ->get_plugin_updater()
  167. ->remove();
  168. }
  169. protected function get_database() : wpdb {
  170. return $this->database;
  171. }
  172. protected function get_templates() : \cx_appengine\templates {
  173. return $this->templates;
  174. }
  175. protected function get_settings() : settings {
  176. return $this->settings;
  177. }
  178. protected function get_tables() : table_names {
  179. return $this->tables;
  180. }
  181. protected function get_versions() : database_versions_manager {
  182. return $this->versions;
  183. }
  184. private string $plugin;
  185. private \cx_appengine\templates $templates;
  186. private wpdb $database;
  187. private table_names $tables;
  188. private settings $settings;
  189. private messages_get_endpoint $messages_get;
  190. private messages_send_endpoint $messages_send;
  191. private settings_check_endpoint $settings_check;
  192. private database_versions_manager $versions;
  193. }