Plugin Directory

source: fleet/trunk/includes/iworks/class-iworks-fleet.php @ 3089212

Last change on this file since 3089212 was 3089212, checked in by iworks, 2 months ago

2.1.6

File size: 14.4 KB
Line 
1<?php
2/*
3Copyright 2017-2018 Marcin Pietrzak (marcin@iworks.pl)
4
5this program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License, version 2, as
7published by the Free Software Foundation.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20if ( ! defined( 'WPINC' ) ) {
21        die;
22}
23
24if ( class_exists( 'iworks_fleet' ) ) {
25        return;
26}
27
28require_once dirname( dirname( __FILE__ ) ) . '/iworks.php';
29
30class iworks_fleet extends iworks {
31
32        private $capability;
33        private $post_type_boat;
34        private $post_type_person;
35        private $post_type_result;
36        private $blocks;
37        protected $options;
38
39        /**
40         * integrations objects
41         *
42         * @since 2.1.6
43         */
44        private $objects = array();
45
46        public function __construct() {
47                parent::__construct();
48                $this->options    = iworks_fleet_get_options_object();
49                $this->base       = dirname( dirname( __FILE__ ) );
50                $this->dir        = basename( dirname( $this->base ) );
51                $this->version    = '2.1.6';
52                $this->capability = apply_filters( 'iworks_fleet_capability', 'manage_options' );
53                /**
54                 * post_types
55                 */
56                $post_types = array( 'boat', 'person', 'result' );
57                foreach ( $post_types as $post_type ) {
58                        include_once $this->base . '/iworks/fleet/posttypes/' . $post_type . '.php';
59                        $class        = sprintf( 'iworks_fleet_posttypes_%s', $post_type );
60                        $value        = sprintf( 'post_type_%s', $post_type );
61                        $this->$value = new $class();
62                }
63                /**
64                 * blocks
65                 */
66                // include_once $this->base . '/iworks/fleet/class-iworks-fleet-blocks.php';
67                // $this->blocks = new iworks_fleet_blocks( $this );
68                /**
69                 * admin init
70                 */
71                add_action( 'admin_init', array( $this, 'admin_init' ) );
72                add_action( 'init', array( $this, 'register_boat_number' ) );
73                add_action( 'init', array( $this, 'db_install' ) );
74                add_action( 'wp_enqueue_scripts', array( $this, 'register_styles' ), 0 );
75                add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
76                add_action( 'plugins_loaded', array( $this, 'action_plugins_loaded' ) );
77                /**
78                 * custom theme
79                 */
80                // add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ), 0 );
81                // add_action( 'init', array( $this, 'register_theme_directory' ) );
82                // add_filter( 'theme_root_uri', array( $this, 'theme_root_uri'), 10, 3 );
83                /**
84                 * shortcodes
85                 */
86                add_shortcode( 'fleet_stats', array( $this, 'stats' ) );
87                /**
88                 * Add SVG to allowed files.
89                 *
90                 * @since 1.2.2
91                 */
92                add_filter( 'upload_mimes', array( $this, 'add_mime_types' ) );
93                /**
94                 * iWorks Rate integration - change logo for rate
95                 *
96                 * @since 2.0.6
97                 */
98                add_filter( 'iworks_rate_notice_logo_style', array( $this, 'filter_plugin_logo' ), 10, 2 );
99        }
100
101        /**
102         * Add SVG to allowed files.
103         *
104         * @since 1.2.2
105         */
106        public function add_mime_types( $mimes ) {
107                $mimes['svg'] = 'image/svg+xml';
108                return $mimes;
109        }
110
111        /**
112         * Publish fleet Statistics.
113         *
114         * #since 1.0.0
115         */
116        public function stats() {
117                $content  = '<div class="fleet-statistsics">';
118                $content .= sprintf( '<h2>%s</h2>', esc_html__( 'Statistics', 'fleet' ) );
119                $content .= '<dl>';
120                $content .= sprintf( '<dt>%s</dt>', esc_html__( 'Number of sailors', 'fleet' ) );
121                $content .= sprintf( '<dd><a href="%s">%d</a></dd>', get_post_type_archive_link( $this->post_type_person->get_name() ), $this->post_type_person->count() );
122                $content .= sprintf( '<dt>%s</dt>', esc_html__( 'Number of boats', 'fleet' ) );
123                $content .= sprintf( '<dd><a href="%s">%d</a></dd>', get_post_type_archive_link( $this->post_type_boat->get_name() ), $this->post_type_boat->count() );
124                $content .= sprintf( '<dt>%s</dt>', esc_html__( 'Number of event results', 'fleet' ) );
125                $content .= sprintf( '<dd><a href="%s">%d</a></dd>', get_post_type_archive_link( $this->post_type_result->get_name() ), $this->post_type_result->count() );
126                $content .= '</dl>';
127                $content .= '</div>';
128                return $content;
129        }
130
131        public function admin_init() {
132                iworks_fleet_options_init();
133                add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
134                add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 );
135        }
136
137        public function get_post_type_name( $post_type ) {
138                $value = sprintf( 'post_type_%s', $post_type );
139                if ( isset( $this->$value ) ) {
140                        return $this->$value->get_name();
141                }
142                return new WP_Error( 'broke', __( 'fleet do not have such post type!', 'fleet' ) );
143        }
144
145        public function admin_enqueue_scripts() {
146                $screen = get_current_screen();
147                /**
148                 * off on not fleet pages
149                 */
150                $re = sprintf( '/%s_/', __CLASS__ );
151                if ( ! preg_match( $re, $screen->id ) ) {
152                        return;
153                }
154                /**
155                 * datepicker
156                 */
157                $file = 'assets/externals/datepicker/css/jquery-ui-datepicker.css';
158                $file = plugins_url( $file, $this->base );
159                wp_register_style( 'jquery-ui-datepicker', $file, false, '1.12.1' );
160                /**
161                 * select2
162                 */
163                $file = 'assets/externals/select2/css/select2.min.css';
164                $file = plugins_url( $file, $this->base );
165                wp_register_style( 'select2', $file, false, '4.0.3' );
166                /**
167                 * Admin styles
168                 */
169                $file    = sprintf( '/assets/styles/fleet-admin%s.css', $this->dev );
170                $version = $this->get_version( $file );
171                $file    = plugins_url( $file, $this->base );
172                wp_register_style( 'admin-fleet', $file, array( 'jquery-ui-datepicker', 'select2' ), $version );
173                wp_enqueue_style( 'admin-fleet' );
174                /**
175                 * select2
176                 */
177                wp_register_script( 'select2', plugins_url( 'assets/externals/select2/js/select2.full.min.js', $this->base ), array(), '4.0.3' );
178                /**
179                 * Admin scripts
180                 */
181                $files = array(
182                        'fleet-admin' => sprintf( 'assets/scripts/admin/fleet%s.js', $this->dev ),
183                );
184                if ( '' == $this->dev ) {
185                        $files = array(
186                                'fleet-admin-select2'    => 'assets/scripts/admin/src/select2.js',
187                                'fleet-admin-boat'       => 'assets/scripts/admin/src/boat.js',
188                                'fleet-admin-datepicker' => 'assets/scripts/admin/src/datepicker.js',
189                                'fleet-admin-person'     => 'assets/scripts/admin/src/person.js',
190                                'fleet-admin-result'     => 'assets/scripts/admin/src/result.js',
191                                'fleet-admin'            => 'assets/scripts/admin/src/fleet.js',
192                        );
193                }
194                $deps = array(
195                        'jquery-ui-datepicker',
196                        'select2',
197                );
198                foreach ( $files as $handle => $file ) {
199                        wp_register_script(
200                                $handle,
201                                plugins_url( $file, $this->base ),
202                                $deps,
203                                $this->get_version(),
204                                true
205                        );
206                        wp_enqueue_script( $handle );
207                }
208                /**
209                 * JavaScript messages
210                 *
211                 * @since 1.0.0
212                 */
213                $data = array(
214                        'messages' => array(),
215                        'nonces'   => array(),
216                        'user_id'  => get_current_user_id(),
217                );
218                wp_localize_script(
219                        'fleet-admin',
220                        __CLASS__,
221                        apply_filters( 'wp_localize_script_fleet_admin', $data )
222                );
223        }
224
225        public function init() {
226                if ( is_admin() ) {
227                } else {
228                        $file = 'assets/styles/fleet' . $this->dev . '.css';
229                        wp_enqueue_style( 'fleet', plugins_url( $file, $this->base ), array(), $this->get_version( $file ) );
230                }
231        }
232
233        /**
234         * Plugin row data
235         */
236        public function plugin_row_meta( $links, $file ) {
237                if ( $this->dir . '/fleet.php' == $file ) {
238                        if ( ! is_multisite() && current_user_can( $this->capability ) ) {
239                                $links[] = '<a href="themes.php?page=' . $this->dir . '/admin/index.php">' . __( 'Settings' ) . '</a>';
240                        }
241
242                        $links[] = '<a href="http://iworks.pl/donate/fleet.php">' . __( 'Donate' ) . '</a>';
243
244                }
245                return $links;
246        }
247
248        public function register_boat_number() {
249                $labels = array(
250                        'name'                       => _x( 'Boat Numbers', 'Taxonomy General Name', 'fleet' ),
251                        'singular_name'              => _x( 'Boat Number', 'Taxonomy Singular Name', 'fleet' ),
252                        'menu_name'                  => __( 'Boat Number', 'fleet' ),
253                        'all_items'                  => __( 'All Boat Numbers', 'fleet' ),
254                        'new_item_name'              => __( 'New Boat Number Name', 'fleet' ),
255                        'add_new_item'               => __( 'Add New Boat Number', 'fleet' ),
256                        'edit_item'                  => __( 'Edit Boat Number', 'fleet' ),
257                        'update_item'                => __( 'Update Boat Number', 'fleet' ),
258                        'view_item'                  => __( 'View Boat Number', 'fleet' ),
259                        'separate_items_with_commas' => __( 'Separate Boat Numbers with commas', 'fleet' ),
260                        'add_or_remove_items'        => __( 'Add or remove Boat Numbers', 'fleet' ),
261                        'choose_from_most_used'      => __( 'Choose from the most used', 'fleet' ),
262                        'popular_items'              => __( 'Popular Boat Numbers', 'fleet' ),
263                        'search_items'               => __( 'Search Boat Numbers', 'fleet' ),
264                        'not_found'                  => __( 'Not Found', 'fleet' ),
265                        'no_terms'                   => __( 'No items', 'fleet' ),
266                        'items_list'                 => __( 'Boat Numbers list', 'fleet' ),
267                        'items_list_navigation'      => __( 'Boat Numbers list navigation', 'fleet' ),
268                );
269                $args   = array(
270                        'labels'             => $labels,
271                        'hierarchical'       => false,
272                        'public'             => true,
273                        'show_admin_column'  => true,
274                        'show_in_nav_menus'  => true,
275                        'show_tagcloud'      => true,
276                        'show_ui'            => true,
277                        'show_in_quick_edit' => true,
278                        'rewrite'            => array(
279                                'slug' => _x( 'fleet-boat-number', 'slug for images', 'fleet' ),
280                        ),
281                );
282                register_taxonomy( 'boat_number', array( 'attachment' ), $args );
283        }
284
285        /**
286         * Get person name
287         */
288        public function get_person_name( $user_post_id ) {
289                return $this->post_type_person->get_person_name_by_id( $user_post_id );
290        }
291        /**
292         * Get person avatar
293         */
294        public function get_person_avatar( $user_post_id ) {
295                return $this->post_type_person->get_person_avatar_by_id( $user_post_id );
296        }
297
298        public function get_list_by_post_type( $type ) {
299                $args  = array(
300                        'post_type' => $this->{'post_type_' . $type}->get_name(),
301                        'nopaging'  => true,
302                );
303                $list  = array();
304                $posts = get_posts( $args );
305                foreach ( $posts as $post ) {
306                        $list[ $post->post_title ] = $post->ID;
307                }
308                return $list;
309        }
310
311        public function db_install() {
312                global $wpdb;
313                $version = intval( get_option( 'fleet_db_version' ) );
314                /**
315                 * 20180611
316                 */
317                $install = 20180611;
318                if ( $install > $version ) {
319                        $charset_collate = $wpdb->get_charset_collate();
320                        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
321                        $table_name = $wpdb->prefix . 'fleet_regatta';
322                        $sql        = "CREATE TABLE $table_name (
323                ID mediumint(9) NOT NULL AUTO_INCREMENT,
324                post_regata_id mediumint(9) NOT NULL,
325                year year,
326                boat_id mediumint(9),
327                helm_id mediumint(9),
328                crew_id mediumint(9),
329                helm_name text,
330                crew_name text,
331                place int,
332                points int,
333                PRIMARY KEY (ID),
334                KEY ( post_regata_id ),
335                KEY ( year ),
336                KEY ( boat_id ),
337                KEY ( helm_id ),
338                KEY ( crew_id )
339            ) $charset_collate;";
340                        dbDelta( $sql );
341                        $table_name = $wpdb->prefix . 'fleet_regatta_race';
342                        $sql        = "CREATE TABLE $table_name (
343                ID mediumint(9) NOT NULL AUTO_INCREMENT,
344                post_regata_id mediumint(9) NOT NULL,
345                regata_id mediumint(9) NOT NULL,
346                number int,
347                code varchar(4),
348                place int,
349                points int default 0,
350                discard boolean default 0,
351                PRIMARY KEY (ID),
352                KEY ( post_regata_id ),
353                KEY ( regata_id )
354            ) $charset_collate;";
355                        dbDelta( $sql );
356                        update_option( 'fleet_db_version', $install );
357                }
358                /**
359                 * 20180618
360                 */
361                $install = 20180618;
362                if ( $install > $version ) {
363                        $charset_collate = $wpdb->get_charset_collate();
364                        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
365                        $table_name = $wpdb->prefix . 'fleet_regatta';
366                        $sql        = "ALTER TABLE $table_name ADD COLUMN country TEXT AFTER boat_id;";
367                        $result     = $wpdb->query( $sql );
368                        if ( $result ) {
369                                update_option( 'fleet_db_version', $install );
370                        }
371                }
372                /**
373                 * 20180619
374                 */
375                $install = 20180619;
376                if ( $install > $version ) {
377                        $charset_collate = $wpdb->get_charset_collate();
378                        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
379                        $table_name = $wpdb->prefix . 'fleet_regatta';
380                        $sql        = "ALTER TABLE $table_name ADD COLUMN date date AFTER year;";
381                        $result     = $wpdb->query( $sql );
382                        if ( $result ) {
383                                $sql    = "ALTER TABLE $table_name ADD key ( date );";
384                                $result = $wpdb->query( $sql );
385                        }
386                        if ( $result ) {
387                                update_option( 'fleet_db_version', $install );
388                        }
389                }
390        }
391
392        /**
393         * register styles
394         *
395         * @since 1.3.0
396         */
397        public function register_styles() {
398                wp_register_style(
399                        $this->options->get_option_name( 'frontend' ),
400                        sprintf( plugins_url( '/assets/styles/fleet%s.css', $this->base ), $this->dev ? '' : '.min' ),
401                        array(),
402                        $this->version
403                );
404        }
405
406        /**
407         * Enquque styles
408         *
409         * @since 1.3.0
410         */
411        public function enqueue_styles() {
412                if ( $this->options->get_option( 'load_frontend_css' ) ) {
413                        wp_enqueue_style( $this->options->get_option_name( 'frontend' ) );
414                }
415        }
416
417        /**
418         * register_theme_directory
419         *
420         * @since 1.3.0
421         *
422         */
423        public function register_theme_directory() {
424                register_theme_directory( dirname( $this->base ) . '/integration/themes' );
425        }
426
427        public function after_setup_theme() {
428                $theme = wp_get_theme();
429                if ( 'twentytwenty-5o5-child' !== $theme->get( 'Name' ) ) {
430                        return;
431                }
432        }
433
434        public function theme_root_uri( $theme_root_uri, $siteurl, $stylesheet_or_template ) {
435                return $theme_root_uri;
436        }
437
438        /**
439         * Plugin logo for rate messages
440         *
441         * @since 2.0.6
442         *
443         * @param string $logo Logo, can be empty.
444         * @param object $plugin Plugin basic data.
445         */
446        public function filter_plugin_logo( $logo, $plugin ) {
447                if ( is_object( $plugin ) ) {
448                        $plugin = (array) $plugin;
449                }
450                if ( 'fleet' === $plugin['slug'] ) {
451                        return plugin_dir_url( dirname( dirname( __FILE__ ) ) ) . '/assets/images/logo.svg';
452                }
453                return $logo;
454        }
455
456        /**
457         * load integrations
458         *
459         * @since 2.1.6
460         */
461        public function action_plugins_loaded() {
462                $dir = dirname( __FILE__ ) . '/fleet';
463                /**
464                 * og
465                 *
466                 * @since 2.1.6
467                 */
468                if ( class_exists( 'iWorks_OpenGraph' ) ) {
469                        include_once $dir . '/integration/class-iworks-fleet-integration-og.php';
470                        $this->objects['og'] = new iworks_fleet_integration_og();
471                }
472                /**
473                 * fleet loaded action
474                 *
475                 * @since 2.1.6
476                 */
477                do_action( 'fleet/loaded' );
478        }
479}
Note: See TracBrowser for help on using the repository browser.