plugin = $plugin;
}
/**
* Hooks.
*/
public function register(): void {
add_action( 'admin_menu', array( $this, 'menu' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'assets' ) );
add_action( 'admin_notices', array( $this, 'notices' ) );
add_filter( 'plugin_action_links_' . THINROOF_BASENAME, array( $this, 'action_links' ) );
add_filter( 'set-screen-option', array( $this, 'set_screen_option' ), 10, 3 );
foreach ( array( 'save_settings', 'save_pricing', 'save_calculator', 'calculator_action', 'lead_update', 'lead_delete', 'export_csv', 'test_email' ) as $action ) {
add_action( 'admin_post_thinroof_' . $action, array( $this, 'handle_' . $action ) );
}
}
/**
* Menu.
*/
public function menu(): void {
$leads = Capabilities::MANAGE_LEADS;
$settings = Capabilities::MANAGE_SETTINGS;
$new = (int) ( $this->plugin->leads->status_counts()['new'] ?? 0 );
$bubble = $new ? ' ' . esc_html( number_format_i18n( $new ) ) . ' ' : '';
$this->hooks['dashboard'] = (string) add_menu_page( __( 'ThinRoof', 'thinroof' ), __( 'ThinRoof', 'thinroof' ) . $bubble, $leads, self::SLUG, array( $this, 'page_dashboard' ), 'dashicons-admin-home', 26 );
add_submenu_page( self::SLUG, __( 'Dashboard', 'thinroof' ), __( 'Dashboard', 'thinroof' ), $leads, self::SLUG, array( $this, 'page_dashboard' ) );
$this->hooks['calculators'] = (string) add_submenu_page( self::SLUG, __( 'Calculators', 'thinroof' ), __( 'Calculators', 'thinroof' ), $settings, 'thinroof-calculators', array( $this, 'page_calculators' ) );
$this->hooks['leads'] = (string) add_submenu_page( self::SLUG, __( 'Leads', 'thinroof' ), __( 'Leads', 'thinroof' ), $leads, 'thinroof-leads', array( $this, 'page_leads' ) );
$this->hooks['pricing'] = (string) add_submenu_page( self::SLUG, __( 'Pricing', 'thinroof' ), __( 'Pricing', 'thinroof' ), $settings, 'thinroof-pricing', array( $this, 'page_pricing' ) );
$this->hooks['email'] = (string) add_submenu_page( self::SLUG, __( 'Email Settings', 'thinroof' ), __( 'Email Settings', 'thinroof' ), $settings, 'thinroof-email', array( $this, 'page_email' ) );
$this->hooks['settings'] = (string) add_submenu_page( self::SLUG, __( 'General Settings', 'thinroof' ), __( 'General Settings', 'thinroof' ), $settings, 'thinroof-settings', array( $this, 'page_settings' ) );
/**
* Premium adds its own submenu pages here.
*
* @param string $parent Parent slug.
*/
do_action( 'thinroof_admin_menu', self::SLUG );
add_action( 'load-' . $this->hooks['leads'], array( $this, 'load_leads' ) );
}
/**
* Assets on our screens only.
*
* @param string $hook Hook suffix.
*/
public function assets( string $hook ): void {
if ( ! in_array( $hook, $this->hooks, true ) ) {
return;
}
$is_settings = ( $this->hooks['settings'] ?? '' ) === $hook;
if ( $is_settings ) {
wp_enqueue_media();
wp_enqueue_style( 'wp-color-picker' );
}
wp_enqueue_style( 'thinroof-admin', THINROOF_URL . 'admin/css/admin.css', array(), THINROOF_VERSION );
wp_enqueue_script( 'thinroof-admin', THINROOF_URL . 'admin/js/admin.js', $is_settings ? array( 'jquery', 'wp-color-picker' ) : array(), THINROOF_VERSION, true );
wp_localize_script(
'thinroof-admin',
'thinroofAdmin',
array(
'copied' => __( 'Copied!', 'thinroof' ),
'confirm' => __( 'Delete permanently? This cannot be undone.', 'thinroof' ),
'chooseLogo' => __( 'Choose logo', 'thinroof' ),
'useLogo' => __( 'Use this logo', 'thinroof' ),
)
);
}
/**
* Notices.
*/
public function notices(): void {
$screen = get_current_screen();
$ours = $screen && in_array( $screen->id, $this->hooks, true );
if ( get_transient( 'thinroof_activation_notice' ) && current_user_can( Capabilities::MANAGE_SETTINGS ) ) {
delete_transient( 'thinroof_activation_notice' );
$sample = (int) get_option( 'thinroof_sample_calculator_id', 0 );
echo '
';
printf(
/* translators: 1: shortcode, 2: pricing URL. */
wp_kses_post( __( 'ThinRoof is ready. A sample calculator was created; add %1$s to any page. Next, set your prices .', 'thinroof' ) ),
'[thinroof_calculator id="' . esc_html( (string) $sample ) . '"]',
esc_url( admin_url( 'admin.php?page=thinroof-pricing' ) )
);
echo '
';
}
if ( ! $ours ) {
return;
}
$page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen check.
if ( ! $this->plugin->settings->pricing_reviewed() && current_user_can( Capabilities::MANAGE_SETTINGS ) && 'thinroof-pricing' !== $page ) {
echo '';
printf(
/* translators: %s: pricing URL. */
wp_kses_post( __( 'Your calculator is using example prices , not your own. Review and save your pricing before visitors see estimates.', 'thinroof' ) ),
esc_url( admin_url( 'admin.php?page=thinroof-pricing' ) )
);
echo '
';
}
$failure = $this->plugin->notifications->last_failure();
if ( $failure && current_user_can( Capabilities::MANAGE_SETTINGS ) ) {
echo '';
printf(
/* translators: 1: error message, 2: email settings URL. */
wp_kses_post( __( 'The last ThinRoof email could not be sent: %1$s Check email settings .', 'thinroof' ) ),
'' . esc_html( (string) $failure['error'] ) . '',
esc_url( admin_url( 'admin.php?page=thinroof-email' ) )
);
echo '
';
}
$message = isset( $_GET['thinroof_msg'] ) ? sanitize_key( wp_unslash( $_GET['thinroof_msg'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$texts = array(
'saved' => __( 'Settings saved.', 'thinroof' ),
'deleted' => __( 'Deleted.', 'thinroof' ),
'duplicated' => __( 'Calculator duplicated. The copy is inactive until you activate it.', 'thinroof' ),
'updated' => __( 'Lead updated.', 'thinroof' ),
'mail_ok' => __( 'Test email sent. Check the inbox (and spam folder) of the address you entered.', 'thinroof' ),
'mail_fail' => __( 'The test email could not be sent. See the delivery log below for the reason.', 'thinroof' ),
'error' => __( 'Something went wrong. Please try again.', 'thinroof' ),
);
if ( isset( $texts[ $message ] ) ) {
$type = in_array( $message, array( 'mail_fail', 'error' ), true ) ? 'error' : 'success';
echo '' . esc_html( $texts[ $message ] ) . '
';
}
}
/**
* Plugin list links.
*
* @param array $links Links.
*/
public function action_links( array $links ): array {
array_unshift( $links, '' . esc_html__( 'Settings', 'thinroof' ) . ' ' );
return $links;
}
/**
* Persist "Number of items per page".
*
* @param mixed $status Status.
* @param string $option Option.
* @param mixed $value Value.
* @return mixed
*/
public function set_screen_option( $status, $option, $value ) {
return 'thinroof_leads_per_page' === $option ? max( 5, min( 200, (int) $value ) ) : $status;
}
/* ------------------------------------------------------------------
* Pages
* ------------------------------------------------------------------ */
/**
* Dashboard.
*/
public function page_dashboard(): void {
$this->view(
'dashboard',
array(
'stats' => $this->plugin->leads->stats(),
'monthly' => $this->plugin->leads->monthly( 12 ),
'recent' => $this->plugin->leads->query( array( 'per_page' => 8 ) )['items'],
'sample' => (int) get_option( 'thinroof_sample_calculator_id', 0 ),
'labels' => Labels::instance(),
)
);
}
/**
* Calculators list / edit.
*/
public function page_calculators(): void {
$this->require_cap( Capabilities::MANAGE_SETTINGS );
$edit = isset( $_GET['edit'] ) ? absint( $_GET['edit'] ) : null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$new = isset( $_GET['action'] ) && 'new' === $_GET['action']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( $new || $edit ) {
$calculator = $edit ? $this->plugin->calculators->get( $edit ) : null;
if ( $edit && ! $calculator ) {
wp_die( esc_html__( 'Calculator not found.', 'thinroof' ) );
}
$this->view(
'calculator-edit',
array(
'calculator' => $calculator,
'config' => $calculator ? $calculator['config'] : Calculator::default_config(),
'options' => $this->plugin->engine->options( null ),
)
);
return;
}
$this->view( 'calculators', array( 'calculators' => $this->plugin->calculators->all() ) );
}
/**
* Load hook for leads: screen options and bulk actions before output.
*/
public function load_leads(): void {
$this->require_cap( Capabilities::MANAGE_LEADS );
if ( isset( $_GET['lead'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return;
}
add_screen_option(
'per_page',
array(
'label' => __( 'Leads per page', 'thinroof' ),
'default' => 20,
'option' => 'thinroof_leads_per_page',
)
);
$this->table = new Leads_List_Table( $this->plugin );
$this->table->process_bulk_action();
}
/**
* Leads list / detail.
*/
public function page_leads(): void {
$this->require_cap( Capabilities::MANAGE_LEADS );
$lead_id = isset( $_GET['lead'] ) ? absint( $_GET['lead'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( $lead_id ) {
$lead = $this->plugin->leads->get( $lead_id );
if ( ! $lead ) {
wp_die( esc_html__( 'Lead not found.', 'thinroof' ) );
}
$calculator = $this->plugin->calculators->get( (int) $lead['calculator_id'] );
$this->view(
'lead',
array(
'lead' => $lead,
'estimate' => Lead_Manager::estimate_data( $lead ),
'activity' => $this->plugin->leads->activity( $lead_id ),
'labels' => Labels::instance(),
'calculator' => $calculator,
)
);
return;
}
if ( ! $this->table ) {
$this->table = new Leads_List_Table( $this->plugin );
}
$this->table->prepare_items();
$this->view(
'leads',
array(
'table' => $this->table,
'calculators' => $this->plugin->calculators->all(),
)
);
}
/**
* Pricing.
*/
public function page_pricing(): void {
$this->require_cap( Capabilities::MANAGE_SETTINGS );
$this->view(
'pricing',
array(
'pricing' => $this->plugin->settings->get( 'pricing' ),
'materials' => $this->plugin->pricing->materials(),
'reviewed' => $this->plugin->settings->pricing_reviewed(),
)
);
}
/**
* Email.
*/
public function page_email(): void {
$this->require_cap( Capabilities::MANAGE_SETTINGS );
$this->view(
'email',
array(
'email' => $this->plugin->settings->get( 'email' ),
'log' => $this->plugin->notifications->log_entries(),
'business_name' => $this->plugin->notifications->business_name(),
)
);
}
/**
* General.
*/
public function page_settings(): void {
$this->require_cap( Capabilities::MANAGE_SETTINGS );
$this->view( 'settings', array( 'general' => $this->plugin->settings->get( 'general' ) ) );
}
/* ------------------------------------------------------------------
* admin-post handlers
* ------------------------------------------------------------------ */
/**
* Save general or email settings.
*/
public function handle_save_settings(): void {
$this->require_cap( Capabilities::MANAGE_SETTINGS );
$group = isset( $_POST['group'] ) ? sanitize_key( wp_unslash( $_POST['group'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified next.
if ( ! in_array( $group, array( 'general', 'email' ), true ) ) {
wp_die( esc_html__( 'Invalid request.', 'thinroof' ), 400 );
}
check_admin_referer( 'thinroof_save_' . $group );
$input = isset( $_POST['thinroof'] ) && is_array( $_POST['thinroof'] ) ? wp_unslash( $_POST['thinroof'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitised per key in Settings::save().
$this->plugin->settings->save( $group, $input );
Labels::reset();
$this->redirect( 'email' === $group ? 'thinroof-email' : 'thinroof-settings', 'saved' );
}
/**
* Save pricing (global settings + material rows).
*/
public function handle_save_pricing(): void {
$this->require_cap( Capabilities::MANAGE_SETTINGS );
check_admin_referer( 'thinroof_save_pricing' );
$input = isset( $_POST['thinroof'] ) && is_array( $_POST['thinroof'] ) ? wp_unslash( $_POST['thinroof'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitised per key.
$this->plugin->pricing->update_from_input( isset( $input['materials'] ) && is_array( $input['materials'] ) ? $input['materials'] : array() );
unset( $input['materials'] );
$this->plugin->settings->save( 'pricing', $input );
Labels::reset();
$this->redirect( 'thinroof-pricing', 'saved' );
}
/**
* Create/update a calculator.
*/
public function handle_save_calculator(): void {
$this->require_cap( Capabilities::MANAGE_SETTINGS );
check_admin_referer( 'thinroof_save_calculator' );
$id = isset( $_POST['calculator_id'] ) ? absint( $_POST['calculator_id'] ) : 0;
$input = isset( $_POST['thinroof'] ) && is_array( $_POST['thinroof'] ) ? wp_unslash( $_POST['thinroof'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitised in Calculator::save().
$saved = $this->plugin->calculators->save(
array(
'title' => $input['title'] ?? '',
'status' => $input['status'] ?? 'active',
'config' => isset( $input['config'] ) && is_array( $input['config'] ) ? $input['config'] : array(),
),
$id
);
wp_safe_redirect(
add_query_arg(
array(
'page' => 'thinroof-calculators',
'edit' => $saved ? $saved : $id,
'thinroof_msg' => $saved ? 'saved' : 'error',
),
admin_url( 'admin.php' )
)
);
exit;
}
/**
* Duplicate / delete a calculator.
*/
public function handle_calculator_action(): void {
$this->require_cap( Capabilities::MANAGE_SETTINGS );
$id = isset( $_GET['calculator'] ) ? absint( $_GET['calculator'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- verified next.
$task = isset( $_GET['task'] ) ? sanitize_key( wp_unslash( $_GET['task'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
check_admin_referer( 'thinroof_calculator_' . $task . '_' . $id );
if ( 'duplicate' === $task ) {
$this->redirect( 'thinroof-calculators', $this->plugin->calculators->duplicate( $id ) ? 'duplicated' : 'error' );
}
if ( 'delete' === $task ) {
$this->redirect( 'thinroof-calculators', $this->plugin->calculators->delete( $id ) ? 'deleted' : 'error' );
}
$this->redirect( 'thinroof-calculators', 'error' );
}
/**
* Update status and/or add a note.
*/
public function handle_lead_update(): void {
$this->require_cap( Capabilities::MANAGE_LEADS );
$id = isset( $_POST['lead_id'] ) ? absint( $_POST['lead_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified next.
check_admin_referer( 'thinroof_lead_update_' . $id );
if ( ! $this->plugin->leads->get( $id ) ) {
$this->redirect( 'thinroof-leads', 'error' );
}
$user = get_current_user_id();
$status = isset( $_POST['status'] ) ? sanitize_key( wp_unslash( $_POST['status'] ) ) : '';
if ( '' !== $status ) {
$this->plugin->leads->update_status( $id, $status, $user );
}
$note = isset( $_POST['note'] ) ? sanitize_textarea_field( wp_unslash( $_POST['note'] ) ) : '';
if ( '' !== trim( $note ) ) {
$this->plugin->leads->log( $id, 'note', $note, $user );
}
$this->redirect( 'thinroof-leads', 'updated', array( 'lead' => $id ) );
}
/**
* Delete one lead.
*/
public function handle_lead_delete(): void {
$this->require_cap( Capabilities::MANAGE_LEADS );
$id = isset( $_GET['lead'] ) ? absint( $_GET['lead'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- verified next.
check_admin_referer( 'thinroof_lead_delete_' . $id );
$this->redirect( 'thinroof-leads', $this->plugin->leads->delete( array( $id ) ) ? 'deleted' : 'error' );
}
/**
* Stream CSV for the current filters.
*/
public function handle_export_csv(): void {
$this->require_cap( Capabilities::MANAGE_LEADS );
check_admin_referer( 'thinroof_export_csv' );
$args = Leads_List_Table::filters_from_request( $_GET ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- verified above.
nocache_headers();
header( 'Content-Type: text/csv; charset=utf-8' );
header( 'Content-Disposition: attachment; filename="thinroof-leads-' . gmdate( 'Y-m-d' ) . '.csv"' );
header( 'X-Content-Type-Options: nosniff' );
$out = fopen( 'php://output', 'w' );
fwrite( $out, "\xEF\xBB\xBF" ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite -- UTF-8 BOM so Excel reads accents.
$this->plugin->leads->write_csv( $out, $args );
fclose( $out ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
exit;
}
/**
* Send a test email.
*/
public function handle_test_email(): void {
$this->require_cap( Capabilities::MANAGE_SETTINGS );
check_admin_referer( 'thinroof_test_email' );
$to = isset( $_POST['test_to'] ) ? sanitize_email( wp_unslash( $_POST['test_to'] ) ) : '';
$result = $this->plugin->notifications->send_test( $to );
$this->redirect( 'thinroof-email', true === $result ? 'mail_ok' : 'mail_fail' );
}
/* ------------------------------------------------------------------
* Helpers
* ------------------------------------------------------------------ */
/**
* Render a view.
*
* @param string $name View.
* @param array $vars Variables.
*/
private function view( string $name, array $vars ): void {
$file = THINROOF_PATH . 'admin/views/' . $name . '.php';
if ( ! is_readable( $file ) ) {
return;
}
$vars['plugin'] = $this->plugin;
( static function ( string $__file, array $__vars ) {
extract( $__vars, EXTR_SKIP ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract -- view variables.
include $__file;
} )( $file, $vars );
}
/**
* Die unless the user has a capability.
*
* @param string $cap Capability.
*/
private function require_cap( string $cap ): void {
if ( ! current_user_can( $cap ) ) {
wp_die( esc_html__( 'You do not have permission to do that.', 'thinroof' ), 403 );
}
}
/**
* Redirect to one of our pages with a message.
*
* @param string $page Page slug.
* @param string $message Message key.
* @param array $extra Extra args.
*/
private function redirect( string $page, string $message, array $extra = array() ): void {
wp_safe_redirect(
add_query_arg(
array_merge(
array(
'page' => $page,
'thinroof_msg' => $message,
),
$extra
),
admin_url( 'admin.php' )
)
);
exit;
}
/**
* Shared helper for views: a Yes/No/Hidden field state select.
*
* @param string $name Input name.
* @param string $current Current state.
* @param bool $no_required Omit "Required".
*/
public static function state_select( string $name, string $current, bool $no_required = false ): void {
$states = array(
'required' => __( 'Required', 'thinroof' ),
'optional' => __( 'Optional', 'thinroof' ),
'hidden' => __( 'Hidden', 'thinroof' ),
);
if ( $no_required ) {
unset( $states['required'] );
}
echo '';
foreach ( $states as $value => $label ) {
echo '' . esc_html( $label ) . ' ';
}
echo ' ';
}
/**
* Settings URL helper for views.
*
* @param string $page Page.
* @param array $args Args.
*/
public static function url( string $page, array $args = array() ): string {
return add_query_arg( array_merge( array( 'page' => $page ), $args ), admin_url( 'admin.php' ) );
}
}