| | 903 | |
| | 904 | /** |
| | 905 | * Load hooks to prevent plugin updates if PHP version requirement not met. |
| | 906 | */ |
| | 907 | public function enforce_php_requirement() { |
| | 908 | add_filter( 'plugin_row_meta', array( $this, 'plugin_update_nag' ), 10, 2 ); |
| | 909 | } |
| | 910 | |
| | 911 | /** |
| | 912 | * Adds small PHP upgrade nag to plugin row. |
| | 913 | * |
| | 914 | * @param array $links |
| | 915 | * @param string $file |
| | 916 | * |
| | 917 | * @return array $links |
| | 918 | */ |
| | 919 | public function plugin_update_nag( $links, $file ) { |
| | 920 | $slug = dirname( $file ); |
| | 921 | if ( $this->is_required_php( $slug ) ) { |
| | 922 | $response = $this->get_plugin_dot_org_api_data( $slug ); |
| | 923 | $links[] = sprintf( '<span style="color:#f00;">' . __( 'PHP v%s required for available plugin update.' ) . '</span>', $response->requires_php ); |
| | 924 | add_action( "after_plugin_row_{$file}", array( $this, 'remove_plugin_update_row' ), 10, 2 ); |
| | 925 | } |
| | 926 | |
| | 927 | return $links; |
| | 928 | } |
| | 929 | |
| | 930 | /** |
| | 931 | * Write out inline style to hide the update row notice. |
| | 932 | * |
| | 933 | * @param string $plugin_file Unused. |
| | 934 | * @param array $plugin_data Plugin data. |
| | 935 | */ |
| | 936 | public function remove_plugin_update_row( $plugin_file, $plugin_data ) { |
| | 937 | print( '<script>' ); |
| | 938 | print( 'jQuery(".update[data-plugin=\'' . $plugin_file . '\']").removeClass("update");' ); |
| | 939 | print( 'jQuery("tr#' . $plugin_data['slug'] . '-update").remove();' ); |
| | 940 | print( '</script>' ); |
| | 941 | } |
| | 942 | |
| | 943 | /** |
| | 944 | * Unset update_plugins from update-core.php as appropriate. |
| | 945 | * |
| | 946 | * @param array $plugins Plugins for updating. |
| | 947 | * |
| | 948 | * @return array $plugins Plugins for updating. |
| | 949 | */ |
| | 950 | public function unset_plugin_updates( $plugins ) { |
| | 951 | if ( ! empty( $plugins ) ) { |
| | 952 | foreach ( $plugins as $file => $class ) { |
| | 953 | if ( $this->is_required_php( $class->update->slug ) ) { |
| | 954 | unset( $plugins[ $file ] ); |
| | 955 | $this->requires_php_update_notice(); |
| | 956 | } |
| | 957 | } |
| | 958 | } |
| | 959 | |
| | 960 | return $plugins; |
| | 961 | } |
| | 962 | |
| | 963 | /** |
| | 964 | * Create requires PHP update notice. |
| | 965 | */ |
| | 966 | public function requires_php_update_notice() { |
| | 967 | $message = '<span style="color:#f00;" class="dashicons dashicons-warning"></span> '; |
| | 968 | $message .= __( 'Some updates are not shown in this list because they require a newer version of PHP.' ); |
| | 969 | print( '<div class="notice-error notice"><p>' . $message . '</p></div>' ); |
| | 970 | } |
| | 971 | |
| | 972 | /** |
| | 973 | * Test for required PHP version. |
| | 974 | * |
| | 975 | * @param string $slug Slug of the repository being tested. |
| | 976 | * |
| | 977 | * @return bool True for below required PHP version. |
| | 978 | * False for above required PHP version or none set. |
| | 979 | */ |
| | 980 | protected function is_required_php( $slug ) { |
| | 981 | $response = $this->get_plugin_dot_org_api_data( $slug ); |
| | 982 | |
| | 983 | if ( ! $response->requires_php ) { |
| | 984 | return false; |
| | 985 | } |
| | 986 | |
| | 987 | return version_compare( PHP_VERSION, $response->requires_php, '<=' ); |
| | 988 | } |
| | 989 | |
| | 990 | /** |
| | 991 | * Get the dot org API data for the plugin or theme slug. |
| | 992 | * |
| | 993 | * @param string $slug Plugin or theme slug. |
| | 994 | * |
| | 995 | * @return object|bool $response |
| | 996 | */ |
| | 997 | protected function get_plugin_dot_org_api_data( $slug ) { |
| | 998 | $response = get_site_transient( 'php_check-' . $slug ); |
| | 999 | if ( ! $response ) { |
| | 1000 | $url = 'https://api.wordpress.org/plugins/info/1.2/'; |
| | 1001 | $url = add_query_arg( array( |
| | 1002 | 'action' => 'plugin_information', |
| | 1003 | 'request[slug]' => $slug, |
| | 1004 | ), $url ); |
| | 1005 | $response = wp_remote_get( $url ); |
| | 1006 | $response = null !== $response['body'] ? json_decode( $response['body'] ) : false; |
| | 1007 | |
| | 1008 | // Plugins not in dot org. |
| | 1009 | if ( ! isset( $response->requires_php ) ) { |
| | 1010 | $response->requires_php = false; |
| | 1011 | } |
| | 1012 | set_site_transient( 'php_check-' . $slug, $response ); |
| | 1013 | } |
| | 1014 | |
| | 1015 | return $response; |
| | 1016 | } |
| | 1017 | |