includes(); } /** * Add the meta field to REST API responses for CPT advanced-wp-table read and write. * * @since 1.0.0 */ public function register_meta_api() { register_rest_field( 'advanced-wp-table', 'advanced_wp_table_data', array( 'get_callback' => array( $this, 'get_meta' ), 'update_callback' => array( $this, 'update_meta' ), 'schema' => null, ) ); } /** * Handler for getting custom field data. * * @param array $object The object from the response. * @param string $field_name Name of field. * * @since 1.0.0 * * @return mixed */ public function get_meta( $object, $field_name ) { return get_post_meta( $object['id'], $field_name, true ); } /** * Handler for updating custom field data. * * @param mixed $value The value of the field. * @param object $object The object from the response. * @param string $field_name Name of field. * * @since 1.0.0 * * @return bool|int */ public function update_meta( $value, $object, $field_name ) { return update_post_meta( $object->ID, $field_name, $value ); } /** * Include the dependencies. * * @since 1.0.0 */ public function includes() { if ( ! $this->should_we_run() ) { return; } require_once plugin_dir_path( __FILE__ ) . 'includes/settings.php'; } /** * Register Post Type. * * @since 1.0.0 */ public function register_post_type() { $args = array( 'public' => true, 'show_in_menu' => false, 'show_in_nav_menus' => false, 'show_in_rest' => true, 'label' => __( 'Advanced WP Table', 'advanced-wp-table' ), ); register_post_type( 'advanced-wp-table', $args ); } /** * Flush rewrite rules if the previously added flag exists, and then remove the flag. */ public function flush_rewrite_rules_maybe() { if ( get_option( 'advanced_wp_table_flush_rewrite_rules_flag' ) ) { flush_rewrite_rules(); delete_option( 'advanced_wp_table_flush_rewrite_rules_flag' ); } } /** * Load frontend scripts. * * @since 1.0.0 */ public function load_frontend_scripts() { wp_register_style( 'advanced-wp-table-style', plugin_dir_url( __FILE__ ) . 'assets/advanced-wp-table.css', array(), filemtime( plugin_dir_path( __FILE__ ) . 'assets/advanced-wp-table.css' ) ); if ( 'advanced-wp-table' === get_post_type() ) { wp_enqueue_style( 'advanced-wp-table-style' ); } } /** * Load backend scripts. * * @param string $hook The hook identifier. * * @since 1.0.0 */ public function load_backend_scripts( $hook ) { if ( 'toplevel_page_advanced-wp-table' !== $hook ) { return; } wp_enqueue_media(); wp_enqueue_script( 'media-upload' ); wp_enqueue_script( 'wp-edit-post' ); wp_enqueue_style( 'wp-edit-post' ); // Automatically load dependencies and version. $asset_file = include plugin_dir_path( __FILE__ ) . 'build/index.asset.php'; wp_enqueue_script( 'advanced-wp-table-backend-js', plugin_dir_url( __FILE__ ) . 'build/index.js', $asset_file['dependencies'], $asset_file['version'], true ); } /** * Register the shortcode. * * @param array $atts The array of options. * * @since 1.0.0 * * @return mixed|void */ public function register_shortcode( $atts ) { $id = isset( $atts['id'] ) ? $atts['id'] : 0; if ( ! $id ) { return; } wp_enqueue_style( 'advanced-wp-table-style' ); $post = get_post( $id ); setup_postdata( $post ); ob_start(); echo '
'; the_content(); echo '
'; $content = ob_get_clean(); wp_reset_postdata(); return $content; } /** * Wraps our table inside unique id. * * @param string $content The post content. * * @since 1.0.1 * * @return string */ public function wrap_table_output( $content ) { if ( is_singular( 'advanced-wp-table' ) && in_the_loop() && is_main_query() ) { $wrap = ''; $wrap .= '
'; $wrap .= $content; $wrap .= '
'; $content = $wrap; } return $content; } /** * Should we run or not. * * @since 1.0.0 * * @return bool */ private function should_we_run() { if ( version_compare( get_bloginfo( 'version' ), '5.0', '<' ) ) { return false; } return true; } /** * Show admin notices. * * @since 1.0.0 */ public function show_notices() { if ( $this->should_we_run() ) { return; } $class = 'notice notice-info'; $message = __( 'Advanced WP Table plugin requires WordPress version 5.0 or greater.', 'advanced-wp-table' ); printf( '

%2$s

', esc_attr( $class ), esc_html( $message ) ); } } /** * Run when the plugin is activated. * * @since 1.0.1 */ function advanced_wp_table_activate() { if ( ! get_option( 'advanced_wp_table_flush_rewrite_rules_flag' ) ) { add_option( 'advanced_wp_table_flush_rewrite_rules_flag', true ); } } register_activation_hook( __FILE__, 'advanced_wp_table_activate' ); /** * Run when the plugin is deactivated. * * @since 1.0.1 */ function advanced_wp_table_deactivate() { delete_option( 'advanced_wp_table_flush_rewrite_rules_flag' ); } register_deactivation_hook( __FILE__, 'advanced_wp_table_deactivate' ); /** * Run the class. * * @since 1.0.0 * * @return \Advanced_WP_Table */ function advanced_wp_table_run() { return new Advanced_WP_Table(); } add_action( 'plugins_loaded', 'advanced_wp_table_run' );