09-plugin.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. $sending_test = new sending_test_view(
  130. $this->get_database(),
  131. $this->get_settings(),
  132. $this->get_templates(),
  133. $this->get_tables()
  134. );
  135. $config_panel->connect();
  136. $dashboard->connect();
  137. $manage_messages->connect();
  138. $manage_customers->connect();
  139. $show_campaigns->connect();
  140. $manage_groups->connect();
  141. $sending_test->connect();
  142. }
  143. private function connect_menu() : void {
  144. add_menu_page(
  145. 'cx-newsletter',
  146. 'cx-newsletter',
  147. 'customize',
  148. 'cx_newsletter',
  149. '',
  150. 'dashicons-art'
  151. );
  152. }
  153. public function update() {
  154. $this
  155. ->get_plugin_updater()
  156. ->update();
  157. }
  158. private function get_plugin_updater() : plugin_updater {
  159. return new plugin_updater(
  160. $this->get_settings(),
  161. $this->get_versions(),
  162. $this->get_tables(),
  163. $this->get_database()
  164. );
  165. }
  166. public function install() {
  167. $this
  168. ->get_plugin_updater()
  169. ->install();
  170. }
  171. public function remove() {
  172. $this
  173. ->get_plugin_updater()
  174. ->remove();
  175. }
  176. protected function get_database() : wpdb {
  177. return $this->database;
  178. }
  179. protected function get_templates() : \cx_appengine\templates {
  180. return $this->templates;
  181. }
  182. protected function get_settings() : settings {
  183. return $this->settings;
  184. }
  185. protected function get_tables() : table_names {
  186. return $this->tables;
  187. }
  188. protected function get_versions() : database_versions_manager {
  189. return $this->versions;
  190. }
  191. private string $plugin;
  192. private \cx_appengine\templates $templates;
  193. private wpdb $database;
  194. private table_names $tables;
  195. private settings $settings;
  196. private messages_get_endpoint $messages_get;
  197. private messages_send_endpoint $messages_send;
  198. private settings_check_endpoint $settings_check;
  199. private database_versions_manager $versions;
  200. }