'; // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- data URI for the menu icon, as WordPress expects. return 'data:image/svg+xml;base64,' . base64_encode( $svg ); } /** * Top-level "PDFkind" menu item. */ public static function menu() { self::$hook = add_menu_page( __( 'PDFkind', 'pdfkind' ), __( 'PDFkind', 'pdfkind' ), 'manage_options', Pdfkind_Settings::PAGE, array( __CLASS__, 'render' ), self::icon(), 81 ); } /** * Styles and script of the PDFkind screen only. * * @param string $hook Current admin page. */ public static function assets( $hook ) { if ( $hook !== self::$hook ) { return; } wp_enqueue_style( 'pdfkind-admin', PDFKIND_URL . 'assets/admin.css', array(), PDFKIND_VERSION ); wp_enqueue_script( 'pdfkind-admin', PDFKIND_URL . 'assets/admin.js', array( 'wp-i18n' ), PDFKIND_VERSION, true ); wp_set_script_translations( 'pdfkind-admin', 'pdfkind' ); wp_add_inline_script( 'pdfkind-admin', 'window.pdfkindAdmin = ' . wp_json_encode( array( 'script' => PDFKIND_ORIGIN . '/embed.js?ver=' . PDFKIND_EMBED_VERSION, 'lang' => Pdfkind_Settings::lang(), 'max' => Pdfkind_Settings::get()['max'], 'color' => Pdfkind_Settings::get()['color'], ) ) . ';', 'before' ); } /** * Smallest file limit set in the site's forms, to suggest the same limit. * Reads Contact Form 7 and WPForms forms; returns null when nothing is found. * * @return array{bytes: int, label: string, form: string}|null */ public static function form_limit() { $found = null; $keep = function ( $bytes, $form ) use ( &$found ) { if ( $bytes > 0 && ( null === $found || $bytes < $found['bytes'] ) ) { $found = array( 'bytes' => $bytes, 'form' => $form, ); } }; if ( post_type_exists( 'wpcf7_contact_form' ) ) { $forms = get_posts( array( 'post_type' => 'wpcf7_contact_form', 'numberposts' => 30, 'post_status' => 'any', ) ); foreach ( $forms as $form ) { // Contact Form 7 reads its form template from a meta field; older versions keep it in the content. $tpl = (string) get_post_meta( $form->ID, '_form', true ); $tpl = '' !== $tpl ? $tpl : $form->post_content; if ( preg_match_all( '/\[file\*?\s[^\]]*/i', $tpl, $fields ) ) { foreach ( $fields[0] as $field ) { // a file field without a limit accepts 1 MB, Contact Form 7's default. $bytes = 1048576; if ( preg_match( '/limit:(\d+)(mb|kb)?/i', $field, $m ) ) { // Contact Form 7 counts 1 MB as 1048576 bytes and 1 KB as 1024. $unit = isset( $m[2] ) ? strtolower( $m[2] ) : ''; $bytes = (int) $m[1] * ( 'mb' === $unit ? 1048576 : ( 'kb' === $unit ? 1024 : 1 ) ); } $keep( $bytes, $form->post_title ); } } } } if ( post_type_exists( 'wpforms' ) ) { $forms = get_posts( array( 'post_type' => 'wpforms', 'numberposts' => 30, 'post_status' => 'any', ) ); foreach ( $forms as $form ) { if ( false !== strpos( $form->post_content, 'file-upload' ) && preg_match_all( '/"max_size":"(\d+(?:\.\d+)?)"/', $form->post_content, $m ) ) { foreach ( $m[1] as $mb ) { $keep( (int) round( (float) $mb * 1048576 ), $form->post_title ); } } } } if ( null === $found ) { return null; } // shown and saved in whole megabytes when it is one (1048576 → 1 MB), otherwise in KB. $mb = $found['bytes'] / 1048576; $found['label'] = ( abs( $mb - round( $mb ) ) < 0.01 && $mb >= 1 ) ? round( $mb ) . 'MB' : (int) floor( $found['bytes'] / 1000 ) . 'KB'; return $found; } /** * Published pages that show a supported form, for the status card. * * @return array */ public static function form_pages() { $needles = array( '[contact-form-7' => 'Contact Form 7', 'wp:contact-form-7' => 'Contact Form 7', '[wpforms' => 'WPForms', 'wp:wpforms' => 'WPForms', '[gravityform' => 'Gravity Forms', 'wp:gravityforms' => 'Gravity Forms', '[ninja_form' => 'Ninja Forms', 'wp:ninja-forms' => 'Ninja Forms', '[formidable' => 'Formidable Forms', 'wp:formidable' => 'Formidable Forms', ); $pages = array(); foreach ( $needles as $needle => $plugin ) { $ids = get_posts( array( 'post_type' => array( 'page', 'post' ), 'post_status' => 'publish', 's' => $needle, 'sentence' => true, 'posts_per_page' => 10, 'fields' => 'ids', ) ); foreach ( $ids as $id ) { if ( ! isset( $pages[ $id ] ) ) { $pages[ $id ] = array( 'title' => get_the_title( $id ) ? get_the_title( $id ) : __( '(no title)', 'pdfkind' ), 'url' => get_permalink( $id ), 'plugin' => $plugin, ); } } } return array_slice( $pages, 0, 10, true ); } /** * The size choice: presets plus "Other". * * @param string $current Selected limit. */ private static function size_picker( $current ) { $name = Pdfkind_Settings::OPTION; $is_preset = in_array( strtoupper( $current ), Pdfkind_Settings::PRESETS, true ); ?>
' . esc_html__( 'The swatches come from your theme.', 'pdfkind' ) . '

'; } } /** * The current values as hidden fields, so a single button can change one thing. * * @param array $values Values to send. */ private static function hidden( $values ) { foreach ( $values as $key => $value ) { if ( false === $value ) { continue; } printf( '', esc_attr( Pdfkind_Settings::OPTION ), esc_attr( $key ), esc_attr( true === $value ? '1' : (string) $value ) ); } } /** * The PDFkind screen. */ public static function render() { if ( ! current_user_can( 'manage_options' ) ) { return; } $s = Pdfkind_Settings::get(); $on = Pdfkind_Settings::saved() && $s['enabled']; $name = Pdfkind_Settings::OPTION; $limit = self::form_limit(); $pages = self::form_pages(); $site = Pdfkind_Settings::LANGUAGES[ Pdfkind_Settings::site_lang() ]; // before the first save, suggest the limit found in the site's forms. $current = ( ! Pdfkind_Settings::saved() && $limit ) ? $limit['label'] : $s['max']; ?>

true, 'where' => $s['where'], 'lang' => $s['lang'], 'credit' => $s['credit'], 'color' => $s['color'], ) ); ?>

false, 'max' => $s['max'], 'where' => $s['where'], 'lang' => $s['lang'], 'credit' => $s['credit'], 'color' => $s['color'], ) ); ?>
true, 'max' => $s['max'], ) ); ?>

[pdfkind tool="merge-pdf"]' ); ?>

id ) { return; } delete_transient( 'pdfkind_activated' ); printf( '

%s %s

%s

', esc_html__( 'PDFkind is installed.', 'pdfkind' ), esc_html__( 'Turn it on in one click to stop "file too big" errors on your forms.', 'pdfkind' ), esc_url( self::url() ), esc_html__( 'Set up PDFkind', 'pdfkind' ) ); } /** * "Settings" link on the Plugins screen. * * @param array $links Existing links. * @return array */ public static function action_links( $links ) { array_unshift( $links, '' . esc_html__( 'Settings', 'pdfkind' ) . '' ); return $links; } }