diff --git a/includes/Admin/Menu.php b/includes/Admin/Menu.php new file mode 100644 index 0000000..b91f3f8 --- /dev/null +++ b/includes/Admin/Menu.php @@ -0,0 +1,53 @@ +register_styles( $this->get_styles() ); + $this->register_scripts( $this->get_scripts() ); + } + + /** + * Get all styles. + * + * @since 0.2.0 + * + * @return array + */ + public function get_styles(): array { + return [ + 'job-place-custom-css' => [ + 'src' => JOB_PLACE_BUILD . '/style-index.css', + 'version' => filemtime( JOB_PLACE_DIR . '/build/style-index.css' ), + 'deps' => [], + ], + 'job-place-css' => [ + 'src' => JOB_PLACE_BUILD . '/index.css', + 'version' => filemtime( JOB_PLACE_DIR . '/build/index.css' ), + 'deps' => [ 'job-place-custom-css' ], + ], + ]; + } + + /** + * Get all scripts. + * + * @since 0.2.0 + * + * @return array + */ + public function get_scripts(): array { + $dependency = require_once JOB_PLACE_DIR . '/build/index.asset.php'; + + return [ + 'job-place-app' => [ + 'src' => JOB_PLACE_BUILD . '/index.js', + 'version' => filemtime( JOB_PLACE_DIR . '/build/index.js' ), + 'deps' => $dependency['dependencies'], + 'in_footer' => true, + ], + ]; + } + + /** + * Register styles. + * + * @since 0.2.0 + * + * @return void + */ + public function register_styles( array $styles ) { + foreach ( $styles as $handle => $style ) { + wp_register_style( $handle, $style['src'], $style['deps'], $style['version'] ); + } + } + + /** + * Register scripts. + * + * @since 0.2.0 + * + * @return void + */ + public function register_scripts( array $scripts ) { + foreach ( $scripts as $handle =>$script ) { + wp_register_script( $handle, $script['src'], $script['deps'], $script['version'], $script['in_footer'] ); + } + } + + /** + * Enqueue admin styles and scripts. + * + * @since 0.2.0 + * + * @return void + */ + public function enqueue_admin_assets() { + wp_enqueue_style( 'job-place-css' ); + wp_enqueue_script( 'job-place-app' ); + } +} diff --git a/job-place.php b/job-place.php index 3e86339..2876d37 100644 --- a/job-place.php +++ b/job-place.php @@ -5,60 +5,316 @@ * Description: A Job posting platform made by WordPress. * Requires at least: 5.8 * Requires PHP: 7.0 - * Version: 0.1.0 + * Version: 0.2.0 * Author: Maniruzzaman Akash * License: GPL-2.0-or-later * License URI: https://www.gnu.org/licenses/gpl-2.0.html * Text Domain: jobplace */ -add_action( 'admin_menu', 'jobplace_init_menu' ); +// don't call the file directly +if ( ! defined( 'ABSPATH' ) ) { + exit; +} /** - * Init Admin Menu. + * Job_Place class. * - * @return void + * @class Job_Place The class that holds the entire Job_Place plugin */ -function jobplace_init_menu() { - global $submenu; +final class Job_Place { + /** + * Plugin version. + * + * @var string + */ + const VERSION = '0.2.0'; + + /** + * Plugin slug. + * + * @var string + * + * @since 0.2.0 + */ + const SLUG = 'jobplace'; + + /** + * Holds various class instances. + * + * @var array + * + * @since 0.2.0 + */ + private $container = []; + + /** + * Constructor for the JobPlace class. + * + * Sets up all the appropriate hooks and actions within our plugin. + * + * @since 0.2.0 + */ + private function __construct() { + require_once __DIR__ . '/vendor/autoload.php'; - $slug = 'jobplace'; - $menu_position = 50; - $capability = 'manage_options'; - add_menu_page( esc_attr__( 'Job Place', 'jobplace' ), esc_attr__( 'Job Place', 'jobplace' ), $capability, $slug, 'jobplace_admin_page', 'dashicons-filter', $menu_position ); + $this->define_constants(); - if ( current_user_can( $capability ) ) { - $submenu[ $slug ][] = [ esc_attr__( 'Home', 'jobplace' ), $capability, 'admin.php?page=' . $slug . '#/' ]; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited - $submenu[ $slug ][] = [ esc_attr__( 'Jobs', 'jobplace' ), $capability, 'admin.php?page=' . $slug . '#/jobs' ]; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + register_activation_hook( __FILE__, [ $this, 'activate' ] ); + register_deactivation_hook( __FILE__, [ $this, 'deactivate' ] ); + + add_action( 'woocommerce_loaded', [ $this, 'init_plugin' ] ); + add_action( 'woocommerce_flush_rewrite_rules', [ $this, 'flush_rewrite_rules' ] ); } -} -/** - * Init Admin Page. - * - * @return void - */ -function jobplace_admin_page() { - require_once plugin_dir_path( __FILE__ ) . 'templates/app.php'; -} + /** + * Initializes the Job_Place() class. + * + * Checks for an existing Job_Place() instance + * and if it doesn't find one, creates it. + * + * @since 0.2.0 + * + * @return Job_Place|bool + */ + public static function init() { + static $instance = false; -add_action( 'admin_enqueue_scripts', 'jobplace_admin_enqueue_scripts' ); + if ( ! $instance ) { + $instance = new Job_Place(); + } -/** - * Enqueue scripts and styles. - * - * @return void - */ -function jobplace_admin_enqueue_scripts() { + return $instance; + } + + /** + * Magic getter to bypass referencing plugin. + * + * @since 0.2.0 + * + * @param $prop + * + * @return mixed + */ + public function __get( $prop ) { + if ( array_key_exists( $prop, $this->container ) ) { + return $this->container[ $prop ]; + } + + return $this->{$prop}; + } /** - * Enqueue styles. + * Magic isset to bypass referencing plugin. + * + * @since 0.2.0 + * + * @param $prop + * + * @return mixed */ - wp_enqueue_style( 'jobplace-style', plugin_dir_url( __FILE__ ) . 'build/index.css', [], '1.0.0' ); - wp_enqueue_style( 'jobplace-component-style', plugin_dir_url( __FILE__ ) . 'build/style-index.css', [], '1.0.0' ); + public function __isset( $prop ) { + return isset( $this->{$prop} ) || isset( $this->container[ $prop ] ); + } + + /** + * Define the constants. + * + * @since 0.2.0 + * + * @return void + */ + public function define_constants() { + define( 'JOB_PLACE_VERSION', self::VERSION ); + define( 'JOB_PLACE_SLUG', self::SLUG ); + define( 'JOB_PLACE_FILE', __FILE__ ); + define( 'JOB_PLACE_DIR', __DIR__ ); + define( 'JOB_PLACE_PATH', dirname( JOB_PLACE_FILE ) ); + define( 'JOB_PLACE_INCLUDES', JOB_PLACE_PATH . '/includes' ); + define( 'JOB_PLACE_TEMPLATE_PATH', JOB_PLACE_PATH . '/templates/' ); + define( 'JOB_PLACE_URL', plugins_url( '', JOB_PLACE_FILE ) ); + define( 'JOB_PLACE_BUILD', JOB_PLACE_URL . '/build' ); + define( 'JOB_PLACE_ASSETS', JOB_PLACE_URL . '/assets' ); + } /** - * Enqueue scripts + * Load the plugin after all plugins are loaded. + * + * @since 0.2.0 + * + * @return void */ - wp_enqueue_script( 'jobplace-script', plugin_dir_url( __FILE__ ) . 'build/index.js', array( 'wp-element' ), '1.0.0', true ); + public function init_plugin() { + $this->includes(); + $this->init_hooks(); + + /** + * Fires after the plugin is loaded. + * + * @since 0.2.0 + */ + do_action( 'job_place_loaded' ); + } + + /** + * Activating the plugin. + * + * @since 0.2.0 + * + * @return void + */ + public function activate() { + // + } + + /** + * Placeholder for deactivation function. + * + * @since 0.2.0 + * + * @return void + */ + public function deactivate() { + // + } + + /** + * Flush rewrite rules after plugin is activated or woocommerce is activated. + * + * Nothing being added here yet. + * + * @since 0.2.0 + */ + public function flush_rewrite_rules() { + // fix rewrite rules + } + + /** + * Include the required files. + * + * @since 0.2.0 + * + * @return void + */ + public function includes() { + if ( $this->is_request( 'admin' ) ) { + $this->container['admin_menu'] = new Akash\JobPlace\Admin\Menu(); + } + } + + /** + * Initialize the hooks. + * + * @since 0.2.0 + * + * @return void + */ + public function init_hooks() { + // Init classes + add_action( 'init', [ $this, 'init_classes' ] ); + + // Localize our plugin + add_action( 'init', [ $this, 'localization_setup' ] ); + + // Add the plugin page links + add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), [ $this, 'plugin_action_links' ] ); + } + + /** + * Instantiate the required classes. + * + * @since 0.2.0 + * + * @return void + */ + public function init_classes() { + // Init necessary hooks + + // Common classes + $this->container['assets'] = new Akash\JobPlace\Assets\Manager(); + } + + /** + * Initialize plugin for localization. + * + * @uses load_plugin_textdomain() + * + * @since 0.2.0 + * + * @return void + */ + public function localization_setup() { + load_plugin_textdomain( 'jobplace', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); + + // Load the React-pages translations. + if ( is_admin() ) { + // Check if handle is registered in wp-script + $this->container['assets']->register_all_scripts(); + + // Load wp-script translation for job-place-app + wp_set_script_translations( 'job-place-app', 'jobplace', plugin_dir_path( __FILE__ ) . 'languages/' ); + } + } + + /** + * What type of request is this. + * + * @since 0.2.0 + * + * @param string $type admin, ajax, cron or frontend + * + * @return bool + */ + private function is_request( $type ) { + switch ( $type ) { + case 'admin': + return is_admin(); + + case 'ajax': + return defined( 'DOING_AJAX' ); + + case 'rest': + return defined( 'REST_REQUEST' ); + + case 'cron': + return defined( 'DOING_CRON' ); + + case 'frontend': + return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' ); + } + } + + /** + * Plugin action links + * + * @param array $links + * + * @since 0.2.0 + * + * @return array + */ + public function plugin_action_links( $links ) { + $links[] = '' . __( 'Settings', 'jobplace' ) . ''; + $links[] = '' . __( 'Documentation', 'jobplace' ) . ''; + + return $links; + } } + +/** + * Initialize the main plugin. + * + * @since 0.2.0 + * + * @return \Job_Place|bool + */ +function job_place() { + return Job_Place::init(); +} + +/* + * Kick-off the plugin. + * + * @since 0.2.0 + */ +job_place(); diff --git a/templates/app.php b/templates/app.php index 51b2022..3a6ae20 100644 --- a/templates/app.php +++ b/templates/app.php @@ -1,3 +1,3 @@
-

Loading...

+

...

\ No newline at end of file