0 ? $raw : 0; } // A string of digits (no sign, no decimals) casts to the id, else 0. return ( is_string( $raw ) && ctype_digit( $raw ) ) ? (int) $raw : 0; } /* --------------------------------------------------------------------- * * WordPress wiring (thin wrappers over the pure core above). * --------------------------------------------------------------------- */ /** * Register the admin hooks for every plugin taxonomy. Admin only. * * @return void */ public static function register(): void { // Admin-only screens; nothing to wire on the front end. if ( ! is_admin() ) { return; } // The CPT class supplies the taxonomy list; bail if it isn't loaded. if ( ! class_exists( 'Mlsimport_Standalone_Cpt' ) ) { return; } // For every plugin taxonomy: render the fields on add/edit, save on create/edit. foreach ( Mlsimport_Standalone_Cpt::taxonomy_slugs() as $taxonomy ) { add_action( "{$taxonomy}_add_form_fields", array( __CLASS__, 'add_fields' ) ); add_action( "{$taxonomy}_edit_form_fields", array( __CLASS__, 'edit_fields' ), 10, 2 ); add_action( "created_{$taxonomy}", array( __CLASS__, 'save' ) ); add_action( "edited_{$taxonomy}", array( __CLASS__, 'save' ) ); } // Load the media library + picker JS on the term screens. add_action( 'admin_enqueue_scripts', array( __CLASS__, 'assets' ) ); } /** * Enqueue the media library + image-picker JS on the term screens of our * taxonomies only. * * @param string $hook Current admin page hook. * @return void */ public static function assets( $hook ): void { // Only the term list (edit-tags.php) and single-term (term.php) screens. if ( 'edit-tags.php' !== $hook && 'term.php' !== $hook ) { return; } // Which taxonomy's screen this is (read from the query for routing only). $taxonomy = isset( $_GET['taxonomy'] ) ? sanitize_key( wp_unslash( $_GET['taxonomy'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen routing. // Skip taxonomies that aren't ours. if ( ! in_array( $taxonomy, Mlsimport_Standalone_Cpt::taxonomy_slugs(), true ) ) { return; } // Load the WP media library, then the picker script that drives the control. wp_enqueue_media(); $base = defined( 'MLSIMPORT_PLUGIN_URL' ) ? MLSIMPORT_PLUGIN_URL : plugin_dir_url( dirname( __DIR__ ) . '/mlsimport.php' ); $ver = defined( 'MLSIMPORT_VERSION' ) ? MLSIMPORT_VERSION : false; wp_enqueue_script( 'mlsimport-term-meta', $base . 'admin/js/mlsimport-term-meta.js', array( 'jquery' ), $ver, true ); } /** * Fields on the "Add new term" screen: a plain HTML textarea (TinyMCE does * not initialise reliably in the inline add-tag AJAX form) + image picker. * * @param string $taxonomy Current taxonomy slug. * @return void */ public static function add_fields( $taxonomy ): void { wp_nonce_field( self::NONCE_ACTION, self::NONCE_FIELD ); ?>