icons_json_path() );
}
// ── Meta box ──────────────────────────────────────────────────────────────
public function add_meta_box(): void {
add_meta_box(
'arshid6social-page-icon',
__( 'Page Icon', '6arshid-social-community' ),
array( $this, 'render_meta_box' ),
'page',
'side',
'default'
);
}
public function render_meta_box( \WP_Post $post ): void {
$current = (string) get_post_meta( $post->ID, self::META_KEY, true );
$available = $this->icons_available();
wp_nonce_field( 'arshid6social_page_icon_' . $post->ID, 'arshid6social_page_icon_nonce' );
?>
get_icon_svg( $current, 28 ), $this->allowed_svg_kses() );
}
?>
$v ) {
$out[ $k ] = $v;
if ( 'title' === $k ) {
$out['arshid6social_icon'] = __( 'Icon', '6arshid-social-community' );
}
}
return $out;
}
public function render_column( string $column, int $post_id ): void {
if ( 'arshid6social_icon' !== $column ) {
return;
}
$icon = (string) get_post_meta( $post_id, self::META_KEY, true );
if ( $icon && $this->icons_available() ) {
$svg = $this->get_icon_svg( $icon, 22 );
if ( $svg ) {
echo wp_kses( $svg, $this->allowed_svg_kses() );
echo '
' . esc_html( $icon ) . '';
return;
}
}
echo '—';
}
// ── Enqueue ───────────────────────────────────────────────────────────────
public function enqueue_assets( string $hook ): void {
$screen = get_current_screen();
if ( ! $screen || 'page' !== $screen->post_type ) {
return;
}
if ( ! in_array( $hook, array( 'post.php', 'post-new.php', 'edit.php' ), true ) ) {
return;
}
wp_enqueue_style(
'arshid6social-icon-picker',
ARSHID6SOCIAL_ASSETS_URL . 'css/admin-icon-picker.css',
array(),
ARSHID6SOCIAL_VERSION
);
wp_enqueue_script(
'arshid6social-icon-picker',
ARSHID6SOCIAL_ASSETS_URL . 'js/arshid6social-icon-picker.js',
array(),
ARSHID6SOCIAL_VERSION,
true
);
wp_localize_script(
'arshid6social-icon-picker',
'ARSHID6SOCIALICP',
array(
'iconsUrl' => $this->icons_json_url(),
'version' => self::BI_VERSION,
)
);
}
// ── Get SVG for a named icon ──────────────────────────────────────────────
public function get_icon_svg( string $name, int $size = 16 ): string {
if ( ! $this->icons_available() ) {
return '';
}
static $data = null;
if ( null === $data ) {
$path = $this->icons_json_path();
$raw = is_readable( $path ) ? file_get_contents( $path ) : false; // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$data = $raw ? (array) json_decode( $raw, true ) : array();
}
if ( ! isset( $data[ $name ] ) ) {
return '';
}
$inner = wp_kses( (string) $data[ $name ], $this->allowed_svg_kses() );
return sprintf(
'',
(int) $size,
$inner
);
}
// ── Helpers ───────────────────────────────────────────────────────────────
private function allowed_svg_kses(): array {
$common = array(
'fill' => true,
'fill-rule' => true,
'clip-rule' => true,
'opacity' => true,
);
return array(
'path' => array_merge( $common, array( 'd' => true ) ),
'circle' => array_merge(
$common,
array(
'cx' => true,
'cy' => true,
'r' => true,
)
),
'ellipse' => array_merge(
$common,
array(
'cx' => true,
'cy' => true,
'rx' => true,
'ry' => true,
)
),
'rect' => array_merge(
$common,
array(
'x' => true,
'y' => true,
'width' => true,
'height' => true,
'rx' => true,
'ry' => true,
)
),
'polyline' => array_merge( $common, array( 'points' => true ) ),
'polygon' => array_merge( $common, array( 'points' => true ) ),
'line' => array_merge(
$common,
array(
'x1' => true,
'y1' => true,
'x2' => true,
'y2' => true,
'stroke' => true,
'stroke-width' => true,
'stroke-linecap' => true,
)
),
'g' => $common,
);
}
}